Hi,

This has been discussed ealier, but i would like to know how the loop executes

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. if(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }

Suppose I enter 321
y will become 32
z will become 1
x will be 32

It will print first z which is 1
then it will print x which is 32

But it should have been 123 ( Reverse of 321)

Suppose I enter 321
y will become 32
z will become 1
x will be 32

It will print first z which is 1
then it will print x which is 32

But it should have been 123 ( Reverse of 321)

why wil it print 32.
See the code.

if(x>0)
{
 y=x/10;
 z=x%10;
 x=y;
 printf("%d",z);
}

the 32 wil again be processed similarly.
u wil have
y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0.

Why have u added the statement:

printf("%d",x);

It wil add an unwanted zero at the end of every reversed number.

hi..

you just take while loop instead of if loop and store your result in array....

#include<stdio.h>
 #include<conio.h>
void main()
{
int x,y,*z,i=0,j;
clrscr();
scanf("%d",&x);
while(x>0)
{
        y=x%10;
	z[i]=y;
	x=x/10;
        printf("%d\n",z[i]);
	i++;
}
for(j=0;j<i;j++)
{
	printf("%d",z[j]);
}
getch();
}

okay...just try this one...

you can also do this using "strrev" function...

#include<stdio.h>
 #include<conio.h>
 #include<string.h>
 void main()
 {
	char *x;

	gets(x);
	strrev(x);
	puts(x);

	getch();
 }

just try it also...

best way to do it is

int reverse(int num)
{
   int rev = 0;
   while(num>0)
   {
       rev = rev*10 + num%10;
       num = num/10;
   }
   return rev;
}

why wil it print 32.
See the code.

if(x>0)
{
 y=x/10;
 z=x%10;
 x=y;
 printf("%d",z);
}

the 32 wil again be processed similarly.
u wil have
y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0.

Why have u added the statement:

printf("%d",x);

It wil add an unwanted zero at the end of every reversed number.

I understood upto this point -

y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0

Now x=3, so,
y=x/10 will be what ? I am not able to understand this.

what do u expect y to be when x is 3.
y=x/10 = 3/10 = 0. thats it. Hence after printing z (which is now 3).

OK I thought u wrote while(x>0). I ignored that line. Sorry for the misunderstanding.
If it were a while loop then next time x becomes zero and the loop breaks.
BTW I dont understand why u have used if(x>0) that wil consider for only one digit.
U must use a while loop instead.

y=x/10 = 3/10 = 0. thats it

I was not sure about this. You mean to say, that in C programming, division of two dissimilar whole numbers proceeds like this. I will be very grateful if you can throw more light on this or point to a helpful tutorial where I can understand this. I am beginining to learn C a bit. So far you have been very helpful.

Thanks

when u divide an integer by an integer u get the result also an integer.
generally when u do 3/2 it should have been 1.5 but since both are integer values u will get the result as 1 not 1.5.
If u want 1.5 u have to typecast either of the operands to float.

dkalita's solution is perfect!
I get it.

By using this logic you can reverse only integer typed string...

int reverse(int num)
{
   int rev = 0;
   while(num>0)
   {
       rev = rev*10 + num%10;
       num = num/10;
   }
   return rev;
}

Now suppose I want to reverse this string "dkalita" ...
then we can't use this logic...then it will be better to use "strrev()" function...

when u divide an integer by an integer u get the result also an integer.
generally when u do 3/2 it should have been 1.5 but since both are integer values u will get the result as 1 not 1.5.
If u want 1.5 u have to typecast either of the operands to float.

Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }

I am not ignoring dkalita's solution..
ya, its right and perfect....
but Mr tojan you just try to use this logic in char string "tojan"...
obiasly it will not work...
dkalita had ignored me to use "strlen()" function and told that his given solution is best ....
so, i mean to say only that dkalita's logic will work for only integer numbers...
and our C compilers are giving use ready mate functions to make better and easy programing then why we can't use this..???
hope you got it.....!!!

u code is not correct also.

7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }

This part pick-up every digit from an integer,so it is enough to complete the question.
printf("%d", x) is redundant . Think it over.

I am not ignoring dkalita's solution..
ya, its right and perfect....
but Mr tojan you just try to use this logic in char string "tojan"...
obiasly it will not work...
dkalita had ignored me to use "strlen()" function and told that his given solution is best ....
so, i mean to say only that dkalita's logic will work for only integer numbers...
and our C compilers are giving use ready mate functions to make better and easy programing then why we can't use this..???
hope you got it.....!!!

yep buddy.
You are right.
Its good to use ready made stuffs.
But when it comes to efficiency and all then sometimes its better to build some modules yourself.
That will improve your programming skills.
You will get to learn a lot.
When you feel that you have learned a lot, you will know when to use ready made stuffs and when not to.
It just depends on the requirement soimetimes.

EDIT:

The solution I gave is a generalized one which u can implement in any programming language where there might not be a library method for that.
N.B. And ofcourse my solution is only for integer.

Thanks to nirav99.
:-) I think ill.

Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }

y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);

in the above code,

y=3/10=0 thats o.k but

what is 3%10 ?

how will z becomes 3 as explained by dkalita

Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }

Hope u got your answer from Tojan Wong's reply.

hello, Mr Tojan..
i had run this program okay..
its works perfectly...
Better that you also run it....
don't use your mind if you not having enough thinking level...
so, first run this program then make your arguments okay...

y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);

in the above code,

y=3/10=0 thats o.k but

what is 3%10 ?

how will z becomes 3 as explained by dkalita

It would be better if you read about arithmatic operations in C first.

Sorry Mr tojan...
If i had used some bad words to you...
i thought your r still ignoring me..
any way sorry and hope you got it my logic...

I accept u criticism, and nothing wrong with your words.
I learn something from both of u.

thank you guys for your help..

#include<stdio.h>
#include<conio.h>
#include<math.h>
 void main()
{
 int x,y,z;
scanf("%d",&x);
while(x>0)
 {
 y=x/10;
 z=x%10;
 x=y;
 printf("%d",z);
 }
 printf("%d",x);
 getch();
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.