Find elements

C++ program to reverse a number using loops

C++ program to reverse a number using loops

In this article, we will discuss the concept of the C++ program to reverse a number using loops

In this post, we are going to learn how to find reverse number of the given number in C++ programming language

Reversed number

Cpp code to reverse a number using for loop

Program 1

The program allows  the user to enter a number and it displays the reverse pattern of the given number using for loop in C++ language

when you are entered 56789,
The output will be displayed as 98765 using the program

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int number,reversed_num=0;
    cout << "Enter a number for find reverse" << endl;

    cin>>number; //Taking a number as an input and stores number variable
    cout << "you entered: "<<number;
    for(; number!=0;){
        reversed_num=reversed_num*10;
       reversed_num=reversed_num+number%10;
       number=number/10;//updating statements
    }
    cout << "\nReversed number is: "<<reversed_num;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Program 1

Explanation

  • Declare and initialize two variables as follows int number,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘number
  • The for loop is used to find the reversed number of the given number
  • The for loop is functioning until ‘ number‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

Cpp code to reverse a number using while loop

Program 2

The program allows  the user to enter a number and it displays the reverse pattern of the given number using while loop in C++ language

when you are entered 234567,
The output will be displayed as 765432 using the program

 

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int number,reversed_num=0;
    cout << "Enter a number for find reverse" << endl;

    cin>>number; //Taking a number as an input and stores number variable
    cout << "you entered: "<<number;
    while(number!=0){
        reversed_num=reversed_num*10;
       reversed_num=reversed_num+number%10;
       number=number/10;
    }
    cout << "\nReversed number is: "<<reversed_num;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Program 2

 

 

Explanation

  • Declare and initialize two variables as follows int number,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘number
  • The while loop is used to find the reversed number of the given number
  • The while loop is functioning until ‘ number‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

 

Cpp code to reverse a number using do-while loop

Program 3

The program allows  the user to enter a number and it displays the reverse pattern of the given number using do-while loop in C++ language

when you are entered 654321,
The output will be displayed as 123456 using the program

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int number,reversed_num=0;
    cout << "Enter a number for find reverse" << endl;

    cin>>number; //Taking a number as an input and stores number variable
    cout << "you entered: "<<number;
    do{
        reversed_num=reversed_num*10;
       reversed_num=reversed_num+number%10;
       number=number/10;
    }while(number!=0);
    cout << "\nReversed number is: "<<reversed_num;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Explanation

  • Declare and initialize two variables as int number,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘number
  • The do-while loop is used to find the reversed number of the given number
  • The do-while loop is functioning until ‘ number‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

 

Suggested post

For loop in C++ language

While loop in C++ language

Do-while loop in C++ language

 

Similar post

Reversed string in Java language

Reversed string in C language

Reversed string in C++ language

C program to reverse a number using loops

Java program to reverse a number using loops

 

Java program to reverse a number using loops
C program to reverse a number using loops
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Can you reverse numbers such as Input= 123 456 0 Output = 321 654. Input must end with 0(zero). Output shouldn't show 0(zero)

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago