Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
C program to reverse a number with explanation
Ans:
In this tutorial, we are going to learn how to write a program to reverse the digits of a given number using a C programming language.
So Let’s Begin
Before directly moving on writing reverse program in C, first you should understand what logic we are going to use to achieve it.
Suppose if someone gives input 123, then our program should provide output 321.
The logic behind it is
First, we will find the last digit of input number, and then we will store it into a variable and remove it. Now again, we will see that the second last digit will become the last digit. So again we will pick the last digit of the number and then append it with the digit which we have calculated and stored previously in ‘reverse’ variable. We will follow the same approach for all digit of a number. Once we apply the same logic for each digit of number then at last ‘reverse’ variable will contain a reverse number.
Below we will see it with the help of the program
C program to reverse a number
#include <stdio.h>
int main()
{
int n=321,reverse=0;
printf("before reverse = %d\n",n);
while(n!=0){
reverse = reverse*10 + n%10;
n=n/10;
}
printf("after reverse = %d",reverse);
return 0;
}
Output:
Explanation of reverse program in c
- In the above program I have taken a static number 321 which is assigned in variable ‘n’. Here I am not taking the value from users.
- We have one more integer variable ‘reverse’ that will store value each time when we find last digit of the number.
- We can say that ‘n’ is storing original value and ‘reverse’ variable will store the value after reverse.
- Now using while loop here we are checking that value of ‘n’ should not be 0. If already zero then we can’t perform any operation on it.
- And when ‘n’ is not zero, in body of while loop we are finding last digit of the number and adding it to the same number by multiplying 10 and then storing it in reverse variable.
- And at last dividing original number by 10 to remove the last digit of the number. As we already collected it in reverse variable.
I hope you have understand it well.
Thank you.
Also Prepare C interview programs given below
C Questions on number
- Write a program to reverse an integer in C.
- Write a program in C to check whether an integer is Armstrong number or not.
- Write a program in C to print the fibonacci series using recursive method.
- Write a program in C to check whether a number is palindrome or not using recursive method.
- Write a program in C to add two integer without using arithmetic + operator.
C Questions on String
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program