Armstrong Number between Intervals
C Program to print Armstrong Numbers
A number is said to be armstrong number of order n, if [ abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n)+….. ]. For example, lets consider 1634 = 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 . Hence, 1634 is an armstrong number. In this section, we will learn how to write a C program to print armstrong number between intervals.
Algorithm to check armstrong number
- Step 1: Start
- Step 2: Declare Variable sum, temp, num
- Step 3: Read num from User
- Step 4: Initialize Variable sum=0 and temp=num
- Step 5: Count the number of digits in the num given as n
- Step 6: While num != 0
let m=num%10
sum = sum + (m)^n
num = num/10 - Step 7: IF sum==temp
It is a Armstrong Number
ELSE
It is not a Armstrong Number - Step 8: Stop
There are two examples given below to print armstromg number between intervals:
Example 1:
Run
#include<stdio.h>
#include<conio.h>
int main ()
{
int s = 1, e = 500, num, n, arm = 0, i, sum;
for (i = s; i <= e; i++)
{
num = i;
sum = i;
while (num != 0)
{
n = num % 10;
arm = arm + pow (n, 3);
num = num / 10;
}
if (sum == arm)
{
printf ("%d\n", i);
}
arm = 0;
}
return 0;
}
Output:
1 153 370 371 407
Example 2:
Run
#include<stdio.h>
#include<conio.h>
int main ()
{
int s = 310, e = 900, num1, n, arm = 0, i, num2, c;
for (i = s; i <= e; i++)
{
num1 = i;
num2 = i;
while (num1 != 0)
{
num1 = num1 / 10;
++c;
}
while (num2 != 0)
{
n = num2 % 10;
arm = arm + (n * n * n);
num2 = num2 / 10;
}
if (arm == i)
{
printf ("%d\n", i);
}
arm = 0;
c = 0;
}
return 0;
}
Output:
370 371 407
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment