﻿ C : Check if a given number is an armstrong number or not

# C Exercises: Check whether a given number is an armstrong number or not

## C For Loop: Exercise-29 with Solution

Write a C program to check whether a given number is an Armstrong number or not.

When the sum of the cube of the individual digits of a number is equal to that number, the number is called Armstrong number. For Example 153 is an Armstrong number because 153 = 13+53+33.

Test Data :
Input a number: 153
Expected Output :
153 is an Armstrong number.

Visual Presentation:

## Visualize C code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution:

C Code:

``````#include <stdio.h> // Include the standard input/output header file.

int main() {
int num, r, sum = 0, temp; // Declare variables for the input number, remainder, sum, and a temporary variable.

printf("Input a number: "); // Prompt the user to input a number.
scanf("%d", &num); // Read the number from the user.

temp = num; // Save the original number in a temporary variable.

while (num != 0) { // Start a loop to extract digits from the number.
r = num % 10; // Get the last digit of the number.
sum = sum + (r * r * r); // Calculate the sum of cubes of each digit.
num = num / 10; // Move to the next digit.
}

if (sum == temp) // If the sum of cubes of digits is equal to the original number.
printf("%d is an Armstrong number.\n", temp); // Print that it's an Armstrong number.
else
printf("%d is not an Armstrong number.\n", temp); // Otherwise, print that it's not an Armstrong number.

return 0; // Return 0 to indicate successful execution.
}

```
```

Sample Output:

```Input  a number: 153
153 is an Armstrong number.
```

Flowchart:

C Programming Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