Tech Unleashed

Wohoo!! We're on YouTube

Monday, 14 February 2022

C program for "A five digit number is entered through the keyboard. write a program to obtain the reversed number and to determine whether the original and reversed number are qual or not."

Problem:  C program for "A five digit number is entered through the keyboard. write a program to obtain the reversed number and to determine whether the original and reversed number are equal or not."

Logic used :
  • take the number and assign it to a variable
  • check while number is not equal to zero
  • take the remainder out using modulus operator by 10
  • store it in a variable
  • use formulae rev = rev*10+ramainder to reverse the number
  • take the quotient out by diving the number by 10
  • store result in number variable itself
  • repeat step 2 to 7 until the number becomes zero
  • now check if the reversed number is same as taken number
  • print the result
Demo :
while(num!=0){
        remainder = num%10;
        rev = rev*10+remainder;
        num=num/10;
    }

C program :

// Program by Shyam sunder kanth

#include<stdio.h>

int main(){
    int num, rev=0, remainder;
    printf("Enter the 5 digit number: ");
    scanf("%d",&num);
    while(num!=0){
        remainder = num%10;
        rev = rev*10+remainder;
        num=num/10;
    }
    printf("The reversed number is %d.\n",rev);
    if(num==rev){
        printf("Both number is same.");
    }else{
        printf("Both number is not same.");
    }
    return 0;
}

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

No comments:

Post a Comment