BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Display Armstrong Number Between Two Intervals

Last Updated: December 3, 2022 by Chaitanya Singh | Filed Under: C Programs

In this guide, we will write a C program to print all Armstrong numbers between the given ranges.

A number is called Armstrong number, if it satisfies the following condition:

abc...n = an + bn+ cn+...
Here, n is the number of digits in the number

For example:
2 is an Armstrong number because 2 = 21
12 is not an Armstrong number because 12 != 12 + 22
370 is an Armstrong number because 370 = 33 + 73 + 03

C Program

Steps in the following program:

  • Get the lower range and higher range values from the user using scanf.
  • If the user enters higher range first and lower range second then swap the ranges so that the program works as expected.
  • Check each number in the given range and print the number if it is an Armstrong number.
#include <math.h>
#include <stdio.h>
int main() {
  int lowRange, highRange, number, checkNumber, rem, count = 0;
  double result = 0.0;
  printf("Enter the lower range and upper range: ");
  scanf("%d %d", &lowRange, &highRange);
  printf("Armstrong numbers from %d to %d are: ", lowRange, highRange);

  // if higher range is lower than lower range then swap those ranges
  if (highRange < lowRange) {
    highRange += lowRange;
    lowRange = highRange - lowRange;
    highRange -= lowRange;
  }

  // Check each number between lowerRange and highRange
  for (number = lowRange; number <= highRange; number++) {
    checkNumber = number;

    // Counting number of digits in the checkNumber
    while (checkNumber != 0) {
    	checkNumber /= 10;
      ++count;
    }

    //restoring the value of checkNumber after counting digits
    checkNumber = number;

    // calculate sum of nth power of individual digits
    // and store the result in the variable result
    while (checkNumber != 0) {
      rem = checkNumber % 10;
      result += pow(rem, count);
      checkNumber /= 10;
    }

    // check if number is equal to result, if it is then
    //the number is an Armstrong number so print that number
    if ((int)result == number) {
      printf("%d ", number);
    }

    // resetting the values for the next number in the range
    count = 0;
    result = 0;
  }

  return 0;
}

Output: Armstrong numbers between 1 and 1000

C Program to Display Armstrong Number Between Two Intervals

Related Examples:

  • C Program to check Armstrong number
  • C Program to calculate power of a number
  • C Program to find prime numbers in a given range
  • Java Program to print Armstrong numbers between a given range
  • C++ Program to print Armstrong numbers between 1 and 1000
❮ C TutorialC Programs ❯

Top Related Articles:

  1. C Program to concatenate two strings without using strcat
  2. C Program to find factorial of a number using Recursion
  3. C Program to check Armstrong number
  4. C Program to display factors of a number
  5. C Program to Display Characters from A to Z Using Loop

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap