QUESTION DESCRIPTION
Armstrong was one of the great scientist.
The Indian council decided that we need to assign some number as a gift to the great scientist.
There was a suggestion given by the Indian Council. If the sum of cube of each number is again equal to the number then they decided that they can assign the number to the great scientist.
Kindly help the Indian Council to complete the task by writing a simple logic.
Refer sample Inputs and Outputs.
Input 1: 153
Output: Give to Scientist Armstrong
Reason((1*1*1 + 5*5*5 3*3*3=153) which is equal to the number)
Input 2: 134
Output: Don't Give to Scientist Armstrong
Reason((1*1*1 + 5*5*5 2*2*2=134) which is not equal to the number )
Solution:
#include <iostream>
using namespace std;
int power(int, int);
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
scanf("%d", &n);
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("Give to Scientist Armstrong\n");
else
printf("Dont Give to Scientist Armstrong\n");
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Armstrong was one of the great scientist.
The Indian council decided that we need to assign some number as a gift to the great scientist.
There was a suggestion given by the Indian Council. If the sum of cube of each number is again equal to the number then they decided that they can assign the number to the great scientist.
Kindly help the Indian Council to complete the task by writing a simple logic.
Refer sample Inputs and Outputs.
Input 1: 153
Output: Give to Scientist Armstrong
Reason((1*1*1 + 5*5*5 3*3*3=153) which is equal to the number)
Input 2: 134
Output: Don't Give to Scientist Armstrong
Reason((1*1*1 + 5*5*5 2*2*2=134) which is not equal to the number )
Solution:
#include <iostream>
using namespace std;
int power(int, int);
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
scanf("%d", &n);
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("Give to Scientist Armstrong\n");
else
printf("Dont Give to Scientist Armstrong\n");
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Checking if a Number is an Armstrong Number
An Armstrong number (also known as a Narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For instance, the number 153 is an Armstrong number because:
In this task, we need to determine if a given number is an Armstrong number and print an appropriate message based on that.
Solution
The provided solution in C++ calculates whether a number is an Armstrong number using these steps:
- Count the number of digits in the number.
- Calculate the sum of each digit raised to the power of the number of digits.
- Compare this sum to the original number.
How the Code Works
- Input Reading: The number is read from the user.
- Digit Counting: The number of digits is counted by dividing the number by 10 repeatedly.
- Sum Calculation: For each digit, calculate the digit raised to the power of the total number of digits and sum these values.
- Comparison: Compare the computed sum with the original number and print the appropriate message.
Sample Test Cases
Test Case 1:
- Input:
153
- Output:
Give to Scientist Armstrong
- Reason: , which is equal to the original number.
Test Case 2:
- Input:
134
- Output:
Don't Give to Scientist Armstrong
- Reason: , which is not equal to the original number.