Tech Unleashed

Wohoo!! We're on YouTube

Wednesday, 19 January 2022

C program for "If a five digit number is input through keyboard, write a program to reverse the number."

Problem:  C program for "If a five digit number is input through keyboard, write a program to reverse the number."

Logic used : 
  • Start a loop
  • check if the number is zero or not
  • take the remainder out using modulus operator and store it in a variable
  • take another variable with initial value zero . lets take it as ' rev '
  • use the formulae : rev = rev x 10 + remainder
  • take the quotient out by dividing number by 10 and store it in the num variable itself
  • repeat whole step until the num variable becomes zero.
Demo :
while(num!=0){
        remainder = num%10;
        rev = rev*10+remainder;
        num=num/10;
    }

C program : 

// Program by Shyam Kanth

#include<stdio.h>

int main(){
    int num, rev, remainder;
    rev = 0;
    printf("Enter the five digit number : ");
    scanf("%d",&num);
    while(num!=0){
        remainder = num%10;
        rev = rev*10+remainder;
        num=num/10;
    }
    printf("Reversed number is %d.",rev);
}

Codes used : 
  • #include < stdio.h > : Header file for input-output
  • int : Integer data type
  • num, rev, remainder : Variable used
  • printf : Function for displaying message
  • scanf : Function for taking input
  • while : Loop
Do comment, if any doubt
Know about me : Shyam Kanth

No comments:

Post a Comment