Assignment3 July2024
Assignment3 July2024
a) -1
b) 1
c) 0
d) Compilation error
Solution: (d) 'int to binary ' operator ‘%' cannot be operated on a floating variable. Thus, i%2 is not a valid
operation in C. The compiler will show an error at this step.
4. Find the output of the following C code. (% indicates modulo operation, which results in the
remainder of a division operation)
#include<stdio.h>
int main()
{
int a=10, b=3, c=2, d=4, result;
result=a+a*-b/c%d+c*d;
printf("%d", result);
return 0;
}
a) -42
b) 24
c) 15
d) -34
Problem Solving through Programming in C
Week 03 Assignment Solution
Solution: (c) Following the precedence rule, we can conclude that the operation steps are
Result=10+10*- 3/2%4+2*4
Result=10-30/2%4+2*4
Result=10-15%4+2*4
Result=10-3+2*4
Result=10-3+8
Result=7+8
Result=15
a) 0
b) 3
c) 4
d) Compilation error
Solution: (c) ‘?:’ is Conditional Expression. If Condition is true ? then value X : otherwise value Y. After
simplifying the expression, we get 36<40?4:3. The condition in LHS of ? is true. Thus 4 will be stored in b.
#include <stdio.h>
int main() {
int x = 1, y = 0;
if (x && y) {
printf("Both are true\n");
} else {
printf("At least one is false\n");
}
return 0;
}
Solution: (b) Since y is 0, the condition x && y evaluates to false, so "At least one is false" will be printed.
Problem Solving through Programming in C
Week 03 Assignment Solution
#include <stdio.h>
int main() {
int x = 10;
if (x = 5) {
printf("x is 5\n");
} else {
printf("x is not 5\n");
}
return 0;
}
a) x is 5
b) x is not 5
c) Compilation error
d) None of the above
Solution: (a) The condition if (x = 5) is an assignment, not a comparison. It assigns 5 to x, which evaluates
to true, so "x is 5" will be printed.
a) b is positive
b) b is non-positive
c) a is greater than b
d) b is positive a is greater than b
Solution: (d) Both "b is positive" and "a is greater than b" will be printed because the second printf is not
part of the inner if-else block.
Solution: (c)
Problem Solving through Programming in C
Week 03 Assignment Solution
a) 0
b) 1
c) 7
d) Compilation error
Solution: (a) 0