Reverse Number
Need to design a program wherein when the human enters an integer, the computer should return an integer in the reversed manner. Write a program to calculate the reverse of the given number.
Note: Take care of the leading zeros (e.g. 5200 gives 25). It means reversed number never has any trailing zeros.
Sample Input:
4235
Sample Output:
5324
Answers
import java.util.Scanner;
public class a
{
public static void main (String args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number");
int num=sc.nextInt();
int rem=0, sum=0;
do
{
rem=num%10;
sum=sum+rem×10;
num=num/10;
while(num!=0);
}
System.out.println("The number is"+sum);
}
}
The program will work don't worry. Be Happy
Answer:
1. Program in C Language
#include <stdio.h>
int main() {
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number of integer = %d", reverse);
return 0;
}
2. Program in C++ language
#include <iostream>
using namespace std;
int main()
{
int n, reverse = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}
cout << "Reversed Number of integer = " << reverse;
return 0;
}
3. Program in Java language
import java.util.*;
public class Main {
public static void main(String[] args) {
int num , reverse = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer: ");
num = scan.nextInt();
while(num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
System.out.println("Reverse Number of integer = " + reverse);
}
}
4. Program in Python language
n=int(input("Enter an integer: "))
reverse=0
while(n>0):
dig=n%10
reverse=reverse*10+dig
n=n//10
print("Reverse number of integer:",reverse)
Note=> Indentation is necessary in Python language.