Write a program to reverse a given number

14 May 2022

Write a program to reverse a given number


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get an input from the user and the print the reverse of the given number as the output

E.g.  let the number be 324 then the reverse of the number is 423

Input

675

Output

576

C Program

#include <stdio.h>

int main ()

{

  int num, rev=0,rem;

  printf("Enter a number: ");

  scanf("%d",&num);

    while(num!=0)

    {

      rem=num%10;

      rev=rev*10+rem;

      num =num/10;

    }

  printf("%d",rev);

  return 0;

}

C++ Program

#include <iostream>

using namespace std;

int main ()

{

  int num, rev=0,rem;

  cout<<"Enter a number: ";

  cin>>num;

    while(num!=0)

    {

      rem=num%10;

      rev=rev*10+rem;

      num =num/10;

    }

  cout<<rev;

  return 0;

}

Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

        System.out.print("Enter a number: ");

        int num=sc.nextInt();

        int rev = 0;

        while(num!=0)

        {

            int rem=num%10;

            rev=rev*10+rem;

            num=num/10;

        }

        System.out.print(rev);

}

}

Python

num=int(input("Enter the Number:"))

rev=0

while num>0:

    rem=num%10

    rev=(rev*10)+rem

    num = num // 10

print(rev)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !