vendredi 23 juin 2017

C: explaining piece of code! While, if, else. Changing values for other possible integers?

I'm new here and need some help:

Explain what the following program is doing? Is the program working fine for all integers? If the program does have a bug, then what is the fix? Tip: change the values of x and y for other possible integers (odd and even numbers) and see if you get the correct result. follow the program step by step.

int main()

{

int x,y,z;    
x=50;
y=23;
z=0;

while(y!=0)

{

if(y%2==0)

{

x=2*x; 
y=y/2;

}

else 

{

z=z+x;
x=2*x;
y=(y-1)/2;

}

}

printf("%d\n",z);
return (0);

}

When I entered it, I got 1150. however, What am I supposed to explain, and how could I tell if it has a bug?

Thank you so much for your help in advance! :)

Aucun commentaire:

Enregistrer un commentaire