162k views
23 votes
I want to solve this question in C program, Please.

I want to solve this question in C program, Please.-example-1
User Nixmd
by
7.7k points

1 Answer

0 votes

Answer:

#include <stdio.h>

#include <math.h>

int is_armstrong(int n) {

int cubedSum = 0;

int r = n;

while (r) {

cubedSum += (int)pow(r % 10, 3);

r = r / 10;

}

return cubedSum == n;

}

int stack_count(int n) {

int sum = 0;

while (n) {

sum += n % 10;

n = n / 10;

}

return sum;

}

int is_magical(int n) {

while (n > 9) {

n = stack_count(n);

printf("%d ", n);

}

return n == 1;

}

int main()

{

int input = 0;

int isMagical, isArmstrong;

while (true) {

printf("Enter a number: ");

scanf_s("%d", &input, sizeof(int));

if (input == -1) break;

isArmstrong = is_armstrong(input);

isMagical = is_magical(input);

if (isArmstrong && isMagical) {

printf("%d is both an armstrong and a magical number.\\", input);

} else if (isArmstrong) {

printf("%d is an armstrong number\\", input);

} else if (isMagical) {

printf("%d is a magical number\\", input);

} else {

printf("%d is neither an armstrong or a magical number.\\", input);

}

}

}

Step-by-step explanation:

Here is a starting point. What's the definition of a magical number?

User Stvnrlly
by
6.8k points