Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

C Program For Armstrong Number

How to Write a Program for Armstrong Number in C using While Loop, For Loop, Functions, and Recursion.? Here, we will also show you, C Program for Armstrong Number between 1 to n.

If the given number is equal to the sum of the power of n for each digit present in that integer, then that number can be Armstrong Number in C programming.

For example, 153 is an Armstrong Number in C programming. Number of individual digits in 153 = 3
153 = 1³ + 5³ + 3³
= 1 + 125 + 27 = 153

Below steps will show you the common approach to check for the Armstrong Number in C programming
Steps:

  1. Enter any number
  2. Divide the given number into individual digits (For Example, Divide 153 into 1, 5 and 3)
  3. Calculate the power of n for each individual and add those numbers
  4. Compare the original value with the Sum value.
  5. If they exactly matched, then it is an Armstrong number. Otherwise, it is not an Armstrong Number in C

C Armstrong Number using While Loop

This program for Armstrong Number in C allows the user to enter any positive integer and then, this program will check whether a number is Armstrong Number or Not using the While Loop

/* Armstrong Number in C using While Loop */

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

int main()
{
  int Number, Temp, Reminder, Times =0, Sum = 0;
 
  printf("\nPlease Enter number to Check for Armstrong \n");
  scanf("%d", &Number);
  
  //Helps to prevent altering the original value
  Temp = Number;
  while (Temp != 0) 
   {
      Times = Times + 1;
      Temp = Temp / 10;
   }
   
  Temp = Number;
  while( Temp > 0)
   {
     Reminder = Temp %10;
     Sum = Sum + pow(Reminder, Times);
     Temp = Temp /10;
   }
 
  printf("\n Sum of entered number is = %d\n", Sum);

  if ( Number == Sum )
      printf("\n %d is Armstrong Number.\n", Number);
  else
      printf("\n %d is not a Armstrong Number.\n", Number);
 
  return 0;
}
C Program For Armstrong Number Using While Loop

This Armstrong Number in C program allows the user to enter any positive integer. Next, that number assigned to variable Number.

Next, We assign the original value to the Temp variable. It will help us to preserve our original value and then do all the manipulation on the Temp variable.

Below While loop will make sure that the given number is greater than 0, and the statements inside the while loop will split the numbers and counts the number of individual digits inside the given number.

If you don’t understand this program logic, Please refer C Program to Count Number Of Digits in a Number article.

while (Temp != 0) 
   {
      Times = Times + 1;
      Temp = Temp / 10;
   }

Second While Loop will make sure that, the given number is greater than 0. Let us see the working principle of this C Programming while loop in iteration wise

For this Armstrong number in c demonstration, User Entered value: Number = 1634 and Sum = 0
Temp = Number
Temp = 1634

First Iteration
Reminder = Temp %10
Reminder = 1634 % 10 = 4

Sum = Sum + pow (Reminder, Times)

For this Armstrong Number in C example, Times = 3 because of the number of digits in 1634 = 4. So pow() function will multiply the Reminder 4 times as shown below

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)
Sum= 0 + (4 * 4 * 4 * 4) => 0 + 256
Sum = 256

Temp = Temp /10 = 1634 /10
Temp = 163

NOTE: If the number of digits count is 5, then the Reminder will be multiplied by 5 times.

Second Iteration
From the first Iteration, the values of both the Temp and Sum has changed as Temp = 163 and Sum = 256

Reminder = Temp %10
Reminder = 163 % 10 = 3

Sum = 256 + (3 * 3 * 3 * 3)
Sum = 256 + 81 => 337

Temp = Temp /10 => 163 /10
Temp = 16

Third Iteration
Temp = 16 and Sum = 337

Reminder = Temp %10
Reminder = 16 % 10 = 6

Sum = 337 + (6 * 6 * 6 *6) => 337 + 1296
Sum = 1633

Temp = 16 /10
Temp = 1

Fourth Iteration
Temp = 1 and Sum = 1633

Reminder = Temp %10
Reminder = 1 % 10 = 0

Sum = 1633 + (1 * 1 * 1 * 1)
Sum = 1633 + 1 => 1634

Temp = 1/10
Temp = 0

Here, Number = 0. So, the while loop condition in this c Armstrong Number program will fail

if ( Number == Sum ) – Condition will check whether the user enter number is exactly equal to Sum number or not. This condition is True, and it is Armstrong, else the given number is not Armstrong number.

if ( Number == Sum ) => if(1634 == 1634) –TRUE. So, Number is an Armstrong Number

NOTE: If you are finding the Armstrong number below 1000, remove the while loop to count the number of digits in a number. Next, replace the below code

Sum = Sum + pow(Reminder, Times);

With

Sum = Sum + (Reminder * Reminder * Reminder)

C Program for Armstrong Number using For Loop

This program for Armstrong number in c allows the user to enter any positive integer. And then, this program will check whether a number is Armstrong Number or Not using For Loop

/* Armstrong Number in C using For loop */

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

int main()
{
  int Number, Temp, Reminder, Times =0, Sum = 0;
 
  printf("\nPlease Enter any number to Check for Armstrong \n");
  scanf("%d", &Number);
  
  Temp = Number;
  
  while (Temp != 0) 
   {
     Times = Times + 1;
     Temp = Temp / 10;
   }
   
   for(Temp = Number; Temp > 0; Temp =  Temp /10 )
   {
     Reminder = Temp % 10;
     Sum = Sum + pow(Reminder, Times);
   }
 
  printf("\nSum of entered number is = %d\n", Sum);

  if ( Number == Sum )
      printf("\n%d is Armstrong Number.\n", Number);
  else
      printf("%d is not the Armstrong Number.\n", Number);
 
  return 0
}

We just replaced the While loop in the above Armstrong Number in c example with the For loop. If you don’t understand the for loop in this program, please refer For Loop article here: For Loop in C Programming

C Program For Armstrong Number Using For Loop

Program for Armstrong Number in C using Functions

This C Armstrong Number program allows the user to enter any positive integer. Next, this program checks whether the number is Armstrong Number or Not using Functions

/* Armstrong Number in C using Functions*/

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

int Check_Armstrong (int);

int main()
{
  int Number, Sum = 0;
 
  printf("\nPlease Enter any number to Check for Armstrong \n");
  scanf("%d", &Number);

  Sum = Check_Armstrong (Number);
  printf("Sum of entered number is = %d\n", Sum);

  if ( Number == Sum )
      printf("\n%d is Armstrong Number.\n", Number);
  else
      printf("%d is not Armstrong Number.\n", Number);
 
  return 0;
}

int Check_Armstrong (int Number)
{
  int Temp, Reminder, Times =0, Sum = 0;
  
  Temp = Number;
  
  while (Temp != 0) 
   {
     Times = Times + 1;
     Temp = Temp / 10;
   }
  
  for(Temp = Number; Temp > 0; Temp =  Temp /10 )
   {
     Reminder = Temp %10;
     Sum = Sum + pow(Reminder, Times);
   }
  return Sum;
}
C Program For Armstrong Number Using Functions

Within this Armstrong Number in c program, when the compiler reaches to

Sum = Check_Armstrong (Number); line in the main() program then the compiler will immediately jump to below function:

int Check_Armstrong (int Number)

We already explained the LOGIC of this Armstrong Number program in the above example.

NOTE: If we create a function with Void, then there is no need to return any value. However, if we declared a function with any data type (int, float, etc.), then we have to return something out from the function.

Armstrong Number in C using Recursion program

This program for Armstrong Number allows you to enter any positive integer. Next, this C program will check whether a number is Armstrong Number or Not using Recursion concept.

/* Armstrong Number in C using Recursion */

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

int Check_Armstrong (int, int);

