Maddy Maddy - 1 month ago 9
C Question

can't get output of this program

Have to find value of x :

#include<stdio.h>
int main()
{
int x=0,i;
for(i=0;i<10;i++)
{
if(i%4==2)
x+=20;
if(i%2==0)
x+=50;
}
return 0;
}


(caution i'm new to programming) i have find value of x and i can't get this one through my head anyone would to please briefly elaborate to me what is going in this program. as far as i m concerned the second if statement will print and x will be 0 or 50 ( i know this is a global platform of high end problem but please just lemme get through this one thanks in adv)

Answer

Code with comments to be easy to understand:

#include <stdio.h>

int main()
{
    int x=0,i;
    for(i=0;i<10;i++) // i = 0 to 9
    {
        if(i%4==2) // rest of i/4 is 2 (2 and 6)
        {
            x+=20;
        }
        if(i%2==0) // rest of i/2 is 0 (0, 2, 4, 6 and 8)
        {
            x+=50;
        }
    printf("Result: %d\n", x); // Prints the result (290)
    return 0;
}