tags:

views:

103

answers:

4

can any one give me the dry output for this program?

#include <stdio.h>
main()
{
int a,b,c,d,e;
printf("Enter the Number to Find it's Reverse\n");
scanf("%d",&a);
while(a!=0)
{
b=a%10;
c=a/10;
printf("%d",b);
a=c;
}
getchar();
}
+3  A: 

Here you go:

Enter the Number to Find it's Reverse

:)

(Assuming the application compiles/runs perfectly and no input is given (my interpretation of "dry"))

Felix Kling
It should be *its* ;)
plaes
@plaes: I did not write the code! ;)
Felix Kling
+1  A: 

As it says itself, it waits until you enter a number then it prints the reverse. So if you enter 367 you get 763. The algorithm is quite straightforward and very popular. The % is used to get modulas of the number and 10. So you get the last digit each time. (ie. 367 % 10 is 7) and then it replaces the old number (i.e. 367) with itself divided by ten (i.e. 36) and it goes on until it gets to 0. Note: The line c=a/10; can also be replaced by a=a/10. Then the program waits (getchar()) until you press a key and then it closes. :)

Auxiliary
+5  A: 

Assuming that from dry output you mean explanation of the code, here is my attempt at it.

Suppose user enters 143. So now a = 143.

while( a != 0 )  // a = 143 therefor condition is true and the block of
                 // code inside the loop is executed.
b =  a % 10 ;  // 143 % 10  ( The remainder is 3 )

So value of b is printed on screen

3

Now

c = a / 10 ;  // 143 / 10 =  14  
a = c ;       // so now a = 14

Once again, we return to the while()

while( a != 0 )  // a = 14 therefor condition is true and the block of
                 // code inside the loop is executed.
b =  a % 10 ;  // 14 % 10  ( The remainder is 4 )

So value of b is printed on screen, which already has 3

34

Now

c = a / 10 ;  // 14 / 10 =  1
a = c ;       // so now a = 1

Again, we return to the while()

while( a != 0 )  // a = 1 therefor condition is true and the block of
                 // code inside the loop is executed.
b =  a % 10 ;  // 1 % 10  ( its output will be 1 )

So value of b is printed on screen which already has 34

341

Now

c = a / 10 ;  // 1 / 10 =  0
a = c ;       // so now a = 0

We return to the while()

while( a != 0 )  // a = 0 therefor condition is FALSE and the block of
                 // code inside the loop is NOT executed.

Hope it was helpful.
Note
Instead of

c=a/10;
a=c;

You can simply write

a /= 10

Secondly,

int a,b,c,d,e;

What is the purpose of e?

Andrew-Dufresne
tnx man it was very helpful of u
shujaat
A: 

It reveres the entered number.Dry output is if you enter 123 you get 321.By the way what the variables d and e for? Remove them else your dry output will be a comiler eror LOL

fahad