Armstrong Numbers from 1 to N using while and do-while loop
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Armstrong Numbers from 1 to N using while and do-while loop

📑 On this page:
  • Introduction
  • C Program using while loop
  • C Program using do-while loop
  • Sample Output
  • Program Explanation
  • Step-by-Step Approach
  • Practice Exercise
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is an Armstrong number
  • How to find Armstrong numbers using while loop
  • How to find Armstrong numbers using do-while loop
  • Complete code example with step-by-step explanation
  • Key differences between while and do-while loop

Introduction

In this tutorial, we will learn how to write a C program to print all Armstrong numbers from 1 to N using while and do-while loop.

An Armstrong number (also called a Narcissistic number or Pluperfect number) is a number that is equal to the sum of the cubes of its individual digits.

For example: 1, 153, 370, 371, and 407 are Armstrong numbers.

Example: 153 is an Armstrong number because:
1³ + 5³ + 3³ = 1 + 125 + 27 = 153

💡 Armstrong Number: A number that equals the sum of the cubes of its digits. For example, 153 = 1³ + 5³ + 3³.

C Program using while loop

#include <stdio.h>

/* Write C Program to find all Armstrong numbers from 1 to N using while loop */

int main()
{
    int r = 0, limit, sum = 0, n = 1, n1;

    /* Input upper limit from user */
    printf("Enter upper limit to find Armstrong number: ");
    scanf("%d", &limit);

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

    while (n <= limit)
    {
        sum = 0;
        n1 = n;

        /* Calculate sum of cubes of digits */
        while (n1 != 0)
        {
            r = n1 % 10;
            sum += r * r * r;
            n1 = n1 / 10;
        }

        /* Check for Armstrong number */
        if (n == sum)
        {
            printf("%d, ", n);
        }
        n++;
    }

    return 0;
}

C Program using do-while loop

#include <stdio.h>

/* Write C Program to find all Armstrong numbers from 1 to N using do-while loop */

int main()
{
    int r = 0, limit, sum = 0, n = 1, n1;

    /* Input upper limit from user */
    printf("Enter upper limit to find Armstrong number: ");
    scanf("%d", &limit);

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

    do
    {
        sum = 0;
        n1 = n;

        /* Calculate sum of cubes of digits */
        do
        {
            r = n1 % 10;
            sum += r * r * r;
            n1 = n1 / 10;
        } while (n1 != 0);

        /* Check for Armstrong number */
        if (n == sum)
        {
            printf("%d, ", n);
        }
        n++;

    } while (n <= limit);

    return 0;
}

Sample Output

Enter upper limit to find Armstrong number: 407
Armstrong numbers between 1 to 407 are:
1, 153, 370, 371, 407

Another Example:

Enter upper limit to find Armstrong number: 1000
Armstrong numbers between 1 to 1000 are:
1, 153, 370, 371, 407

Program Explanation

Let's break down the code step by step using the while loop version:

  1. Variable Declaration: int r=0, limit, sum=0, n=1, n1;
    • limit — stores the upper range
    • n — current number being checked
    • sum — stores the sum of cubes of digits
    • r — stores the remainder (individual digit)
    • n1 — temporary variable for processing
  2. Input from User:
    printf("Enter upper limit to find Armstrong number: ");
    scanf("%d", &limit);
    — The user enters a number (e.g., 407), which is stored in limit.
  3. Outer while Loop: while(n <= limit)
    — This loop runs from 1 to limit. For each number, we check if it is an Armstrong number.
  4. Calculate Sum of Cubes of Digits:
    — n1 = n; (copy the current number)
    — The inner while loop extracts each digit, calculates its cube, and adds it to sum.
    Example for 153:
    • Digit 3: r=3, sum=27, n1=15
    • Digit 5: r=5, sum=152, n1=1
    • Digit 1: r=1, sum=153, n1=0
  5. Check Armstrong Condition:
    if(n == sum)
    — If the original number n equals the sum of cubes of its digits, it is an Armstrong number and gets printed.
  6. Increment: n++;
    — Move to the next number and repeat until n > limit.
  7. Return: return 0; — successful program execution.

📝 Note: The do-while loop version works similarly, except the loop body executes at least once before checking the condition. In this program, both versions produce the same output.

Step-by-Step Approach

Step Action Explanation
1Declare variablesDeclare limit, n, sum, r, and n1
2Read limitGet upper limit from user
3Initialize n=1Start checking from 1
4Calculate digit cubesExtract digits, calculate sum of cubes
5Check conditionIf n equals sum of cubes, print n
6Increment nMove to next number
7RepeatContinue until n exceeds limit

Algorithm to Find Armstrong Numbers

  1. Start
  2. Read the value of N (limit) from user
  3. Initialize n = 1
  4. While n <= N:
    • Set sum = 0
    • Set n1 = n
    • While n1 != 0:
      • r = n1 % 10
      • sum += r * r * r
      • n1 = n1 / 10
    • If n == sum, print n
    • n++
  5. End

💻 Practice Exercise

Challenge 1: Write a program to find all 3-digit Armstrong numbers (153, 370, 371, 407) without using loops.

Challenge 2: Write a program to check if a given number is an Armstrong number or not.

Challenge 3: Write a program to print Armstrong numbers between two given numbers (start and end range).

🔍 Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
    int num, r, sum;
    printf("3-digit Armstrong numbers are:\n");
    for(num = 100; num <= 999; num++) {
        sum = 0;
        int temp = num;
        while(temp != 0) {
            r = temp % 10;
            sum += r * r * r;
            temp = temp / 10;
        }
        if(num == sum)
            printf("%d, ", num);
    }
    return 0;
}
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
    int num, r, sum = 0, temp;
    printf("Enter a number: ");
    scanf("%d", &num);
    temp = num;
    while(temp != 0) {
        r = temp % 10;
        sum += r * r * r;
        temp = temp / 10;
    }
    if(num == sum)
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is NOT an Armstrong number.\n", num);
    return 0;
}

Frequently Asked Questions

1. What is an Armstrong number?

An Armstrong number is a number that is equal to the sum of the cubes of its individual digits. For example, 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

2. What is the difference between while and do-while loop?

The while loop checks the condition first, then executes the body. The do-while loop executes the body first, then checks the condition. This means do-while guarantees at least one execution.

3. What are the Armstrong numbers between 1 and 1000?

The Armstrong numbers between 1 and 1000 are: 1, 153, 370, 371, 407.

4. What is the time complexity of this algorithm?

The algorithm has a time complexity of O(N × d) where N is the limit and d is the number of digits in each number. For 3-digit numbers, it's O(N × 3).

5. Can I calculate Armstrong numbers without using loops?

Yes, you can manually check each number, but loops make the program efficient and scalable for any range.

💡 Tip: Always validate the input to handle negative numbers and ensure the limit is positive.

📖 Related Tutorials

  • Prime Numbers from 1 to N
  • Factorial using while loop
  • Leap Years from 1 to N
  • Sum of N Natural Numbers

Previous Topic: -->> Prime Numbers from 1 to N   ||   Next topic: -->> Leap Years from 1 to N


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python If-Else FAQ Python Loops FAQ Python Lists & Dictionaries FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Control Flow & Operators FAQ Java Methods FAQ Java OOP Best Practices FAQ Java Threads & Concurrency FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.