➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
Write a C program to find the reverse of a number. For example, the reverse of number 7854 is given as 4587.
From the operators topic, we know that the modulus operator (%) can be used to find the last digit of the number. Similarly, the division operator (/) can be used to remove the last digit of the number.
Steps to reverse a number,
Declare a variable reverse, and initialize it with 0.
Step1:- Find the last digit of the number. lastDigit = number%10
Step2:- Append this lastDigit to the variable reverse. reverse = (reverse*10)+lastdigit;
Step3:- Remove the last digit of the number. number = number/10
Repeat this process (from step1 to step3) until the number becomes 0.
C Program to Find Reverse of a Number using while loop
#include<stdio.h>
int main()
{
int number, lastDigit, reverse=0;
printf("Enter a number: ");
scanf("%d",&number);
while( number!=0 )
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
number /= 10;
}
printf("Reverse Number = %d\n",reverse);
return 0;
}
Output:-
Enter a number: 12345
Reverse Number = 54321
Enter a number: 0032
Reverse Number = 23
Enter a number: 123000
Reverse Number = 321
In this program, the input number is stored in the variable number
. To reverse the number modulus and division operators are used, and the result is stored in the variable reverse
.
Another Way
This method is a shortcut way and it can’t be used with big programs. Thi method after finding the last digit, it is displayed to the screen without storing it. Each iteration display one digit. The below program displays the number in reverse. Don’t use this method in another program.
#include<stdio.h>
int main()
{
int number, lastDigit;
printf("Enter a number: ");
scanf("%d",&number);
while( number!=0 )
{
lastDigit = number%10;
//displaying within while loop
printf("%d",lastDigit);
number /= 10;
}
return 0;
}
Output for different test-cases:-
Enter a number: 852
258
Enter a number: 123000
000321
Enter a number: 000032
23
C Program to Find Reverse of a Number using the do-while loop
#include<stdio.h>
int main()
{
int number, lastDigit, reverse=0;
printf("Enter a number: ");
scanf("%d",&number);
do
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
number /= 10;
}while( number!=0 );
printf("Reverse Number = %d\n",reverse);
return 0;
}
C Program to Find Reverse of a Number using for loop
#include<stdio.h>
int main()
{
int number, lastDigit, reverse;
printf("Enter a number: ");
scanf("%d",&number);
for(reverse=0; number!=0; number /= 10)
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
}
printf("Reverse Number = %d\n",reverse);
return 0;
}
In this program reverse variable is declare before for loop becomes it is used outside of the for loop (in the last statement). If we declare the reverse variable at the time of initialization of for loop then the variable becomes local variable and can be accessed only within the for
loop. You can read more about it in Scope and lifetime of a variable.
Output for the different test-cases:-
Enter a number: 12345
Reverse Number = 54321
Enter a number: 00123
Reverse Number = 321
Enter a number: 2145000
Reverse Number = 5412
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!