Description
To reverse a number Step 1: Take a variable sum assign it to 0. Step 2: Follow step 3 to step 5 until number is greater than 0. step 3: Find modulus(remainder). step 4: Multiply sum to 10 and add remainder to it and assign to sum variable. step 5: Divide number by 10 and assign to number variable. step 6: Exit.
C/C++
/* C Program to reverse a number */ //Save it as ReverseNumber.c #include<stdio.h> int main(){ int number, sum=0, remainder, temp; printf("Enter a number : "); scanf("%d",&number); //Assign number of temporary variable to retain it's value temp = number; while(number > 0){ remainder = number % 10; sum = ((sum * 10) + remainder); number /= 10; } //assign the value of temp variable to number variable number = temp; printf("The reverse of %d is %d",number,sum); return 0; }
Input: Enter a number : 5249 Output: The reverse of 5249 is 9425
Java
/* Java Program to reverse a number */ //Save it as ReverseNumber.java import java.io.*; import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number, sum=0, remainder, temp; System.out.println("Enter a number : "); number = scanner.nextInt(); //Assign value of number variable to temporary variable to retain it's value temp = number; while(number > 0){ remainder = number % 10; sum = ((sum * 10) + remainder); number /= 10; } //assign the value of temp variable to number variable number = temp; System.out.println("The reverse of "+number+" is "+sum); } }
Input: Enter a number : 5249 Output: The reverse of 5249 is 9425
Related Programs
1) Program to find sum of digits of a number2) Program to find sum of first and last digit of a number
3) Program to find factors of a Number
4) Program to Swap Two Numbers
5) Program to calculate Factorial of a Number
6) Program to find largest and smallest digit in a Number
7) Program for finding Second Largest Element Of Array
8) Program to Reverse a String
9) Program to copy string
10) Count Number of Words in a String