I/O Operations Q. 6: Scientist Game
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 )
#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 )
#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;
}
Comments
Post a Comment