×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

Armstrong Number in C | A C Program to Find Armstrong Number

What is an Armstrong Number?

Armstrong Number is a Number which is equal to it’s sum of digit’s cube. For example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).

Checking Armstrong Number in C

To check Armstrong number, we have to calculate sum of each digit’s cube and then compare number is equal to Sum or not. If Number and Sum of digit’s cube then Number will be an Armstrong Number otherwise not.

C Program to Find Armstrong Number

/* C program to check whether a number is armstrong or not */
 
#include <stdio.h>
 
int main()
{
    int number, sum=0, rem=0,tempNumber;
 
    printf("Enter an integer number: ");
    scanf("%d", &number);
 
    tempNumber=number;
 
    while(tempNumber!=0)
    {
        rem=tempNumber%10;
        sum=sum + (rem*rem*rem);
        tempNumber/=10;
    }  
 
/* checking number is armstrong or not */ 
    if(sum==number)  
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);
 
    return 0;
}

Checking Armstrong number using user-defined function

/* C program to check whether a number is armstrong or not */
 
#include <stdio.h>
 
/*function to check Armstrong Number*/
int isArmstrong(int num)
{
    int tempNumber=num;
    int rem,sum;
     
    /*sum of digit's cube*/
    sum=0;
    while(tempNumber!=0)
    {
        rem=tempNumber%10;
        sum=sum + (rem*rem*rem);
        tempNumber/=10;
    }  
     
    if(sum==num)
        return 1;   /*Armstrong Number*/
    else
        return 0;   /*Not an Armstrong Number*/
}
 
int main()
{
  int number;
  
  printf("Enter an integer number: ");
  scanf("%d", &number);
  
    if(isArmstrong(number))
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);
  
  return 0;
}

Output

First run:
Enter an integer number: 153
153 is an Armstrong number.

Second run:
Enter an integer number: 167
167 is not an Armstrong number.

C Number Manipulation Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.