Saturday 19 December 2015

Program to find the Reverse of a number.

//Program to find the Reverse of a number.
#include<stdio.h>
#include<conio.h>

void main()
{
      int  number,reverse=0, temp; /* 'reverse' is  used to store the reverse of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the reminder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&number);
      temp = number; //copies the value of 'number' in 'temp'

// logic for finding the reverse of the number

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
          rem=temp%10; // extracting the last digit by dividing it with 10
     reverse=reverse*10+rem; // calculating the reverse of the number
     temp=temp/10; // dividing the number by 10 to break it in parts.
      } // end of while loop

      printf("The reverse of the number %d = %d",number,reverse); // printing the reversed number
      getch(); // for holding the console screen
}

No comments:

Post a Comment