If a five-digit number is input through the keyboard, write a program to reverse the number.

/*Use an integer variable to store the reverse (say rev_num).

take out the first (rightmost) digit of the number.

Give it to rev_num.

Take out the 2nd digit from right.

Now multiply rev_num by 10 and add this new digit to it.

Do this till the last digit.

*/

/*To understand the program, assume that the five digit number -- 12345 is entered

Calculate the values manually and check against the given number.

The final result (reversed number) is going to be 54321*/

**#include<stdio.h>**

main()

**{**

**int**number, rev_num, next_digit,last_digit;

printf ("Enter the number that is to be reversed: ");

scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);

**}**

## 19 comments:

the explaination of the problem given here is very good and can be easily understood by begginers......

it's not giving proper result...When I am giving input 12345 it's giving -11215...please help me regarding this...

The program is absolutely wonderful to understand and execute

Nice Explanation...........

very nice ....its easy to understand

its ease to understand

Its really wonderful to understand...

Try This

main( )

{

int x,result,next;

printf("Enter The number to revers=");

scanf("%d",&x);

//5th

result=x%10;

result=result*10;

//4th

next=(x/10)%10;

result=(result+next)*10;

//3rd

next=(x/100)%10;

result=(result+next)*10;

//2nd

next=(x/1000)%10;

result=(result+next)*10;

//1st

next=(x/10000)%10;

result=result+next;

printf("%d",result);

}

Both answers are totally wrong first check then publish

The best way of making this program is by using the while or for loop.its the shortest way of doing it

#include

int main(){

int num,d1,d2,d3,d4,d5,renum,sum;

printf("Enter a five digit number:");

scanf("%d",&num);

d1=num/10000;

d2=(num%10000)/1000;

d3=(num%1000)/100;

d4=(num%100)/10;

d5=num%10;

renum=d5*10000+d4*1000+d3*100+d2*10+d1;

sum=d1+d2+d3+d4+d5;

printf("Reverse number:%d\n",renum);

printf("Sum:%d",sum);

getch();

}

in dev c++ you should get the correct output

in turbo c++ there is error

if you type 12345 output will come as 11215

whereas in dev c++ and same coding

output is correct that is 54321.

good explanation

#include

int main()

{

int num,a,b,c,d,e,aa,bb,cc,dd,ee;

printf("Enter the five digit number\n");

scanf("%d", &num);

e = num%10;

dd = num%100;

d = dd/10;

cc = num%1000;

c = cc/100;

bb = num%10000;

b = bb/1000;

aa = num%100000;

a = aa/10000;

printf("%d%d%d%d%d", e,d,c,b,a);

getchar();

getchar();

return 0;

}

this programs not properly execute.

try this ..........so simple and easy

for more help contact me at

timcook_apple@yahoo.com

#include

void main()

{

int long x,p,q,r,s,t;

clrscr();

printf("\nEnter a 5-digit number:\n\t:");

scanf("%ld",&x);

p=x/10000;

q=(x/1000)-(p*10);

r=x/100-(p*100)-(q*10);

s=(x/10)-(p*1000)-(q*100)-(r*10);

t=x-(p*10000)-(q*1000)-(r*100)-(s*10);

printf("The reerse of given No:\n\t: %ld %ld %ld %ld %ld",t,s,r,q,p);

getche();

}

I did it this way...

main()

{

int number, last_digit, prev_digit, total;

printf("Enter the number to be written in reverse: ");

scanf("%d", &number);

last_digit=number%10;

total=last_digit*10000;

prev_digit=(number/10)%10;

total=total+(prev_digit*1000);

prev_digit=(number/100)%10;

total=total+(prev_digit*100);

prev_digit=(number/1000)%10;

total=total+(prev_digit*10);

prev_digit=(number/10000)%10;

total=total+prev_digit;

printf("\nThe reverse of the enetered number is %d", total);

getch();

}

#include

#include

#include

main()

{

int temp;

int rev=0;

cout<<"enter 5 digits to be reverse ";

cin>>temp;

for (int i=0;i<5;i++)

{

rev=(rev*10)+(temp%10);

temp/=10;

}

cout<<"reverse number is "<<rev;

getch();

}

Post a Comment