#include<stdio.h>
void main(){
int i;
int mul = 1;
for(i = 50; i > 1; i--){
if(i % 2 == 0){
mul = mul * i;
}
}
printf("\n Multiplication is %d",mul);
}
The answer comes zero. Tried it many times but always shows zero.
#include<stdio.h>
void main(){
int i;
int mul = 1;
for(i = 50; i > 1; i--){
if(i % 2 == 0){
mul = mul * i;
}
}
printf("\n Multiplication is %d",mul);
}
The answer comes zero. Tried it many times but always shows zero.
What you should do to debug:
for(i = 50; i > 1; i--){
printf("i: %d i%2: %d mul: %d\n", i, i%2, mul);
if(i % 2 == 0){
mul = mul * i;
}
printf("i: %d i%2: %d mul: %d\n", i, i%2, mul);
}
But as other have pointed out, the answer is too big to fit in int. And when you overflow an int the behavior is undefined, so it's not guaranteed that this code prints zero.