Site icon Matistics

C-If a Five-digit Number is input through the keyboard, write a program to REVERSE the number.

#include <stdio.h>
int main() {
    int n,a,b,c,d,e;
    printf("Enter a five digit number ");
    scanf("%d",&n);
    a = n/10000;
    b = n/1000 - a*10 ;
    c = n/100 - a*100 - b*10 ;
    d = n/10 - a*1000 - b*100 - c*10 ;
    e = n - a*10000 - b*1000 - c*100 - d*10;
    printf("%d",e*10000 + d*1000 + c*100 + b*10 + a);
    return 0;
}

  1. Input Prompt: printf("Enter a five-digit number: "); prompts the user to enter a five-digit number.
  2. Reading Input: scanf("%d", &n); reads the input number.
  3. Extracting Digits:
    • a = n / 10000; extracts the first digit.
    • b = (n / 1000) % 10; extracts the second digit.
    • c = (n / 100) % 10; extracts the third digit.
    • d = (n / 10) % 10; extracts the fourth digit.
    • e = n % 10; extracts the fifth digit.
  4. Printing the Reversed Number: printf("Reversed number: %d", e * 10000 + d * 1000 + c * 100 + b * 10 + a); prints the reversed number.
Exit mobile version