Program to check whether a number is an armstrong number or not in C

Logic:

  • Armstrong Number A number is an armstrong number if the sum of cubes of its digits is equal to the number itself.
  • Enter the number n
  • Example 1: n=153
  • Digits= 1, 5, 3
  • sum of cubes of digits =1 + 125 + 27 =153=number itself
  • So It is an armstrong number
  • Example 2: n=135
  • Digits= 1, 3, 5
  • sum of cubes of digits =1 + 27 + 125 =153 is not equal to number i.e. 135
  • So It is not an armstrong number

Algorithm

  1. Enter the number as n
  2. no = n;
  3. while (n > 0)
  4. {
  5. r = n % 10;
  6. s += r * r * r;
  7. n /= 10;
  8. }
  9. if (s == no)
  10. printf(“It is armstrong no.”);
  11. else
  12. printf(“It is not armstrong no.”);

Program

#include <stdio.h>

int main()
{
    printf("************************************************************");
    printf("\n************************************************************");
    printf("\n**     WAP to check a number for armstrong                 *");
    printf("\n**           Created by Sheetal Garg                      **");
    printf("\n**             Assistant Professor                        **");
    printf("\n**             Phone No:9467863365                        **");
    printf("\n************************************************************");
    printf("\n************************************************************\n");
    int n, no, i, s = 0, r, sum;
    printf("enter number");
    scanf("%d", &n);
    no = n;
    while (n > 0)
    {
        r = n % 10;
        s += r * r * r;
        n /= 10;
    }
    if (s == no)
        printf("It is armstrong no.");
    else
        printf("It is not armstrong no.");
    return 0;
}

Output 1

***************************************************************************
***************************************************************************
**                    WAP to check a number for armstrong                 *
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
enter number
153
It is armstrong no.

Output 2

***************************************************************************
***************************************************************************
**                    WAP to check a number for armstrong                 *
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
enter number
135
It is not armstrong no.
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.