Program to reverse ANY given number in C
#include
main()
{
int num,mod,rev=0;
printf("Enter a number:");
scanf("%d", &num);
while(num>0)
{
mod=num%10;
rev=(rev*10)+mod;
num=num/10;
}
printf("Reverse of the given number: %d", rev);
getchar();
}
Other Ideas! Use the While Loop
#include
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();
}
Before compiling include stdio.h
-----------------------------------------------------------------------------
Using sprintf/atoi functions./..
int _tmain(int argc, _TCHAR* argv[])
{
int inumber;
printf("\n\n Enter a Number:");
scanf("%d",&inumber);
char pNumber[10];
sprintf(pNumber, "%d", inumber);
int iLength=strlen(pNumber);
char temp;
for(int i=0;i temp=pNumber[i]; pNumber[i]=pNumber[iLength-1]; pNumber[iLength-1]=temp; iLength--; } inumber=atoi(pNumber); printf("\n\nReverse no is %d",inumber); getchar(); return 0; }
#include<stdio.h>
#include<conio.h>
void main()
{
int abcd;
printf("enter a 4-digit no.");
scanf("%d",&abcd);
a=abcd/1000;
abcd=abcd%1000;
b=abcd/100;
abcd=abcd%100;
c=abcd/1o;
abcd=abcd%10;
d=abcd;
printf("reverse=%d",dcba);
getch();
}
h = x/100; // (1) x = 325, h = 3 (2) x = 1, h = 0
u = x%10; // (1) u = 5 (2) u = 1
y = x - 99* (h - u); // (1) y = 325 - (-198) = 523 (2) y = 1 - (-99) = 100
OK? Comments show two examples, 325 to 523 and 1 to 100.
h=num%100 u=h%10
g=num/100
i=h/10
numreverse=u*100+i*10+g
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter number:\t);
scanf("%d",a);
printf("Reversal of number: ");
do {
b=a%10;
printf("%d",b);
a = a/10;
} while(a>0);
getch();
}
//Reverse of the Number
// Programme Written By Maulin Thaker Ahmedabad;
#include
#include
void main()
{
int a,b,c,d,e;
clrscr();
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;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,b;
long int revnum=0;
clrscr();
printf("\n Enter three digit number");
scanf("%d", &n);
a=n%10;
n=n/10;
revnum=revnum+a*100;
a=n%10;
n=n/10;
revnum=revnum+a*10;
a=n%10;
revnum=revnum+a;
printf("\nThe reversed number is %ld",revnum);
getch();
}
#include<srdio.h>
#include<conio.h>
void main()
{
int num,reverse;
clrscr();
printf("enter any number ==>")
scanf("%d",&num);
reverse=rev(num);
printf("reverse of number is==>%d",reverse);
getch();
}
int rev(int num)
{
static sum,r;
if(num)
{
r=num%10;
sum=sum*10+r;
rev(num%10);
}
else
return 0;
return sum;
}
#include<stdio.h>
#include<conio.h>
int main (void)
{
int n,s=0,d;
clrscr();
printf("\n Enter the no. which u want to reverse\n");
scanf("%d",&n);
while(n>0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
printf("\nThe reverse no. is=%d",s);
getch();
return 0;
}
int RevNum( int num )
{
const int base = 10;
int result = 0;
int remain = 0;
do
{
remain = num % base;
result *= base;
result += remain;
} while( num /= base);
return( result );
}
int main()
{
int n;
printf( "Enter a value:\t" );
scanf( "%d", &n );
printf( "Reversed:\t%d\n", RevNum( n ));
}
#include <stdio.h> int main (void) { puts ("123 894 765"); return 0; }
The rightmost digit represents how many 1s (in this example 1) 1 The next digit left represents how many 2s (in this example 1) 2 The next digit left represents how many 4s (in this example 0) 0 The next digit left represents how many 8s (in this example 1) 8 The next digit left represents how many 16s (in this example 1) 16 The next digit left represents how many 32s (in this example 1) 32 The next digit left represents how many 64s (in this example 1) 64 Total 123
Repeatedly divide the number by 10 and store the remainder (the modulo). By way of an example, if the number were 12345: 12345 % 10 = 5 (first digit) 12345 / 10 = 1234 1234 % 10 = 4 (second digit) 1234 / 10 = 123 123 % 10 = 3 (third digit) 123 / 10 = 12 12 % 10 = 2 (fourth digit) 12 / 10 = 1 (fifth digit) This algorithm forms the basis of number reversals. The following function demonstrates the most efficient way of reversing any number in the range -2,147,483,648 to 2,147,483,647, inclusive. int RevNum( int num ) { const int base = 10; int result = 0; int remain = 0; do { remain = num % base; result *= base; result += remain; } while( num /= base); return( result ); }
#include <stdio.h> int main (void) { puts ("1 22 333 4444 55555"); return 0; }
# Algo: # 1) Input number n # 2) Set rev=0, sd=0 # 3) Find single digit in sd as n % 10 it will give (left most digit) # 4) Construct revrse no as rev * 10 + sd # 5) Decrment n by 1 # 6) Is n is greater than zero, if yes goto step 3, otherwise next step # 7) Print rev # if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find reverse of given number" echo " For eg. $0 123, I will print 321" exit 1 fi n=$1 rev=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev \* 10 + $sd` n=`expr $n / 10` done echo "Reverse number is $rev"
With 123 digits you can make 123 one-digit numbers.
123
123
123
1+2+3=6 1x2x3=6 123
You can change it from 123 to 123/1. This is just how to write it on the computer usually. Otherwise, it will be: 123 1
1, 12, 23, 34, 45, 56, 67, 78, 89,101, 112, 123, 134, 145, 156, 167, 178, 189 and so on
123 is already in decimal form.
One good fact about the number 1089 is: pick a 3 digit number (they cannot be the same) i.e 123 reverse it 321 take the smallest amount from the largest 321-123=198 take the answer and reverse it 891 then add that number to the answer of the subtraction 891+198=1089 no matter what 3 numbers you use you will always end up with 1089 for the answer.
You write it as 123.
Just 6: 123 132 213 231 312 321
123%