Armstrong numbers between two intervals in C Program

Armstrong numbers between two intervals in C Program

Armstrong numbers between two intervals in C Program Description:

Write a Program to generate Armstrong numbers between two intervals in C programming language using loops. The program should accept two positive integers from the user and display all Armstrong numbers between the given numbers (given interval).

📢 The Program should generate all digits of Armstrong numbers i.e 3-digit and N-digit Armstrong numbers

This program is useful to generate the Armstrong numbers between the numbers. Like you may want to know all Armstrong numbers from 1 to 1000, or You may want to know all Armstrong numbers from 100 to 10000, etc. So by using this program you can easily get all Armstrong numbers between two intervals.

Here are the expected input and output of the program:

Expected Input and Output:

Input:

Enter Two Number: 1 1000

Output:

Armstrong numbers between 1 and 1000 are : 1 2 3 4 5 6 7 8 9 153 370 371 407

What is an N-Digit Armstrong Number?

A positive number of n digits is said to be an Armstrong number of order n, If the sum of the power of n of each digit in the number is equal to the original number itself.

If the number is qwer is Armstrong number, Then it should satisfy the below equation.

qwer = pow(q, n) + pow(w, n) + pow(e, n) + pow(r, n)

Here n is the number of digits in the number qwer which is 4

Few Examples of Armstrong Numbers.

DigitArmstrong Numbers
10, 1, 2, 3, 4, 5, 6, 7, 8, 9
3153, 370, 371, 407
41634, 8208, 9474
554748, 92727, 93084
6548834
71741725, 4210818, 9800817, 9926315

Related Armstrong Numbers Programs:

This program is part of the Armstrong numbers programs series. You can find the other programs below.

Pre-Requisites (Knowledge Required):

It is recommended to know the basics of the C Loops like the While Loop, For loop, And Arithmetic operators like Modulus operators, and Division Operators.

Armstrong numbers between two intervals in C Algorithm:

  1. Accept two numbers from the user and store them in variables num1 and num2 respectively.
  2. We don’t allow the negative number for intervals, So Check if any of the numbers ( num1 or num2 or both) is a Negative number, If so display the error message( Error: Please enter positive numbers) and stop the program. Otherwise, proceed to the next step.
  3. As specified earlier, We need to calculate the pow(digit, nDigits) to check the Armstrong number.
  4. So we need to count the number of digits in the given number num. To do that, We use a function called countDigits(), Which takes a number as input and returns the number of digits.- int nDigits = countDigits(temp);
  5. We are going to use two loops, Outer-Loop and Inner-Loop.
  6. The Outer-Loop: This starts from the number num1 and goes all the way up to the number num2. We use the variable i to control Outer-Loop. – for(i=num1; i<=num2; i++)
    • Take the backup of the variable i and store it in variable temp. As we need i for later comparisons.
    • The Inner-Loop: Inner-Loop will help us to check if the outer loop variable i is an Armstrong number or not. If the i is Armstrong number, Then it will print it onto the console.
      • At Each Iteration,
      • Get the last digit from the number ( temp) i.e rem = temp %10;
      • Calculate the power of rem with nDigits(Number of digits in temp) – i.e power(rem, nDigits);. We are using the Power of Number Program in C to calculate the power instead of the pow built-in function. Then Update the arm variable.
      • Remove the last digit using the temp/10 operation.
    • Check if the variable arm is equal to i. If both are equal, Then i is an Armstrong number. Print it onto the console.
  7. The above step-6 will continue until i reaches the second number num2. ( Outer Loop Condition – i<=num2 )
  8. Once the above two loops are terminated, We will get all Armstrong numbers between the interval of num1 and num2. ( user-provided interval )

Armstrong numbers between two intervals in C Program:

In this program, We are using the custom power function ( double power(double base, double exponent) ) to calculate the power of numbers.

We are using two user-defined functions in this program, They are

The countDigits function:

Prototype:

int countDigits(int num)

The countDigits function is used to count the number of digits in the given number. This function takes num as the input and returns an integer which is the number of digits in num

Learn more about it at Count digits in a Number Program

The power function:

Prototype:

double power(double base, double exponent)

The power function is used to calculate the power of a number. It accepts two numbers base and exponent and calculates the power and returns it (as double).

Learn more about custom power function at Power of Number Program

We can also use the C languages library power function. We will look at in the next section.

Program Output:

Compile the Program. We are using the GCC compiler here.

$ gcc armstrong-between-numbers.c

Run the executable. Let’s generate all Armstrong numbers from 1 to 1000 numbers.

As you can see from the above output, The numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407 are the Armstrong numbers between the interval 1 and 1000.

Let’s try a few more examples.

We are getting the expected results, The Armstrong numbers between the number 1 and 100000 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084.

armstrong-numbers-between-two-intervals-in-c-program-output

Here is the example with Negative numbers

As expected, The program is displaying the error message if the user enters a negative number.

Generate Armstrong numbers between two intervals in C using pow library function:

We have used our custom power function in the above example, But as we already know the C language has an in-built pow function, which is available as part of math.h header files and libm.so library.

Let’s remove our custom power function from the above program and use the in-built pow function.

Here is the pow function syntax:

double pow(double x, double y)

📢 Don’t forget to include the math.h header file

Program Output:

As we are using the pow function from the math.h headerfile, We need to pass the -lm compilation option. Otherwise, we will get a Linking error like below.

So let’s add the -lm option as well to compile the program.

$ gcc armstrong-between-interval.c -lm

get-armstrong-numbers-between-two-intervals-output

Run the program

Related Programs:

  1. Collection of C Loops Programs [ List of Loop Practice Programs ]
  2. C Program to Check number is Prime Number or not?
  3. C Program to Check Prime Number using Square Root (Sqrt) Method
  4. C Program to Print Prime Numbers between Two numbers
  5. C Program to print First N Prime numbers
  6. C Program to generate Prime numbers up to N
  7. C Program to Calculate Nth Prime Number
  8. C Program Check Prime Number [Mutliple Methods]
  9. C Program to Convert Decimal Number to Binary Number
  10. C Program to Convert Binary Number to Decimal Number
  11. C Program to Convert Octal Number to Decimal Number

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] C Program to Generate Armstrong Numbers between two Intervals […]

  2. […] C Program to Generate Armstrong Numbers between two Intervals […]

  3. […] C Program to Generate Armstrong Numbers between two Intervals […]

  4. […] C Program to Generate Armstrong Numbers between two Intervals […]

Leave a Reply