answersLogoWhite

0

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;

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

How to code a program to print 123 894 765 when input is 123 456 789?

#include <stdio.h> int main (void) { puts ("123 894 765"); return 0; }


Is 123 in Decimal System equal to 1111011 in binary system?

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


How do you separate digits in c plus plus?

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 ); }


C program to print 1 12 123 1234?

#include <stdio.h> int main (void) { puts ("1 22 333 4444 55555"); return 0; }


Write a shell script to print given number in reverse order in Unix?

# 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"