First of all. Step by step working of the above C program: So,n=n/10 i.e n=1/10=0. In the above program, the number that needs to be reversed is 63972. To understand this example, you should have the knowledge of the following C++ programming topics: So, n=n/10 i.e 145/10=14. All Rights Reserved. Reverse of a Number using For loop in C++, Reverse of a Number using while loop in C, Reverse of a Number using while loop in C++, Reverse of a Number using do-while loop in C, Reverse of a Number using do-while loop in C++. 3rd Iteration: from the second iteration the value of n=14 and reverse=65. Finally the reverse of a given number is printed. The above program does not consider leading zeros after a number eg. Logic to find reverse of a number in C programming. Your email address will not be published. i>0 (4321>0) for loop condition is true. (adsbygoogle = window.adsbygoogle || []).push({}); Your email address will not be published. Input a number from user to find reverse. In this program, we will use for loop to reverse a number entered by the user. Finally the reverse of a given number is printed. Required fields are marked *. Reverse a Number using For Loop in C Programming #include int main() { int num, sum = 0, … 1st Iteration: rem=n%10 i.e rem=1456%10=6 and rev=rev*10+rem i.e rev=0*10+6=0+6=6.So, n=n/10 i.e 1456/10=145. C++ Program to Reverse a Number Using While Loop In this program, the compiler will ask the user to enter the number which the user wants to reverse a number. So, n=n/10=14/10=1. C++ Program to Reverse a Number Using For Loop, Linear Search Program in C++ With Explanation, Insertion Sort Program in C++ – [Algorithm With Explanation]. After reversing the number the compiler will print output on the screen. 1. AddressPuloly South,pointpedroJaffna, Srilanka, HoursMonday—Friday: 9:00AM–5:00PMSaturday & Sunday: 11:00AM–3:00PM, C program to find factorial using function, C function to check a number is even or odd, C program: Find the frequency of each character in the string, C++ program to reverse a number using loops, Cpp code to reverse a number using for loop, Cpp code to reverse a number using while loop, Cpp code to reverse a number using do-while loop. First the computer reads a number from the user. 4th Iteration: from the third iteration the value of n=1 and rev=654. This program takes an integer input from the user. Example. First the computer reads a number from the user. It comes out of the for loop and prints the reverse number which is stored in variable “s”. rem=n%10 i.e rem=1%10=1 and rev=rev*10+rem i.e rev=654*10+1=6540+1=6541. Step by step working of the above C++ program: Your email address will not be published. rem=n%10 i.e rem=14%10=4 and the rev=rev*10+rem i.e rev=65*10+4=650+4=654. Then using for loop the reverse number is calculated and stored in the variable ‘s’. It is stored in the variable num. There are many methods to reverse a number in C++ we will see it one by one. Time Complexity of the above program is O(log(n)). Finally the reverse of a given number is printed. Store it in … In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by 10 times. Inside the loop, the reversed number is computed using: rev = rev*10 + remainder; Then using for loop the reverse number is calculated and stored in the variable ‘s’. Basic C programming, While loop. Step by step descriptive logic to find reverse of a number. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. First the computer reads a number from the user. Reverse of number means reverse the position of all digits of any number. How to find reverse of any number using loop in C program. CodingConnect.net © 2018. Then the while loop is used until n != 0 is false (0). For Example 12345 and it’s reverse is 54321. 500 program will consider as 5. Your email address will not be published. Input. Let us consider user enter the number 1456. For example reverse of 251 is 152. Reversing a number means changing all the positions of the digit of a number. Logic to reverse a Number Without Using Loop, 3. 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 … Here is source code of the C++ Program to Reverse a Number using while loop. consider the number 123.Now perform 123%10 = 3 = aperform 123/10 =12if not 0,then multiply a with 10, so a = 30Now perform 12%10 = bperform 12/10 = 1if not 0, then multiply b with 10 & a with 10,so a = 300 & b = 20perform 1%10 = cperform 1/10 = 0if not 0, continue….however, as this becomes 0, you can stop the process.just add a+b+c. Reverse of 12345 = 54321. 1. The output will be displayed as 98765 using the program, When the above code is executed, it produces the following result, 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, Only the loop has been changed but, the logic behind the program is the same. 2nd Iteration: from the first iteration the value of n=145 and rev=6. Required knowledge. rem=n%10 i.e rem=145%10=5 and rev=rev*10+rem i.e rev=6*10+5=60+5=65. Required fields are marked *, C++ Program to find Prime Number or Not using While Loop, Android Application that implements Multi threading, Android Application that creates Alarm Clock, Android Application that uses GUI components, Font and Colors, Simple Android Application for Native Calculator, Factorial of a Number in C using do-while Loop, C++ program for Palindrome Number using While Loop, Simple Android Application that makes use of Database. In this program, the compiler will ask the user to enter the number which the user wants to reverse a number.