C program to find the Armstrong Number between intervals - wordsclank.in

Saturday, 10 March 2018

C program to find the Armstrong Number between intervals

A  number of n digit is called Armstrong number if sum of the power of n of its digits is equal to the number itself.
For example a number of three digits integer is an Armstrong number if the sum of the cubes of its digits is equal to the number itself. 153 is an Armstrong number since 1*1*1 + 5*5*5 + 3*3*3 = 371. 
armstrong number,armstrong number in C


Most Related:

PROBLEM STATEMENT

Write a program in C to find the Armstrong Number between intervals.




SOURCE CODE


 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
#include
#include

int main()
{
    int a, b, i, num1, num2, rem, n = 0, result = 0;

    printf("Enter the first interval: ");
    scanf("%d", &a);
    printf("Enter the last interval: ");
    scanf("%d", &b);
    printf("Armstrong numbers between %d and %d are: ", a, b);

    for(i = a + 1; i <= b; i++)
    {
        num1 = i;
        num2 = i;

        //checking number of digits
        while (num1 != 0)
        {
            num1 = num1 / 10;
            n++;
        }

        while (num2 != 0)
        {
            rem = num2 % 10;
            result = result + pow(rem, n);
            num2 = num2 / 10;
        }

        if (result == i) 
 {
            printf("%d ", i);
        }
        n = 0;
        // setting n as o for next iteration
        result = 0;

    }
    return 0;
}



OUTPUT

armstrong number,armstrong number in C

No comments:

Post a Comment