C Program to Check Armstrong Number using Function | Recursion | For loop | While loop
C program to check Armstrong number using function, recursion, for loop, and while loop.
In this article, you will learn how to make a C program to check Armstrong number using function, recursion, for loop, and while loop.
Example-1
Example-2
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C Function
- C Recursion
- C For loop
- C While loop
- C Math library
What is Armstrong Number?
A positive integer is called an Armstrong number (of order n) if
In this article, we solve this problem in five methods:
- Using the for loop
- Using the function
- Using the recursion
- Using the while loop
- Without using pow() function
Source Code
// C Program to Check Armstrong Number using For loop
#include <stdio.h>
#include <math.h>
int main() {
int x, y, rem, n = 0;
float r = 0.0;
// x - To store the input number
// y - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
printf("-----Enter the integer number-----\n");
scanf("%d", &x);
y = x;
// store the number of digits of x in n
for (y = x; y != 0; ++n) {
y /= 10;
}
for (y = x; y != 0; y /= 10) {
rem = y % 10;
// store the sum of the power of individual digits in r
r += pow(rem, n);
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
printf("%d is an Armstrong number.\n", x);
} else {
printf("%d is not an Armstrong number.\n", x);
}
return 0;
}
Output
-----Enter the integer number-----
9474
9474 is an Armstrong number.
Explanation
In these given programs, we have taken input 9474
a positive integer number to check it is an Armstrong or not using the for
loop and while
loop statement.
We applied the Armstrong number formula in these loops to check the sum of its digits' multiplication with its length is equal to the given input value.
C Program to Check Armstrong Number using Function
// C Program to Check Armstrong Number using Function
#include <stdio.h>
#include <math.h>
// This function will check input number is armstrong or not
void CheckArmstrong(int x) {
int y, rem, n = 0;
float r = 0.0;
// y - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
y = x;
// store the number of digits of x in n
for (y = x; y != 0; ++n) {
y /= 10;
}
for (y = x; y != 0; y /= 10) {
rem = y % 10;
// store the sum of the power of individual digits in r
r += pow(rem, n);
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
printf("%d is an Armstrong number.\n", x);
} else {
printf("%d is not an Armstrong number.\n", x);
}
}
// It's the driver function
int main() {
int x; // To store the input number
printf("-----Enter the integer number-----\n");
scanf("%d", &x);
CheckArmstrong(x);
return 0;
}
C Program to Check Armstrong Number using Recursion
// C Program to Check Armstrong Number using Recursion
#include <stdio.h>
#include <math.h>
// This function will work recursive
float CheckArmstrong(int x, int size) {
if (x > 0)
return (pow( x%10, size) + CheckArmstrong(x/10, size));
}
// It's the driver function
int main() {
int x; // To store the input number
int y, size = 0;
printf("-----Enter the integer number-----\n");
scanf("%d", &x);
y = x;
for (y = x; y != 0; ++size) {
y /= 10;
}
if ((int) CheckArmstrong(x, size) == x) {
printf("%d is an Armstrong number.\n", x);
} else {
printf("%d is not an Armstrong number.\n", x);
}
return 0;
}
Output
-----Enter the integer number-----
153
153 is an Armstrong number.
C Program to Check Armstrong Number or Not using While loop
// C Program to Check Armstrong Number using While loop
#include <stdio.h>
#include <math.h>
int main() {
int x, y, z, rem, n = 0;
float r = 0.0;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
printf("-----Enter the integer number-----\n");
scanf("%d", &x);
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
while (z != 0) {
rem = z % 10;
r += pow(rem, n);
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
printf("%d is an Armstrong number.\n", x);
} else {
printf("%d is not an Armstrong number.\n", x);
}
return 0;
}
C Program to Check Armstrong Number without using pow() Function
// C Program to Check Armstrong Number without using pow() Function
#include <stdio.h>
int main() {
int x, y, z, rem = 1, rem2 = 1, n = 0, m = 0;
float r = 0.00;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem & rem2 - To store the reminder values
// r - To store the result variable
printf("-----Enter the integer number-----\n");
scanf("%d", &x);
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
m = n;
while (z != 0) {
rem = z % 10;
rem2 = 1;
while (n > 0) {
rem2 *= rem;
--n;
}
n = m;
r += rem2;
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
printf("%d is an Armstrong number.\n", x);
} else {
printf("%d is not an Armstrong number.\n", x);
}
return 0;
}
Output
-----Enter the integer number-----
154
154 is not an Armstrong number.
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student