Check The Given Number Is An Armstrong Number Or Not




Source code :-

#include<stdio.h>
#include<math.h>
int main()
{
    int n,z,r,s,i;
    printf("Enter the number: ");
    scanf("%d",&n);
    z=n;
    i=0;
    while(z!=0)
    {
        i++;
        z=z/10;
    }
    z=n;
    s=0;
    while(z!=0)   
    {
        r=z%10;
        s=s+pow(r,i);
        z=z/10;  
    }
    if(s==n)
        printf("\nThe enter number %d is an armstrong number.\n",n);
    else
        printf("\nThe enter number %d is not an armstrong number.\n",n);
    return 1;
}

Input-Output :- 

abhi@hp-15q-laptop:~$ gcc armstrong.c -lm
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 145
The enter number 145 is not an armstrong number.

abhi@hp-15q-laptop:~$ gcc armstrong.c -lm
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 153
The enter number 153 is an armstrong number.

Print All The Armstrong Number Of The Given Range

Source code :

#include<stdio.h>
#include<math.h>
int main()
{
    int n,z,r,s,i,a,b;
    printf("Enter the lower limit of the range: ");
    scanf("%d",&a);
    printf("\nEnter the upper limit of the range: ");
    scanf("%d",&b);
    printf("\nArmstrong numbers are: \n\n");
    for(n=a;n<=b;n++)
    {
        z=n;
        i=0;
        while(z!=0)
        {
            i++;
            z=z/10;
        }
        z=n;
        s=0;
        while(z!=0)   
        {
            r=z%10;
            s=s+pow(r,i);
            z=z/10;
        }
        if(s==n)
        printf("%d ",n);
    }
    return 1;

Input-Output :

abhi@hp-15q-laptop:~$ gcc armstrong.c -lm
abhi@hp-15q-laptop:~$ ./a.out
Enter the upper limit of the range : 1
Enter the lower limit of the range : 200
Armstrong numbers are : 1 2 3 4 5 6 7 8 9 153

Post a Comment

أحدث أقدم