Armstrong number in C using for loop-contents4you

 C Program to Check Armstrong Number

armstrong
In this example, you will learn to check whether an integer entered by the user is an Armstrong number or not.

A positive integer is called an Armstrong number (of order n) if

abcd... = an + bn + cn + dn + 

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because

153 = 1*1*1 + 5*5*5 + 3*3*3 

Check Armstrong Number of three digits

#include <stdio.h>
int main() {
    int num, originalNum, remainder, result = 0;
    printf("Enter a three-digit integer: ");
    scanf("%d", &num);
    originalNum = num;

    while (originalNum != 0) {
       // remainder contains the last digit
        remainder = originalNum % 10;
        
       result += remainder * remainder * remainder;
        
       // removing last digit from the orignal number
       originalNum /= 10;
    }

    if (result == num)
        printf("%d is an Armstrong number.", num);
    else
        printf("%d is not an Armstrong number.", num);

    return 0;
}


Output

Enter a three-digit integer: 371
371 is an Armstrong number.














In this program, the number of digits of an integer is calculated first and stored in n. And, the pow() function is used to compute the power of individual digits in each iteration of the second for loop.

Armstrong Number in C

An Armstrong number of a three-digit number is a number in which the sum of the cube of the digits is equal to the number itself.

Consider the example:

153 is an Armstrong number

So, 1*1*1+5*5*5+3*3*3=1+125+27=153

Hence 153 is an Armstrong number.

***********************************************************************************

एसी ही नया टेक्नोलॉजी ,Programming Language , Coding , C Language, C++, से रिलेटेड जानकारियाँ पाने के लिए हमारे इस वेबसाइट www.contents4you.com को सब्सक्राइब कर दीजिए | जिससे हमारी आने वाली नई पोस्ट की सूचनाएं जल्दी प्राप्त होगी |

अगर आपको Post पसंद आये  है तो अपने  friends और  social media पर share करे.