-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers4.c
More file actions
43 lines (40 loc) · 1023 Bytes
/
numbers4.c
File metadata and controls
43 lines (40 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Write a program that finds whether entered number is an armstrong or not.
#include<stdio.h>
#include<math.h>
void is_armstrong(int n);
int main()
{
int num;
printf("Enter a number :");
scanf("%d",&num);
is_armstrong(num);
}
void is_armstrong(int n)
{
int reminder,count=0,sum=0,i,num=n,num1=n;
// Find the numbers of digits of input number and store it in count.
while(n!=0)
{
reminder=n%10;
count++;
n/=10;
}
printf("This number has %d digits \n",count);
// Calculate the sum of the digits raised to the power of the number of digits.
for(i=0;i<count;i++)
{
reminder=num%10;
sum+=pow(reminder,count);
num/=10;
}
printf("Sum is %d \n",sum);
// Check if the sum of the digits is equal to the original input number.
if(sum==num1)
{
printf("Entered number is an armstrong number \n");
}
else
{
printf("Entered number is not an armstrong number \n");
}
}