Program to Find the reverse of a given number

Problem statement:- Program to Find the reverse of a given number.

Sample Input/Output:-

Sample Input First: 564646                      Sample Output First: 646465

Sample Input Second: 7894664               Sample Output Second4664987

Data requirement:-

   Input Data:- num

  Output Data:-num2

  Additional Data:-rem

Program in C
  
Here is the source code of the C Program to find the reverse of a given number.

Code:

#include<stdio.h>
int main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
    int num2=0;
    while(num!=0)
    {
        int rem=num%10;
        num/=10;
        num2=num2*10+rem;
    }
    printf("The reverse of the number is %d",num2);
}

Input/Output:
Enter a number:564646
The reverse of the number is 646465

Program in C++
  
Here is the source code of the C++ Program to find the reverse of a given number.

Code:

#include<iostream>
using namespace std;

int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;
    int num2=0;
    while(num!=0)
    {
        int rem=num%10;
        num/=10;
        num2=num2*10+rem;
    }
    cout<<"The reverse of the number is "<<num2;
}

Input/Output:
Enter a number:7894664
The reverse of the number is 4664987

Program in Java
  
Here is the source code of the Java Program to find the reverse of a given number.

Code:

import java.util.Scanner;
public class ReverseOfNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
    System.out.println("Enter a number:");
    num=cs.nextInt();
    int num2=0;
    while(num!=0)
    {
        int rem=num%10;
        num/=10;
        num2=num2*10+rem;
    }
    System.out.println("The reverse of the number is "+num2);
cs.close();
}
}}

Input/Output:
Enter a number:
7894
The reverse of the number is 4987

Program in Python
  
Here is the source code of the Python Program to find the reverse of a given number.

Code:

num=int(input("Enter a number:"))
num2=0
while(num!=0):
   rem=num%10
   num=int(num/10)
   num2=num2*10+rem
print("The reverse of the number is",num2)

Input/Output:
Enter a number:4589
The reverse of the number is 9854

C/C++/Java/Python Practice Question






Post a Comment

0 Comments