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
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
Explanation
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
Explanation
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
Suggested post
Similar post
Reversed string in Java language
Reversed string in C++ language
C program to reverse a number using loops
Java program to reverse a number using loops
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
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)