Reverse Number Program in C

Advertisements

Prev Tutorial Next Tutorial

Reverse Number Program in C

Reverse of number means reverse the position of all digits of any number. For example reverse of 839 is 938. Before start writing this code learn basic concept of While Loop in C

reverse a number program in c

C Program to Reverse any Number Using While Loop

#include<stdio.h>
#include<conio.h>

void main()
{
int no,rev=0,r,a;
clrscr();
printf("Enter any num: ");
scanf("%d",&no);
a=no;
while(no>0)
{
 r=no%10;
 rev=rev*10+r;
 no=no/10;
}
printf("Reverse of %d is: %d",a,rev);
getch();
}

Output

Enter any num : 964
Reverse of 164 is 469

Explanation of program

Code

while(no>0)
{
 r=no%10;
 rev=rev*10+r;
 no=no/10;
}

In Above code we first check number is greater than zero, now find reminder of number and get last digits. After this step we place last digit at first position (at unit place) using "rev=rev*10+r". Again we divide number by 10 (no=no/10) and value are store in "no" variable. now we have a new number which have all digits except last digit. again check while loop and find remainder and get last digits. same process is repeated again and again until condition is true.

C Program to Reverse Number using for loop

#include<stdio.h>
#include<conio.h>

  void main()
{
int no,rev=0,r,a;
clrscr();
printf("Enter any num: ");
scanf("%d",&no);
a=no;
for(;no>0;)
{
 r=no%10;
 rev=rev*10+r;
 no=no/10;
}
printf("Reverse of %d is %d",a,rev);
getch();
}

Output

Enter any num : 164
Reverse of 164 is 461

Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements