Home C Language Basic C Programs Armstrong number in C

Armstrong number in C

## Write a C Program to Find Whether the Given Number is Armstrong Number or not.

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

EX : 371 is an Armstrong number.

(3^3) + (7^3) + (1^3) = 371.

### Code for check three digit Armstrong number in c

#include <stdio.h>

int main()

{

int num, temp, digit, sum = 0;

printf(“Enter a three-digit number: “);

scanf(“%d”, &num);

temp = num;

while (temp != 0)

{

// picking last digit from temp

digit = temp % 10;

sum += digit * digit * digit;

// removing last digit from the temp number

temp /= 10;

}

if (sum == num)

printf(“%d is an Armstrong number.”, num);

else

printf(“%d is not an Armstrong number.”, num);

return 0;

}

Output:

i) Enter a three-digit number: 153

Given number is Armstrong.

ii) Enter a three-digit number: 516

Given number is not Armstrong.

### Code for check ‘n’ digit Armstrong number in c

#include<stdio.h>

#include<math.h>

int main()

{

long int n,s=0;

int a,t,d;

printf(“Enter a value : “);

scanf(“%ld”,&n); // Read the number.

t=n;

d=0; //here we calculate no,of digits of the given number

while(t>0)

{

d++;

t=t/10;

}

t=n;

while(t>0) //If entered number is greater than zero then only loop will execute

{

a=t%10; //pick last digit from temporary variable.

s=s+pow(a,d); //adding cube ‘a’ to ‘s’.

t=t/10; //removing last digit from ‘t’

}

if(n==s)

printf(“Given number is Armstrong\n”);

else

printf(“Given number is not Armstrong\n”);

return 0;

}

Output:

i) Enter a value : 1634

Given number is Armstrong.

ii) Enter a value : 632

Given number is not Armstrong.

For improvement of your coding skills we give some tasks along this. If you solve this tasks and send to our email
(onlineexamshubteam@gmail.com) with your details. we will display your details(like Name, City, college, photo) in our site.

Task: Write a C program to generate Armstrong numbers from ‘m’ to ‘n’ natural numbers. For example user entered 250 & 500 then output will be : 370 371 407