ADVERTISEMENT
Number Is Armstrong or not
This program extracts the digits of a given number, calculates the sum of the cubes of these digits, and checks if this sum is equal to the original number. If it is equal, the number is considered an Armstrong number; otherwise, it’s not.
#include<stdio.h>
#include<math.h>
main()
{
int num,i,j,temp,sum=0;;
printf("Enter a number to know whether it is armstrong or not\n");
scanf("%d",&num);
temp=num;
while(num>0)
{
i=num%10;
sum+=i*i*i;
num=num/10;
}
if(sum==temp)
{
printf("Giveb number %d is an armstrong number\n",temp);
}
else
{
printf("Giveb number %d is not an armstrong number\nsince the sum of cubes of individual digits is %d\n",temp,sum);
}
}
C Programming ExamplesOUTPUT
Enter a number to know whether it is armstrong or not
375
Giveb number 375 is not an armstrong number
since the sum of cubes of individual digits is 495C Programming ExamplesExplanation:
This C program is designed to determine whether a given number is an Armstrong number. An Armstrong number (also known as a narcissistic number or pluperfect digital invariant) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. Let’s go through the code step by step:
Header File:
- The program includes the standard C library header <stdio.h> for input and output operations.
Main Function:
- The main function is the entry point of the program.
User Input:
- The program starts by asking the user to input a number, which is stored in the variable num.
Variable Initialization:
Several variables are declared and initialized:
- temp: It’s used to store the original number, so it’s initialized with the value of num.
- sum: It’s used to calculate the sum of the cubes of individual digits, and it’s initialized to 0.
Digit Extraction and Armstrong Number Check:
- The program enters a while loop that continues as long as num is greater than 0.
- Inside the loop, the program calculates the last digit of num by taking the remainder when dividing num by 10 (i.e., i = num % 10).
- The cube of the extracted digit is added to the sum variable.
- The last digit is removed from num by dividing it by 10 (i.e., num = num / 10).
Armstrong Number Check:
- After the while loop, the program checks if sum is equal to the original number temp.
- If sum equals temp, it means the given number is an Armstrong number, and it prints a message indicating that.
- If sum is not equal to temp, it means the given number is not an Armstrong number, and it prints a message stating the sum of the cubes of individual digits.