0% found this document useful (0 votes)
26 views4 pages

Week 4 Assignment

Doc. C

Uploaded by

afshazareen44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Week 4 Assignment

Doc. C

Uploaded by

afshazareen44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Objective:Explorethe fullscopeofexpressions,type-compatibilityofvariables

&constants and operators used in the expression and how operator precedence
works.

Suggested Experiments/Activities:

Tutorial4:Operatorsandtheprecedenceandas associativity:
Lab4:Simplecomputationalproblemsusingtheoperator’precedenceandassociativity
i) Evaluatethe followingexpressions.
a. A+B*C+(D*E)+F*G
To evaluate the expression A + B * C + (D * E) + F * G, you can follow the order of
operations (PEMDAS/BODMAS):
 Calculate the products within parentheses/brackets first: D * E = result1
 Calculate the multiplication and division from left to right: B * C = result2
 Calculate the addition and subtraction from left to right: A + result2 + result1 + F * G
= final result
 Here's how you can evaluate the expression:
 Assuming A = 2, B = 3, C = 4, D = 5, E = 6, F = 7, and G = 8:
 Calculate D * E = 5 * 6 = 30 (result1)
 Calculate B * C = 3 * 4 = 12 (result2)
 Now, evaluate the expression: A + result2 + result1 + F * G = 2 + 12 + 30 + 7 * 8
 Calculate the final result: 2 + 12 + 30 + 56 = 100
 So, the value of the expression A + B * C + (D * E) + F * G is 100 with the given
values for A, B, C, D, E, F, and G.

b. A/B*C-B+A*D/3
To evaluate the expression A/B * C - B + A * D / 3, you should follow the order of
operations (PEMDAS/BODMAS). Here's how to do it step by step:
 Assuming A = 4, B = 2, C = 3, and D = 6:
 First, perform the division: A / B = 4 / 2 = 2 A * D / 3 = 4 * 6 / 3 = 8
 Now, calculate the products and additions from left to right: 2 * C - B + 8 / 3
 Continue to perform the remaining operations: 2 * 3 - 2 + 8 / 3
 Calculate the multiplication, subtraction, and division: 6 - 2 + 8 / 3
 Finally, complete the subtraction and division: 6 - 2 + 2.67 (rounded to two decimal
places)
 Calculate the final result: 6 - 2 + 2.67 = 6.67
 So, the value of the expression A/B * C - B + A * D / 3 is approximately 6.67 with the
given values for A, B, C, and D.

c. A+++B---A
The expression A +++ B --- A contains increment and decrement operators. The increment
operator (++x) increases the value of the variable by 1, while the decrement operator (--x)
decreases the value by 1.
To evaluate this expression, let's assume that A has an initial value of 5 and B has an initial
value of 3:
 Start with the initial values: A = 5, B = 3.
 Evaluate the expression from left to right:
 A +++ B --- A
 First, the first A is incremented (A++):
 A is now 6, and the expression becomes:
 6 ++ B --- 6
 Second, B is incremented (B++):
 A is still 6, B is now 4, and the expression becomes:
 6 ++ 4 --- 6
 Third, the second A is decremented (A--):
 A is now 5, B is still 4, and the expression becomes:
 6 ++ 4 --- 5
 Finally, perform the remaining operations:
 10 --- 5
 Perform the subtraction:
 10 - 5 = 5
 So, the value of the expression A +++ B --- A is 5 with the initial values of A and B.

d. J=(i++)+(++i)
 The expression J = (i++) + (++i) involves both the post-increment operator i++ and
the pre-increment operator ++i. The post-increment operator increases the value of i
after its current value is used, while the pre-increment operator increases the value of
`i before its current value is used.
 The result of this expression will depend on the initial value of i. Let's analyze this
step by step:
 Start with an initial value of i. Let's assume i is initially set to 5.
 Evaluate the expression from left to right:
 J = (i++) + (++i)
 First, i++ is evaluated. Since it's a post-increment, the current value of i (5) is used in
this part of the expression, and then i is incremented to 6. So, at this point:
 J = 5 + (++i)
 Next, ++i is evaluated. Since it's a pre-increment, i is incremented to 7, and the
current value (7) is used in this part of the expression. So now:
 J=5+7
 Perform the addition:
 J = 12
 So, if i is initially set to 5, then the value of J after evaluating the expression J = (i++)
+ (++i) will be 12.

ii) Findthemaximumofthreenumbersusing conditionaloperator


#include <stdio.h>

int main() {
int num1, num2, num3, max;

// Input three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Find the maximum using the conditional operator


max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 :
num3);

// Display the maximum number


printf("The maximum number is: %d\n", max);
return 0;
}
Output:-
Enter three numbers: 10
20
30
The maximum number is: 30

iii) Takemarksof5subjectsinintegers, andfindthetotal, averagein float

#include <stdio.h>

int main() {
int subject1, subject2, subject3, subject4, subject5;
float total, average;

// Input marks for 5 subjects


printf("Enter marks for 5 subjects:\n");
printf("Subject 1: ");
scanf("%d", &subject1);
printf("Subject 2: ");
scanf("%d", &subject2);
printf("Subject 3: ");
scanf("%d", &subject3);
printf("Subject 4: ");
scanf("%d", &subject4);
printf("Subject 5: ");
scanf("%d", &subject5);

// Calculate the total


total = subject1 + subject2 + subject3 + subject4 + subject5;

// Calculate the average


average = total / 5.0; // Using 5.0 to ensure a floating-point division

// Display the total and average


printf("Total marks: %.0f\n", total); // Displaying total without decimal places
printf("Average marks: %.2f\n", average); // Displaying average with 2 decimal places

return 0;
}
Output:-
Enter marks for 5 subjects:
Subject 1: 50
Subject 2: 50
Subject 3: 60
Subject 4: 70
Subject 5: 80
Total marks: 310
Average marks: 62.00
Result:-
Hence the above programs has been Successfully Executed.

You might also like