You are currently viewing C Program to Print Armstrong Numbers using While Loop between 1 to n

C Program to Print Armstrong Numbers using While Loop between 1 to n

To Print the list of all Armstrong numbers that is present between 1 to n, First we have to write a logic to check given number is Armstrong or not. After building logic and writing Program, we will be using while loop in C to iterate and print only the Armstrong number.

In this tutorial, you will learn how to write a program to check and print all Armstrong numbers between 1 to n using a while loop.

Required Knowledge

Problem Statement

Write a C program that takes a number n as input and prints all Armstrong numbers between 1 and n using a while loop.

Example

Input:

Enter the value of n: 1000

Output:
1
153
370
371
407

C Program to print all Armstrong numbers between 1 to n using while loop

#include <stdio.h>
#include <math.h>

int countDigits(int num) {
    int counter = 0;
    while (num != 0) {
        num /= 10;
        counter++;
    }
    return counter;
}

int isArmstrong(int num) {
    int originalNum = num;
    int digitCounter = countDigits(num);
    int sum = 0;
    while (num != 0) {
        int digit = num % 10;
        sum += pow(digit, digitCounter);
        num /= 10;
    }
    return originalNum == sum;
}

void printArmstrongNumbers(int n) {
    printf("Armstrong numbers between 1 and %d are:\n", n);
    for (int i = 1; i <= n; i++) {
        if (isArmstrong(i)) {
            printf("%d\n", i);
        }
    }
}

int main() {
    int n;
    printf("Please Enter the value of n: ");
    scanf("%d", &n);
    printArmstrongNumbers(n);
    return 0;
}

Output

Please Enter the value of n: 160
Armstrong numbers between 1 and 160 are:
1
2
3
4
5
6
7
8
9
153

Program Explanation

  • The program takes input for the value of n from the user.
  • It initializes a variable num to 1, which represents the number being checked.
  • The program enters a while loop with the condition num <= n to iterate through numbers from 1 to n.
  • Inside the while loop, it initializes a variable sum to 0, which will hold the sum of the cubes of the digits.
  • It also assigns the value of num to a temporary variable temp for processing.
  • The program enters a nested while loop with the condition temp > 0 to extract the digits of temp.
  • Inside the nested while loop, it calculates the remainder of temp divided by 10 and stores it in remainder.
  • The program adds the cube of remainder to sum.
  • It updates temp by dividing it by 10, effectively removing the rightmost digit.
  • The nested while loop continues until all digits have been processed.
  • After the nested while loop, the program checks if sum is equal to num.
  • If sum is equal to num, it means num is an Armstrong number.
  • In that case, the program prints num on the screen.
  • If sum is not equal to num, it means num is not an Armstrong number, and the program continues to the next iteration.
  • The program increments num by 1 and repeats the process until all numbers from 1 to n have been checked.
  • Once the while loop is complete, the program ends.

This program explanation provides a step-by-step breakdown of how the program works to identify and print all Armstrong numbers between 1 and n using a while loop.

Conclusion

The C program successfully prints all Armstrong numbers between 1 and n using a while loop. By iterating through the numbers, checking each number for the Armstrong property using a nested while loop, and printing the Armstrong numbers, it identifies and displays the Armstrong numbers on the screen.