PROGRAM TO REVERSE A FIVE DIGIT NUMBER

C, C++, etc Programing examples. Post as many you can. Test the programs and discuss about the output of the programes.
Forum rules
Please Discuss & post only about the programes and its Output

PROGRAM TO REVERSE A FIVE DIGIT NUMBER

Postby praveen » Sun Feb 22, 2009 7:29 pm

/* PROGRAM TO REVERSE A FIVE DIGIT NUMBER */
Code: Select all
#include<stdio.h>
#include<conio.h>
main()
{
long int n;
int a;
int rev=0;
printf("Enter the 5 digit number to be reversed\n");
scanf("%d",&n);

a=n%10;
rev=a*10000;
n/=10;     /* i.e, n=n/10 */

a=n%10;
rev+=a*1000; /* i.e, rev=rev+(a*1000) */
n/=10;     /* i.e, n=n/10 */


a=n%10;
rev+=a*100; /* i.e, rev=rev+(a*100) */
n/=10;     /* i.e, n=n/10 */

a=n%10;
rev+=a*10; /* i.e, rev=rev+(a*10) */
n/=10;     /* i.e, n=n/10 */

a=n%10;
rev+=a*1; /* i.e, rev=rev+(a*1) */
n/=10;     /* i.e, n=n/10 */

printf("The reversed number is %d\n",rev);
//printf("Test");
getch();
}
Regards,
Praveen
AMIE Study

ImageImage
-------------------------------------------------------------------------------------------
Work from Home | AMIE Online Coaching
User avatar
praveen
Site Admin
 
Posts: 989
Joined: Thu Oct 23, 2008 1:42 pm
Location: Mumbai
membership: Senior Technician of The Institution of Engineers (India)- ST

ad
 

Re: PROGRAM TO REVERSE A FIVE DIGIT NUMBER

Postby irfanmasiullah » Tue Apr 13, 2010 9:16 am

Hello friend
find below my program for same function in C++, please comment on it, advice me also.
specially I would like comments from Mr. Praveen.


/* This is a program to reverse any number (digit is not limitation) reverse numbers */

/* n= Given number
r= reverse output
d=constant
*/

#include <iostream>
#include <cmath>

int main ()
{
using namespace std;
int r=0,d,n;
cout << "Enter a value:";
cin >> n;
do
{
d=n%10;
n= n/10;
r=r*10+d;
}
while(n!=0);
{
cout << r << "\n" ;
system ("pause");
return 0;
}

}
Thanks & Best Regards -
Irfan M Ansari - Section B - Civil
MOB- 00966 561 770 321 - Dammam - Saudi Arabia
Email :irfanmasiullah@gmail.com
IM: irfanmasiullah@yahoo.com : irfanmasiullah@gmail.com
User avatar
irfanmasiullah
Super Member
Super Member
 
Posts: 77
Joined: Thu Jul 30, 2009 7:59 pm
Location: Mumbai / Dammam (Saudi Arabia)
membership: Senior Technician of The Institution of Engineers (India)- ST


Return to Computer Notes : Some Sample Programs

Who is online

Users browsing this forum: No registered users and 0 guests