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
- Enter the number as 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.”);
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.