How To Ideas | How To Articles | How To Tutorials


0

How to check whether a number is Armstrong Number in C


Armstrong Numbers are those numbers which are equal to the sum of its own digits each raised to the power of the number of digits in the number. Consider number 153, it has three digits, so length is 3.
Now each digits raised to the power of the number of digits in the number :  13 = 1, 53 = 125, 33 = 27.
Sum of these values : 1 + 125 + 27 = 153. So, sum is equal to the number itself, so 153 is an Armstrong Number.

Armstrong Numbers are also known as Narcissistic Numbers, Pluperfect Digital Invariant (PPDI) and Plus Perfect Numbers. You can learn more about Armstrong Numbers on Wikipedia.

Algorithm:

  1. Let num is the number which is to be checked whether it’s an Armstrong Number or not.
  2. Let af (additive factor),sum and temp be three temporary variables, sum having value 0 and temp having value equal to variable num initially.
  3. Check if num is less than zero or not, if yes, print an error message that only positive numbers are allowed and exit the program. Otherwise continue to next step.
  4. Find the length of the number i.e. how many digits it has and store it in variable length.
  5. Now check if temp is equal to zero or not. If yes, proceed to 9th step, else continue to next step.
  6. Fetch rightmost digit of temp and store it in variable af.
  7. Calculate aflength i.e. digit raised to power of the number of digits in number and store in af itself.
  8. Add af to sum and remove rightmost digit from temp variable by dividing it with 10 and move to step 5.
  9. Check if variable sum is equal to actual number num. If yes, then num is an Armstrong Number, else it is not.
  10. Quit the program.

Instructions:

  1. We only need a single header file for this program to work.
    #include <stdio.h>
  2. Now in main function ask the user to enter the number he/she wants to check. Check if it is positive number or not. If not, then exit the program.
    int num, temp;
    printf("Enter the number which you want to check : ");
    scanf("%d", &num);
    if(num < 0)
    {
        printf("Number entered is not valid positive number");
        return 0;
    }
  3. Now call user defined function isArmstrong (explained in next step) to calculate sum of its digits each raised to power of number of digits in the number. After than check if value returned by isArmstrong function is equal to actual number or not. If yes, then number entered by user is Armstrong Number, otherwise it is not. Print desired message and quit the program.
    temp = isArmstrong(num);
    if(temp == num)
        printf("%d is an Armstrong number...", num);
    else
        printf("%d is not an Armstrong number...", num);
  4. In isArmstrong function, find sum of its digits each raised to power of number of digits in the number using a for loop. Before that we have to find number of digits in the number as well. Finally return the sum calculated.
    int isArmstrong(int num)
    {
        int temp, temp2, af, length = 0, sum = 0;
        temp = num;
        while(temp)
        {
            length++;
            temp /= 10;
        }
        for(temp = num; temp; temp /= 10)
        {
            af = temp % 10;
            for(temp2 = 1; temp2 < length; temp2++)
                af *= temp % 10;
            sum += af;
        }
        return sum;
    }

Here are some screenshots of the working sample program.

Check Armstrong Number - 4

Check Armstrong Number - 4

Check Armstrong Number - 100

Check Armstrong Number - 100

Check Armstrong Number - 153

Check Armstrong Number - 153

Check Armstrong Number - 371

Check Armstrong Number - 371

You can download the sample program as well from here.

Incoming search terms:

  • how to add id tag to notepad (2)
  • algorithm for armstrong number in unix (1)
  • greatest common factor of 1784 (1)
Filed in: C Tags: , , , , , , ,

Leave a Reply

Submit Comment



© 6759 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.