-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp-6-30.c
More file actions
58 lines (48 loc) · 1.29 KB
/
exp-6-30.c
File metadata and controls
58 lines (48 loc) · 1.29 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
> File Name: exp-6-30.c
> Author: xiaoxiaoh
> Mail: xiaoxxhao@gmail.com
> Created Time: Tue Jul 11 11:57:46 2017
************************************************************************/
/*
* Write a C program to check whether a number is Armstrong number or not.
*
* An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. Read more about Armstrong numbers. Below are few examples of Armstrong numbers:
* 6 = 6^1 = 6
* 371 = 3^3 + 7^3 + 1^3 = 371
*
* Example
*
* Input
* Input number: 371
*
* Output
* 371 is armstrong number
*
*/
#include <stdio.h>
#include <math.h> //used for log10(), pow() and round() functions
int main()
{
int num, newNum, digits, lastDigit, sum;
//Read a number from user
printf("Enter a number: ");
scanf("%d", &num);
newNum = num;
digits = (int)(log10(newNum)) + 1;
//Caculate sum of power of digits
while(newNum > 0)
{
//EXtract the last digit
lastDigit = newNum % 10;
//Clculate the sum
sum += pow(lastDigit, digits);
//Remove the last digit
newNum /= 10;
}
if(num == sum)
printf("The number %d is Armstrong number\n", num);
else
printf("The number %d is not Armstrong number\n", num);
return 0;
}