C Program to Check "Armstrong Number"
C program to checking the number is Armstrong or is not Armstrong, here we share complete details about the C program for Armstrong number.
If you are finding a solution to those questions...
1. Write a C program to check Armstrong's Number of three digits? OR
C program to check given number is Armstrong or Not?
2. Write a C program to check whether a number is Armstrong's Number or Not?
3. Write a C program to find first to n numbers of Armstrong Numbers?
4. Write a C program to find between of given numbers for Armstrong Numbers?
So, Just scroll down and read this article.
Before writing the c program to check whether the number is Armstrong or not, let's first understand "what is Armstrong number?".
Definition of Armstrong Number
" An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself."
The Armstrong number is a special number. For finding this number you should follow one mathematic process. Which written below.
1. Count a total number of digits of 'Given number'.
2. Multiply each number by itself as the total number of digits.
3. Addition the answer to each multiplication.
4. If your final answer and your given number both are same then those number is Armstrong number else it is not Armstrong.
Now let, see a C program for "Armstrong Numbers".
C program to check Armstrong's Number of three digits
Here, The C program to check number is Armstrong or not. This program checks only for Three-digit numbers. If you enter four or more digit numbers so it not work.
So also see below the C program that checks the three-digit Armstrong number.
#include <stdio.h> int main() { int num, originalNum, remainder, result = 0; printf("Enter a three-digit number: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { // remainder contains the last digit remainder = originalNum % 10; // Multiplication of Three Digit result += remainder * remainder * remainder; // Multiplication of Two Digit // rwsult += remainder * remainder; // removing last digit from the orignal number originalNum /= 10; } // checking Number is Armstrong or not if (result == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; }
OUTPUT :
Enter a three-digit number: 153
153 is an Armstrong number. code-box
C program to check given number is Armstrong or Not
Sometimes you required to check any number is Armstrong or not. Then you can use the below C-program. In this Program, we just enter one loop that counting the number of digits and we can use it directly in calculation.
Here, We use the "POW" function, The pow function use for performing power calculation, and It library file is "<math.h>".
Mostly this C program is use for checking Armstrong Number.
Mostly this C program is use for checking Armstrong Number.
#include<stdio.h> #include<math.h> int main() { int num1,num2,num3,count=0,reminder,i,check=0; // gething Number printf("Enter Your Number = "); scanf("%d",&num1); num2=num1; // For Calculat Armstrong Number num3=num1; // For Find Number of Digit // Counting Number of Digit by While loop while(num3 != 0) { count++; num3/=10; } // Counting Number of Digit by for loop // for(i=0;i<=num3;count++); // Calculating The Armstrong number for(i=0;i<=count;i++) { reminder=num2%10; check+=pow(reminder,count); num2/=10; } // Checking the number is Armstrong or Not if( num1 == check) { printf(" %d is an Armstrong Number",num1); } else { printf(" %d is not an Armstrong Number",num1); } return 0; }
OUTPUT :
Enter your number: 8208
8208 is an Armstrong number.
Enter Your Number: 1000
1000 is not an Armstrong Number code-box
C program to find n number of Armstrong Number
For this program, we get value for finding a number of Armstrong Number.
" For example, If the user enters 5, then the program returns the first 5 Armstrong number. "
Here, We have written a C Program to display n number of Armstrong Number.
#include <stdio.h>
#include <math.h>
int main()
{
int num, lastDigit, digits, sum, i, end;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &end);
printf("Armstrong number between 1 to %d are: \n", end);
for(i=1; i<=end; i++)
{
sum = 0;
/* Copy the value of num for processing */
num = i;
/* Find total digits in num */
digits = (int) log10(num) + 1;
/* Calculate sum of power of digits */
while(num > 0)
{
/* Extract last digit */
lastDigit = num % 10;
// Find sum of power of digits
// Use ceil() function to overcome any rounding errors by pow()
sum = sum + ceil(pow(lastDigit, digits));
/* Remove the last digit */
num = num / 10;
}
/* Check for Armstrong number */
if(i == sum)
{
printf("%d, ", i);
}
}
return 0;
}
OUTPUT :
Enter upper limit : 10
Armstrong number between 1 to 10 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153 code-box
C program to find between of given numbers for Armstrong Numbers
If you went to display Armstrong's number from a particular range like...
" Display Armstrong number that between 100 to 300 "
It's possible by a simple change of the upper c program. You just customize the first value which we set by default 1.
we mention in the below C program that for Displaying Armstrong numbers from the given range.
#include <stdio.h>
#include <math.h>
int main()
{
int num, lastDigit, digits, sum, i;
int start, end;
/* Input lower and upper limit from user */
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);
printf("Armstrong number between %d to %d are: \n", start, end);
for(i=start; i<=end; i++)
{
sum = 0;
/* Copy the value of num for processing */
num = i;
/* Find total digits in num */
digits = (int) log10(num) + 1;
/* Calculate sum of power of digits */
while(num > 0)
{
/* Extract the last digit */
lastDigit = num % 10;
// Find sum of power of digits
// Use ceil() function to overcome any rounding errors by pow()
sum = sum + ceil(pow(lastDigit, digits));
/* Remove the last digit */
num = num / 10;
}
/* Check for Armstrong number */
if(i == sum)
{
printf("%d, ", i);
}
}
return 0;
}
OUTPUT :
Enter lower limit : 1
Enter upper limit : 1000
Armstrong number between 100 to 1000 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153,370,371,407 code-box
Those are the C program for finding/displaying Armstrong numbers.
If you have any questions or doubts regarding those codes or any other program, so just write in the comment box.
We will solve your doubts or problem.
" Keep Learning & Keep Programming "
Post a Comment