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 digi(long int);

int main()

{

long int n,s=0;

int a,t,d;

printf(“Enter a value : “);

t=n;

d=digi(n); //Returns No.of digits in given integer.

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;

}

int digi(long int t)

{

int dig=0;

while(t>0)

{

dig++;

t=t/10;

}

return dig;

}

Output:

i) Enter a value : 1634

Given number is Armstrong.

ii) Enter a value : 632

Given number is not Armstrong.