int main()
{
  int Number, Sum = 0, Times =0,Temp;
 
  printf("\nPlease Enter number to Check for Armstrong \n");
  scanf("%d", &Number);
  
  Temp = Number;
  while (Temp != 0) 
   {
     Times = Times + 1;
     Temp = Temp / 10;
   }

  Sum = Check_Armstrong (Number, Times);
  printf("Sum of entered number is = %d\n", Sum);

  if ( Number == Sum )
      printf("\n%d is Armstrong Number.\n", Number);
  else
      printf("%d is not the Armstrong Number.\n", Number);
 
  return 0;
}

int Check_Armstrong (int Number, int Times)
{
  static int Reminder, Sum = 0;
  
  if (Number > 0)
   {
     Reminder = Number %10;
     Sum = Sum + pow(Reminder, Times);
     Check_Armstrong (Number /10, Times);
     return Sum;
   }
   else
     return 0;
}
C Program For Armstrong Number Using Recursion

In this Armstrong Number program, we used the below statement inside a function,

Armstrong_Check (Number /10);

This statement will help to call the function Recursively with the updated value. If you miss this statement, after completing the first line, it will terminate.

For example, Number = 153 then the output will be 27

Let’s see the If condition of this C Armstrong Number

if (Number > 0) will check whether the number is greater than 0 or not. For Recursive functions, it is very important to place a condition before using the function recursively. Otherwise, we will end up in infinite execution (Same like Infinite Loop).

Please be careful :)

Armstrong Numbers in C between 1 to 1000 (or n)

This C Armstrong Number program allows you to enter a minimum and maximum values. This program finds Armstrong Numbers between the Minimum and Maximum values.

/* Armstrong Number in C between 1 to n */

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

int Check_Armstrong (int);

int main()
{
  int Number,Reminder,Reverse,Temp, Sum;
  int Minimum,Maximum;

  printf("\nPlease Enter the Minimum & Maximum Values\n");
  scanf("%d %d",&Minimum, &Maximum);

  printf("Armstrong Numbers Between %d and %d are:\n",Minimum, Maximum);
  for(Number = Minimum; Number <= Maximum; Number++)
   {
     Sum = Check_Armstrong (Number);
   
     if(Number == Sum)
        printf("%d ",Number);
   }
 
  return 0;
}

int Check_Armstrong (int Number)
{
  int Temp, Reminder, Times =0, Sum = 0;
  
  Temp = Number;
  
  while (Temp != 0) 
   {
     Times = Times + 1;
     Temp = Temp / 10;
   }
  
  for(Temp = Number; Temp > 0; Temp =  Temp /10 )
   {
     Reminder = Temp %10;
     Sum = Sum + pow(Reminder, Times);
   }
  
return Sum;
}
C Program For Armstrong Number Between 1 to n

This Armstrong Number in c program allows the user to enter a minimum and maximum values.

for (Number=Minimum; Number<=Maximum; Number++)

The For Loop helps the compiler to iterate between Minimum and Maximum Variables. Iteration starts at the Minimum, and then it will not exceed the Maximum variable.

if(Number==Sum) -– condition will check whether the iteration number is exactly equal to the Reverse number or not. And, if this condition is True, then it is Armstrong else the given number is not Armstrong number.

If this condition is True then, below statement will print

printf("%d ",Number);

