Reverse any number using c program




Code 1:
1. Write a c program to reverse a given number
2. C program to find reverse of a number
3. C program to reverse the digits of a number
4. Reverse of a number in c using while loop

#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 12
Reversed of number: 21

Code 2:
1. Reverse very large or big numbers beyond the range of long int
2. Reverse five digit number c program

Logic is we accept the number as string

#include<stdio.h>
#define MAX 1000

int main(){

    char num[MAX];
    int i=0,j,flag=0;

    printf("Enter any positive integer: ");
    scanf("%s",num);

    while(num[i]){
         if(num[i] < 48 || num[i] > 57){
             printf("Invalid integer number");
             return 0;
         }
         i++;
    }

    printf("Reverse: ");
    for(j=i-1;j>=0;j--)
         if(flag==0 &&  num[j] ==48){
         }
         else{
             printf("%c",num[j]);
             flag =1;
         }

    return 0;

Sample output:

Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432

Code 3:
1. C program to reverse a number using for loop
2. How to find reverse of a number in c
3. Wap to reverse a number in c

#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 123
Reversed of number: 321

Code 4:
1. C program to reverse a number using recursion

#include<stdio.h>
int main(){
    int num,reverse;

    printf("Enter any number: ");
    scanf("%d",&num);

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}

int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;

    return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654


59 comments:

Ghanshyam said...

better.........

Anonymous said...

its helping me a lost

adin said...

u can avoid recursion by directly writing this code

while(num!=0)
{
r=num%10;
sum=sum *10 + r
num=num/10;
}

Raj said...

take num=12
now r=12%10=2
sum=0*10+2=2
num=12/10=1
now num becomes 1
r=1%10=1
sum=2*10+1=21
num=1/10=0 so now 0>0 does not satisfy
printf("%d",sum)
so reverse of 12 is 21

Anonymous said...

@raj this logic only possible for upto 9nums

kapil said...

/*i think i hv a b8r code.....*/

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

Anonymous said...

kapil's prog is great and easy

Anonymous said...

it is good but not perfect

sangeerth said...

yours s good but
in some case ther will some need to hav zero in 1st digit
like 1230 we need to get 0321
yours cant do that,..???

Ritesh kumar said...

Hi sangeerth,
In code 2 instead of
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
printf("%c",num[j]);
flag =1;
}

Write only
for(j=i-1;j>=0;j--)
printf("%c",num[j]);

alida said...

These programs to reverse a number are not workin in the case of numbers which ends with zeor . Please help. for example if we input 320 the reverse shows 23 only not zero at the beginning.

Anonymous said...

@alida: because the reverse of 320 is 023 and the zero at the front of a whole number is ignored because it is not significant.

Anonymous said...

@alida: because the reverse of 320 is 023 and the zero at the front of a whole number is ignored because it is not significant.

Swamy K said...

wow wonderful..............dennis ritche is very happy in heaven

Kshirod Hati said...

/*U can write a 'c' program to search details of a person (simple program)*/





#include
#include
#include
void main()
char n;
int p==2429;
printf("Enter a name);
scanf("%s",n);
printf("Password please");
if(n==khirod&&p==2429)
{
printf("details of khirod");
}
if(n==giri&&p==2429)
{
printf("details of giri");
}
{
.
.
.
.
.
}
getch();
}

Unknown said...

hay frnz any body tell me which one video tutorial cd(disk) is best . where i will purchase it

idris zariwala said...

write a C program to calculate area and perimeter of circle according to user choice by using switch statement.
give me the answer as soon as possible
thank you

ritesh kumar said...

These links may help you:
C PROGRAM TO CALCULATE AREA OF A CIRCLE
Write a c program to find the perimeter of a circle

Anonymous said...

: Write a program that prints the following pattern.
*
* *
* * *
* * * *
* * * * *
* * * * * *


Anonymous said...

i am needs this programme.i hope you will help me......

Anonymous said...

Write a program which adds two arrays with the help of their pointers.

Anonymous said...

Using your method if u will calculate the reverse of 10 or 100 or 1000 and so on ...then it will return 1 and 1 and 1 respectively so on .it can not show 01 , 001 , 0001 as reverse.

Anonymous said...

thanks but can you logics for using this code i.e:

r=no%10;
no=no/10;
rno=rno*10+r;

akanksha varshney said...

if(num[i] < 48 || num[i] > 57)......
plz explain this condition ......

ISHIMWE MARTIN said...

some programs have errors

Sajal said...

