C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Check whether a number is Armstrong (loop)

Introduction

An Armstrong number (also called 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.

Example:

  • 153 = 1³ + 5³ + 3³ = 153 → Armstrong
  • 370 = 3³ + 7³ + 0³ = 370 → Armstrong
  • 123 = 1³ + 2³ + 3³ = 36 → Not Armstrong

This program checks whether a given number is an Armstrong number using a for, while and do..while loop.

 

C Program: Check whether a number is Armstrong (loop)

Method 1: Using for loop

C

#include <stdio.h>

#include <math.h> // for pow() function

 

int main() {

    int num, original, remainder, digits = 0;

    double result = 0.0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Count digits using for loop

    for (int temp = num; temp != 0; temp /= 10)

        digits++;

 

    // Calculate sum of powers using for loop

    for (int temp = num; temp != 0; temp /= 10) {

        remainder = temp % 10;

        result += pow(remainder, digits);

    }

 

    // Check Armstrong condition

    if ((int)result == original)

        printf("%d is an Armstrong number.\n", original);

    else

        printf("%d is not an Armstrong number.\n", original);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 153
153 is an Armstrong number.

OUTPUT 2 :
Enter a positive integer: 370
370 is an Armstrong number.

OUTPUT 3 :
Enter a positive integer: 123
123 is not an Armstrong number.
 

Explanation

  1. The user enters a positive integer.
  2. The program first counts the number of digits using a for
  3. Another for loop extracts each digit and computes its power using pow(remainder, digits).
  4. The results are summed into result.
  5. If the sum equals the original number → Armstrong number.

 

C Program: Check whether a number is Armstrong (loop)

Method 2: Using while loop

C

#include <stdio.h>

#include <math.h> // for pow() function

 

int main() {

    int num, original, remainder, digits = 0;

    double result = 0.0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Count number of digits

    int temp = num;

    while (temp != 0) {

        temp /= 10;

        digits++;

    }

 

    // Calculate sum of powers of digits

    temp = num;

    while (temp != 0) {

        remainder = temp % 10;

        result += pow(remainder, digits);

        temp /= 10;

    }

 

    // Check Armstrong condition

    if ((int)result == original)

        printf("%d is an Armstrong number.\n", original);

    else

        printf("%d is not an Armstrong number.\n", original);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 153
153 is an Armstrong number.

OUTPUT 2 :
Enter a positive integer: 371
371 is an Armstrong number.

OUTPUT 3 :
Enter a positive integer: 123
123 is not an Armstrong number.

OUTPUT 4 :
Enter a positive integer: -370
Invalid input! Please enter a positive number.

Explanation

  1. Take a positive integer input.
  2. Store it in original for comparison.
  3. Count the total number of digits using a while
  4. Again, use a while loop to extract each digit and compute
    pow(digit, number_of_digits) and sum them.
  5. Compare the sum with the original number.
  6. If they match → Armstrong number; else → Not Armstrong.

 

Key Points

  • Works for any number of digits.
  • Uses <math.h> for the pow()
  • Uses two while loops: one for digit count, one for power-sum calculation.
  • Checks only positive integers as requested.

 

C Program: Check whether a number is Armstrong (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

#include <math.h> // for pow() function

 

int main() {

    int num, original, remainder, digits = 0;

    double result = 0.0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Count digits using do...while loop

    int temp = num;

    do {

        digits++;

        temp /= 10;

    } while (temp != 0);

 

    // Calculate sum of powers of digits using do...while loop

    temp = num;

    do {

        remainder = temp % 10;

        result += pow(remainder, digits);

        temp /= 10;

    } while (temp != 0);

 

    // Check Armstrong condition

    if ((int)result == original)

        printf("%d is an Armstrong number.\n", original);

    else

        printf("%d is not an Armstrong number.\n", original);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 153
153 is an Armstrong number.

OUTPUT 2 :
Enter a positive integer: 371
371 is an Armstrong number.

OUTPUT 3 :
Enter a positive integer: 123
123 is not an Armstrong number.



Explanation

  1. The user enters a positive integer.
  2. The program counts how many digits the number has using a do..while loop.
  3. Another do..while loop extracts each digit and calculates the sum of its powers.
  4. If this sum equals the original number, it’s an Armstrong number.

 

Key Points

  • Works for any number of digits.
  • Uses <math.h> for the pow()
  • Uses two do..while loops: one for digit count, one for power-sum calculation.
  • Checks only positive integers as requested.