Filed Under: C Programs

  • C Hello World
  • C Add Two numbers
  • C Armstrong Number Program
  • C Convert Celsius to Fahrenheit
  • C Convert Decimal to Binary
  • C Convert Decimal to Octal
  • C Convert CM to Meter and KM
  • C Convert KM to M, CM & MM
  • C Convert Fahrenheit to Celsius
  • C Compound Interest Program
  • C Count Digits in a Number
  • C Count Notes in an Amount
  • C Cube of a Number
  • C Electricity Bill Program
  • C Factors of a Number Program
  • C Factorial of a Number Program
  • C GCD of Two Numbers
  • C Generic Root of a Number
  • C Gross Salary of an Employee
  • C Largest of Two Numbers
  • C Largest of Three numbers
  • C Last Digit Of a Number
  • C LCM of Two Numbers
  • C Leap Year program
  • C Multiplication Table program
  • C Natural Numbers from 1 to N
  • C Natural Numbers in reverse
  • C NCR Factorial of a Number
  • C Number is Divisible by 5 and 11
  • C Palindrome Program
  • C Palindrome String program
  • C Perfect Number Program
  • C Profit or Loss Program
  • C Print 1 to 100
  • C Print Prime Numbers 1 to 100
  • C Prime Number program
  • C Prime Factors of a Number
  • C Prime, Armstrong or Perfect
  • C Positive or Negative program
  • C Print Odd Numbers 1 to N
  • C Print Even Numbers 1 to N
  • C Print Integer, Char & Float
  • C Power of a Number program
  • C Product of Digits in a Number
  • C Roots of a Quadratic Equation
  • C Reverse a Number
  • C Simple Calculator Program
  • C Simple Interest
  • C Square of a Number
  • C Square Root of a Number
  • C Standard Deviation program
  • C Strong Number Program
  • C Student Grade program
  • C Sum of Odd Numbers program
  • C Sum of Even Nums program
  • C Sum of Even and Odd Nums
  • C Swap First & Last digit of Num
  • C Sum of First & Last digit Num
  • C Sum of Digits of a Number
  • C Sum of N Numbers Program
  • C Sum and Average of n Number
  • C Swap 2 Numbers
  • C Total, Avg & % of Five Subjects
  • C Quick Sort
  • C Selection Sort
  • C Insertion Sort
  • C Bubble Sort
  • C Fibonacci Series Program
  • C Nth Fibonacci number
  • C Sum of AP Series
  • C Sum of GP Series
  • C Sum of series 1³+2³+3³+….+n³
  • C Sum of series 1²+2²+3²+….+n²
  • C ASCII value of String chars
  • C Print Characters in a String
  • C Compare Two Strings
  • C Concatenate Two Strings
  • C Copy String
  • C String length program
  • C Convert String to Lowercase
  • C First Occurrence of string char
  • C First Occur of String Word
  • C Count Occur of string Char
  • C Count Alphabets, Digits & Special Chars
  • C Count Vowels & Consonants
  • C string remove all Occ of char
  • C remove 1st Occ of char in string
  • C remove last Occ char in string
  • C replace All Occ of char in string
  • C Replace last Occ of String Char
  • C Replace 1st occ char in string
  • C Reverse words Order in string
  • C Reverse a String
  • C Toggle Case of all char in string
  • C Array Arithmetic Operations
  • C Matrix Arithmetic Operations
  • C Count Frequency of array item
  • C Count Duplicate Array items
  • C Count +ve & -Ve Array items
  • C Count Even & Odd Array items
  • C Copy an Array to another
  • C Delete Duplicate Array items
  • C Delete an Element in an Array
  • C insert an Element in an Array
  • C 2nd largest Number in an Array
  • C Find Largest Array Number
  • C Find Smallest Array Number
  • C Largest & Smallest Array item
  • C Merge Two Arrays
  • C Print Unique Array Elements
  • C Print Arrayy Elements
  • C Print Negative Array Numbers
  • C Print Positive Array Numbers
  • C Put +ve & -Ve in 2 Arrays
  • C Put Even & Odd in 2 Arrays
  • C Program to Reverse an Array
  • C Search an Element in an Array
  • C Sum of array even, odd nums
  • C Sort Array in Ascending Order
  • C Sort Array in Descending
  • C Swap 2 Arrays without Temp
  • C Sum of all Array Elements
  • C Sum of each Matrix column
  • C sum of each row in a Matrix
  • C Sum of Matrix row & column
  • C Add Two Matrices
  • C Sparse Matrix
  • C Symmetric Matrix
  • C Identity Matrix
  • C Interchange Matrix Diagonals
  • C Check two Matrices Equal
  • C Lower Triangle of a Matrix
  • C Upper Triangle of a Matrix
  • C Sum of Lower Triangle Matrix
  • C Sum of Upper Triangle Matrix
  • C Subtract Two Matrices
  • C Transpose of a Matrix program

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy