Problem 18

author
By Supreme2023-03-15
Problem Statement :
Write a program to reverse a number
Code:

#include <stdio.h>

int main(){


//18. Write a program to reverse a number.

int n, reverse=0 ,remainder;

printf("\tReverse A Number");
printf("\n========================================");

printf("\nEnter The Number : ");
scanf("%d",&n);

while(n != 0){
remainder = n%10;
reverse = (reverse*10) + remainder;
n /= 10;
}

printf("\n__________________________________________");
printf("\n\nReversed Number Is : %d",reverse);
printf("\n\n__________________________________________\n\n");


return 0;
}

Description :
  • This is a C program that reverses a given integer number. The program prompts the user to input a number and then uses a while loop to reverse it. The variables reverse, remainder, and n are declared and initialized to 0. In each iteration of the loop, the program calculates the remainder of the number when divided by 10, and then updates the reverse variable by multiplying it by 10 and adding the remainder. The program then divides the original number n by 10 and continues the loop until n becomes 0. Finally, the program prints the reversed number to the console along with a message. The program ends by returning 0 to indicate that it has executed successfully.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.