Ticker

6/recent/ticker-posts

Armstrong Number in C

Program in C to check Armstrong Number

Armstrong Number: When a number is equal to the sum of the cube of each digit, then it is called an Armstrong Number.

Example: Number 153 is Armstrong number because:
  1             5            3
= 1*1*1 + 5*5*5 + 3*3*3
=    1     +  125   +   27
=   153 = Number, Hence it is Armstrong number.
The numbers 0, 1, 153, 370, 371 and 407 are some of the Armstrong numbers
s.

There are various ways to check whether a number is Armstrong or not. The General algorithm for the program is given below :

 Algorithm :


Step 1: Accept a number.
Step 2: Extract each digit and cube it and add it to a variable say sum.
Step 3
 If the sum is equal to a number then display that the number is Armstrong
else display that the number is not Armstrong.
Step 3: Stop

Code 1 :


#include < stdio.h >

int main()

{

    int numbernumberCopysum = 0;

    printf(" Enter number here : ");

    scanf("%d", &number);

    numberCopy = number;

    while (numberCopy > 0)
    {

        int lastDigit = numberCopy % 10;

        int cubeOfLastDigit = lastDigit * lastDigit * lastDigit;

        sum = sum + cubeOfLastDigit;

        numberCopy = numberCopy / 10;
    }

    if (sum == number)

        printf(" The given number %d is an Armstrong Number "number);

    else

        printf(" The given number %d is not an Armstrong Number "number);

    return 0;
}



Code 2 :


#include <stdio.h>
#include <math.h>
int main()
{
    int numbernsum = 0;
    printf(" Enter number here : ");
    scanf("%d", &number);
    n = number;
    while (n > 0)
    {
        sum = sum + pow(n % 103);
        n = n / 10;
    }
    if (sum == number)
        printf(" The given number %d is an Armstrong Number "number);
    else
        printf(" The given number %d is not an Armstrong Number "number);
    return 0;
}


If you have any doubt/questions related to Program, or if you want to give any suggestionsfeel free to comment below. Don't forget to subscribe and share it with friends.

Post a Comment

0 Comments