Three digit number problem
Ask the user to enter a three digit number and check if the sum of 'number and number obtained by reversing the digits’ is a three digit number or not. Also check if any of the digits in the result matches the digits in the original number.
Let us understand the problem statement with the help of an example:
Let num = 452,
So, sum of 'number and number obtained by reversing the digits’ = 452 + 254 = 706
As you can see, sum is a three digit number and there is no common digit between the original number and sum.
Let us consider another example,
Let, num = 428
So, sum of 'number and number obtained by reversing the digits’ = 428 + 824 = 1252
As you can see, sum is a four digit number and there is a common digit '2' between the original number and sum.
Let num = 452,
So, sum of 'number and number obtained by reversing the digits’ = 452 + 254 = 706
As you can see, sum is a three digit number and there is no common digit between the original number and sum.
Let us consider another example,
Let, num = 428
So, sum of 'number and number obtained by reversing the digits’ = 428 + 824 = 1252
As you can see, sum is a four digit number and there is a common digit '2' between the original number and sum.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int number, o, t, h, sum, revnum, flag = 0;
printf("\nEnter a three digit number\t:");
scanf("%d",&number);
o = number % 10;
h = number / 100;
t = (number / 10) % 10;
revnum = (o * 100 + t * 10 + h);
printf("\nThe reverse number is\t:%d",revnum);
sum = number + revnum;
printf("\nThe sum of the number and its reverse is\t:%d",sum);
if(sum <= 999)
{
printf("\nThe sum is a three digit number");
//ones place
if(((sum % 10) == o) || ((sum % 10) == h) || ((sum % 10) == t))
{
flag = 1;
}
//hundreds place
else if(((sum / 100) == o) || ((sum / 100) == h) || ((sum / 100) == t))
{
flag = 1;
}
//tens place
else if((((sum / 10) % 10) == o) || (((sum / 10) % 10) == h) || (((sum / 10) % 10) == t))
{
flag = 1;
}
}
if(sum > 999)
{
printf("\nThe sum is a four digit number");
//ones place
if(((sum % 10) == o) || ((sum % 10) == h) || ((sum % 10) == t))
{
flag = 1;
}
//thousand digit
else if(((sum / 1000) == o) || ((sum / 1000) == h) || ((sum / 1000) == t))
{
flag = 1;
}
//tens place
else if((((sum / 10) % 10) == o) || (((sum / 10) % 10) == h) || (((sum / 10) % 10) == t))
{
flag = 1;
}
//hundreds digit
else if((((sum / 100) % 10) == o) || (((sum / 100) % 10) == h) || (((sum / 100) % 10) == t ))
{
flag = 1;
}
}
if(flag == 1)
{
printf("\nDigits in the result matches the digits in the original number");
}
else
{
printf("\nDigits in the result does not matches the digits in the original number");
}
getch();
}
Output
********** Run1 **********
Enter a three digit number :452
The reverse number is :254
The sum of the number and its reverse is :706
The sum is a three digit number
Digits in the result does not matches the digits in the original number
********** Run2 **********
Enter a three digit number :143
The reverse number is :341
The sum of the number and its reverse is :484
The sum is a three digit number
Digits in the result matches the digits in the original number
********** Run3 **********
Enter a three digit number :458
The reverse number is :854
The sum of the number and its reverse is :1312
The sum is a four digit number
Digits in the result does not matches the digits in the original number
********** Run4 **********
Enter a three digit number :428
The reverse number is :824
The sum of the number and its reverse is :1252
The sum is a four digit number
Digits in the result matches the digits in the original number
Explanation
A number is taken as an input from the user and stored in the variable named say 'number'.
First step is to extract the individual digits of the number (ones, tens, and hundreds) and store in the variable named 'o', 't' and 'h' respectively. This is done using the expressions:
To calculate the reverse of the number, following expression is used.
Next step is to calculate the sum of the number and the reversed number.
Now we have to check whether the 'sum' is of 3 digits or 4 digits. This is done by comparing it with number '999'.
If sum <= 999, then it is of 3 digits.
If sum > 999, then sum is of 4 digits.
Now, we have to check whether any of the digits in 'sum' and the actual number matches or not. For this, we have to extract the individual digits of 'sum' and compare them with the digits of actual number ('o', 't', 'h') extracted previously. If 'sum' is 3 digit number, then we will calculate the ones, tens and hundreds place of the 'sum'. And if the 'sum' is of 4 digit, then we will calculate ones, tens, hundreds, and thousands place of the 'sum'. This is done as follows:
If
If
Now, how to keep track whether any of the digit is matched or not. For this a variable named 'flag' is used. Initially 'flag' is set to 0, if any of the digit matches, then this 'flag' is set to 1.
After all the matching is done, value of 'flag' is checked.
If value of 'flag' is 0, then none of the digits matched.
If value of 'flag' comes to be 1, then one or some of the digits are matched.
First step is to extract the individual digits of the number (ones, tens, and hundreds) and store in the variable named 'o', 't' and 'h' respectively. This is done using the expressions:
o = number % 10;
h = number / 100;
t = (number / 10) % 10;
To calculate the reverse of the number, following expression is used.
revnum = (o * 100 + t * 10 + h);
Next step is to calculate the sum of the number and the reversed number.
sum = number + revnum;
Now we have to check whether the 'sum' is of 3 digits or 4 digits. This is done by comparing it with number '999'.
If sum <= 999, then it is of 3 digits.
If sum > 999, then sum is of 4 digits.
Now, we have to check whether any of the digits in 'sum' and the actual number matches or not. For this, we have to extract the individual digits of 'sum' and compare them with the digits of actual number ('o', 't', 'h') extracted previously. If 'sum' is 3 digit number, then we will calculate the ones, tens and hundreds place of the 'sum'. And if the 'sum' is of 4 digit, then we will calculate ones, tens, hundreds, and thousands place of the 'sum'. This is done as follows:
If
sum <= 999
(3 digit sum) then,
for ones place,
for hundreds place,
for tens place,
|
If
sum > 999
(4 digit sum) then,
for ones place
for thousand digit
for tens place
for hundreds digit
|
Now, how to keep track whether any of the digit is matched or not. For this a variable named 'flag' is used. Initially 'flag' is set to 0, if any of the digit matches, then this 'flag' is set to 1.
After all the matching is done, value of 'flag' is checked.
If value of 'flag' is 0, then none of the digits matched.
If value of 'flag' comes to be 1, then one or some of the digits are matched.