#include
int main()
{
int row,i,j;
printf("Enter the row:");
scanf("%d",&row);
for(i=1;i<=row;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
getch();
return 0;
}

Anand Barnwal said...

Program to reverse a very large number.

#include
#include
main()
{
char str[100];
int i, j;
scanf("%s",str);
for(i = strlen(str)-1; i >= 0; i--)
printf("%c",str[i]);
}

Anand Barnwal said...
This comment has been removed by the author.
Anand Barnwal said...

num[i] will return ASCII value of that character. ASCII value of '0' is 48 and '9' is 57.

akshayks.ks said...

devlop an algorithm to find reverse of the given number.

aditi said...

why the 1 code is not reversing the number 12345 to 54321...??????????

aditi said...

it should revrse all the numbers less than 32767 which is int max value..

Anonymous said...

this program won't work for 000002 as it's reverse shall be 200000 but the answer comes out to be 2.... please someone help me with this....

Anonymous said...

your program is so easy to understand. thanks kapil bhai

Anonymous said...

why need of flag?? and why to take char ??cant we do it for large numbers..widout char??

Anonymous said...

*
***
*****
*******
*********
pls giv me the code for thiz...

Priyanka Singhal said...

void main()
{
int r,c;
for(r=0;r<5;r++)//for row
{
for(c=0;c<=r*2;c++)
{
printf("*");
}
printf("\n");
}
}

Ganesh Kumar said...

very good

Anonymous said...

Zero at the beginning of a number has no value anyways.

Srinath Santhi said...

#include
#include
void main()
{
int i;
for(i=10;i>=1;i--)
{
printf("%d\n",i);
}
getch();
}

Srinath Santhi said...

put............... #include
#include

balaji venkatesan96 said...

super kapil urs program was G.R.E.A.T

balaji venkatesan96 said...

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


this prog print 000002 as output while 200000 as input and this was also explained by Kapil

Affity Solutions said...

Thanks for your ideas. You can also find the details on Affity Solutions, at the C Developers. The main object of the Affity Solutions is to provide quality web services and is among the few software development company in Nagpur.

ramesh said...
This comment has been removed by the author.
Raghavendra yemul said...

import java.util.Scanner;
public class Reverse
{
public static void main(String args[])
{
int a,b,c,d=0;
System.out.println("Enter the number");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
while(a!=0)
{
b=a%10;
c=a/10;
d=d*10+b;

a=c;
}
System.out.println(d);
}

}

Harsha Roy said...

awesome site...:)
it really helping me alot....and yes,i want such good sites for java programs...plzz guys help me...

Allam bhaskar reddy said...

Why we use n% 10 why not n% 100 in reverse of a number

Jahirul Islam Monir said...

Hello frnds I need a code....Count the number of words in a line using array...For exm..Word=space+1;

Nur Haque said...

Very nice

Nur Haque said...

Very nice

Omi Yadav said...

No need of variable c, you can write a = a/10;

sneha agarwal said...

how can i print 0 in the beginning and t the last because this program does not post value 0

Kayal Vizhi said...

very helpful and useful post you have shared.


web design and development company in chennai

Unknown said...

hii priyanka
can u plz tell me how to print reverse num of this value (1000 or 0001)

sudarsana said...

Good idea using while loop

Karan Jani said...

/* This is the program to reverse the input number without using any looping or functioning and recursive Technique*/

#include
main()
{
int a,a1,a2,a3;
int b=0,b1,b2,b3,b4;
int no;
int new1,new2,new3,new4;
printf("Enter a Five Digit No: "); //U have to ennter a five digit Number
scanf("%d",&no); //entered number will be saved at the location of variable no
//For example the entered number is 12345
a=no%10; //here after this modulo operation a=5
b=b*10+a; // After this arithmetic operation b=5
new1=no/10; // after this new1=1234
a1=new1%10; // a1=4
b1=b*10+a1; // b1=5*10+4=54
new2=new1/10; //new2=123
a2=new2%10; //a2=3
b2=b1*10+a2; //b2=54*10+3=543
new3=new2/10; // new3=12
a3=new3%10; //a3=2
b3=b2*10+a3; //b3=543*10+2=5432
new4=new3/10; //new4=1
b4=b3*10+new4; //b4=5432*10+1=54321
printf("The Reverse of the entered no is : %d",b4);

//SO the number is reversed here finally
}

Unknown said...

Void main()
{
int i,j,n=5;
For(j=1;j<=n;j++)
{
For(i=1;i<=2*j-1;i++);
{
Printf("*");
}
Printf("\n");
}
}

Nishant Rathod said...

Void main()
{
int i,j,n=5;
For(j=1;j<=n;j++)
{
For(i=1;i<=2*j-1;i++);
{
Printf("*");
}
Printf("\n");
}
}