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 numbers are equal or not.
#include <stdio.h>
#include <conio.h>
void main( )
{
int n, a, b, num;
long revnum = 0; /* Initialised otherwise it will contain garbage value. */
/* Declared as long since after reversing, it may not be in the int range */
clrscr ( );
printf ("\n Enter a five digit number (less than 32767) ............:"); scanf ("%d",&n);
num = n; /* Entered number stored for comparison later */
a = n % 10; /* Last digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 10000L; /* revnum updeted with value of extracted digit */
a = n % 10; /* Fourth digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 1000;
a = n % 10; /* Third digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 100;
a = n % 10; /* Second digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 10;
a = n % 10; /* First digit */
revnum = revnum + a;
if (revnum == num)
printf ("\n Given number and its reversed number are equal.");
else
printf ("\n Given number and its reversed number are not equal.");
getch ( );
}
To contact us
#include <stdio.h>
#include <conio.h>
void main( )
{
int n, a, b, num;
long revnum = 0; /* Initialised otherwise it will contain garbage value. */
/* Declared as long since after reversing, it may not be in the int range */
clrscr ( );
printf ("\n Enter a five digit number (less than 32767) ............:"); scanf ("%d",&n);
num = n; /* Entered number stored for comparison later */
a = n % 10; /* Last digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 10000L; /* revnum updeted with value of extracted digit */
a = n % 10; /* Fourth digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 1000;
a = n % 10; /* Third digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 100;
a = n % 10; /* Second digit */
n = n / 10; /* Remaining digits */
revnum = revnum + a * 10;
a = n % 10; /* First digit */
revnum = revnum + a;
if (revnum == num)
printf ("\n Given number and its reversed number are equal.");
else
printf ("\n Given number and its reversed number are not equal.");
getch ( );
}
To contact us
No comments:
Post a Comment