Thursday, April 2, 2020

c program to check given number is armstrong number or not

C program to check given number is armstrong number or not   

  • A Positive number is called Armstrong number.  

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

  • In this case of Armstrong number of 3 digits, the sum cubes of each digits is equal to the number of itself.

123 = 1*1*1 + 2*2*2 + 3*3*3
#include 
int main() 
{
    int num, originalNum, remainder, result = 0; // Integer values
    printf("Enter a three-digit integer: "); // Enter by user three digits
    scanf("%d", &num); // storing values
    originalNum = num; // replace number

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

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

    return 0;
}

Output

Enter three integer 371
371 is an Armstrong number
Happy coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!
Location: Pakistan

0 comments:

Post a Comment

Recent Posts