Reverse Number Program in C
0 1027
This C program reverses an integer entered by the user.
It uses a while loop to extract each digit from the input number, appending them in reverse order to form the reversed number.
Input: Prompt the user to enter an integer using printf and read it with scanf. Reverse the Number:
Iterate using a while loop: Calculate the remainder as the last digit of the number. Update the reversed number by appending the remainder. Reduce the number by one digit by dividing it by 10.
Output: Display the reversed number using printf. Example:
Algorithm Steps
Declare variables: number: to store the input number. reversed number: to store the reversed number. remainder: to capture the last digit of the number.Input: Prompt the user to enter an integer using printf and read it with scanf. Reverse the Number:
Iterate using a while loop: Calculate the remainder as the last digit of the number. Update the reversed number by appending the remainder. Reduce the number by one digit by dividing it by 10.
Output: Display the reversed number using printf. Example:
// program for reversed Number in C #include<stdio.h>Output:int main() { int number, reversedNumber = 0, remainder; // Input printf("Enter an integer: "); scanf("%d", &number); // Reverse the number while (number != 0) { remainder = number % 10; // Extract the last digit reversedNumber = reversedNumber * 10 + remainder; // Append the digit number /= 10; // Remove the last digit } // Output printf("Reversed number: %d\n", reversedNumber); return 0; }
Reversed number: 987Execution: If the user enters 7890, the program will output: Reversed number: 987 This confirms that the number 7890 has been reversed to 987.
Share:




Comments
Waiting for your comments