C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print all Armstrong numbers between 1 and N

Introduction

An Armstrong number (also called a narcissistic number) is a number that is equal to the sum of its digits raised to the power of the number of digits.

Examples:

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

In this program, the user enters a positive integer N, and the program prints all Armstrong numbers from 1 to N using nested for, while and do..while loops.

 

C Program: Print all Armstrong numbers between 1 and N

Method 1: Using for loop

C

#include <stdio.h>

#include <math.h>

 

int main() {

    int n, num, original, remainder, digits;

    double sum;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

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

        return 0;

    }

 

    printf("Armstrong numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 1 to n

    for (num = 1; num <= n; num++) {

        original = num;

        sum = 0;

        digits = 0;

 

        // Count number of digits

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

            digits++;

 

        // Calculate sum of powers of digits

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

            remainder = temp % 10;

            sum += pow(remainder, digits);

        }

 

        // Check Armstrong condition

        if ((int)sum == original)

            printf("%d ", original);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 500
Armstrong numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

OUTPUT 2 :
Enter the value of N: 200
Armstrong numbers between 1 and 200 are:
1 2 3 4 5 6 7 8 9 153

Explanation

  1. The user enters a positive integer n.
  2. Outer loop iterates each number from 1 to n.
  3. For each number:
    • Count the number of digits.
    • Compute the sum of each digit raised to the power of the number of digits.
  4. If the sum equals the number → it is an Armstrong number and printed.

 

C Program: Print all Armstrong numbers between 1 and N

Method 2: Using while loop

C

#include <stdio.h>

#include <math.h>

 

int main() {

    int n, num, original, remainder, digits, temp;

    double sum;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

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

        return 0;

    }

 

    printf("Armstrong numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 1 to n

    num = 1;

    while (num <= n) {

        original = num;

        sum = 0;

        digits = 0;

        temp = num;

 

        // Count number of digits

        while (temp != 0) {

            digits++;

            temp /= 10;

        }

 

        // Calculate sum of powers of digits

        temp = num;

        while (temp != 0) {

            remainder = temp % 10;

            sum += pow(remainder, digits);

            temp /= 10;

        }

 

        // Check Armstrong condition

        if ((int)sum == original)

            printf("%d ", original);

 

        num++;

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 500
Armstrong numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

OUTPUT 2 :
Enter the value of N: 200
Armstrong numbers between 1 and 200 are:
1 2 3 4 5 6 7 8 9 153

Explanation

  1. User inputs a positive integer n.
  2. Outer while loop iterates through numbers from 1 to n.
  3. For each number:
    • Count digits using a while
    • Compute sum of each digit raised to the number of digits using another while
  4. If sum equals the number → it’s an Armstrong number and printed.

 

C Program: Print all Armstrong numbers between 1 and N

Method 3: Using do..while loop

C

#include <stdio.h>

#include <math.h>

 

int main() {

    int n, num = 1, original, remainder, digits, temp;

    double sum;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

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

        return 0;

    }

 

    printf("Armstrong numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 1 to n

    do {

        original = num;

        sum = 0;

        digits = 0;

        temp = num;

 

        // Count number of digits

        if (temp == 0)

            digits = 1; // special case for 0

        else

            do {

                digits++;

                temp /= 10;

            } while (temp != 0);

 

        // Calculate sum of powers of digits

        temp = num;

        do {

            remainder = temp % 10;

            sum += pow(remainder, digits);

            temp /= 10;

        } while (temp != 0);

 

        // Check Armstrong condition

        if ((int)sum == original)

            printf("%d ", original);

 

        num++;

    } while (num <= n);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 500
Armstrong numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

OUTPUT 2 :
Enter the value of N: 200
Armstrong numbers between 1 and 200 are:
1 2 3 4 5 6 7 8 9 153

Explanation

  1. User inputs a positive integer n.
  2. Outer do..while loop iterates num from 1 to n.
  3. For each number:
    • Count digits using a do..while loop.
    • Compute sum of each digit raised to the number of digits using another do..while loop.
  4. If sum equals the number → it’s an Armstrong number and printed.