Saturday 15 October 2011

(h) If a five-digit number is input through the keyboard, write a program to reverse the number.


/* This program takes a five-digit integer from user, and shows its reverse.*/
#include<stdio.h>
#include<conio.h>
int main ()
{

/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,reverse,temp;

/*prompt to take the input*/
    printf("Please enter a five-digit number: ");

/*Taking input*/   
    scanf("%d",&value);
 temp = value ;

/*Calculations*/
    dig1 = value % 10;
  
    /* e.g. if value is 12345 then 12345 % 10 gives 5*/
  
    value = value / 10;
  
    /* '12345/10' gives '1234.5' but this is integer division so digit/s after '.' will
    be dropped and we get '1234'*/
   
    dig2 = value % 10;
  
    /*now '1234 % 10' gives '4' and so on*/
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
   
    reverse = (dig1*10000) + (dig2*1000) + (dig3*100) + (dig4*10) + (dig5) ;

/*showing Results*/   
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   By reversing %d it becomes : %d",temp,reverse);
    getch();
    return 0;
}
   



   

7 comments:

  1. Kanhaiya Kumar Singh
    do u get output for this program

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

  3. int main()
    {
    int a,b,c,d,e,f;
    printf("\n\nenter Five Digit number= ");
    scanf("%d",&a);
    f=a%10;
    a=a/10;
    b=a%10;
    a=a/10;
    c=a%10;
    a=a/10;
    d=a%10;
    a=a/10;
    e=a%10;
    a=a/10;

    printf("\n\n\nreverse of five digit number= %d%d%d%d%d\n\n\n\n",f,b,c,d,e);
    getch();
    return 0;

    }

    ReplyDelete
  4. sir why we *digit1 with 10000,1000,100,10,5

    ReplyDelete
  5. This is helpful and specially thanks for writing comments

    ReplyDelete
  6. I have method which is better than all others

    ReplyDelete