In this Armstrong Number in C tutorial, we will write a C program for armstrong number.
An Armstrong number or Narcissistic number is an n-digit number equivalent to the sum of digits raised to the nth power of digits from the number. A few Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc.
Syntax for Armstrong number
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Let’s try to understand why 1634 is an Armstrong number.
Let’s example it using mathematical computation.
Example: 1634
1634 = (1)4 + (6)4 + (3)4 + (4)4
= 1 + 1296 + 81 + 256
= 1634
Pictorial Presentation:
Algorithm to check given number is Armstrong number or not
START
Step 1 → Take integer variable num.
Step 2 → Assign (num) value to the (temp) variable.
Step 3 → Split all digits of num by dividing it to base value 10.
Step 4 → Find the nth power of each digit.
Step 5 → Add all digits values together.
Step 6 → If Sum equivalent to num print, It is an Armstrong number.
Step 7 → If Sum not equivalent to num print, It is not an Armstrong Number.
STOP
Pseudocode for Armstrong number
Below is the procedure for armstrong number in C
temp = num rem = 0 WHILE temp IS NOT 0 rem ← temp modulo 10 sum ← sum + power(rem, digits); divide temp by 10 END WHILE IF sum equivalent to num PRINT Armstrong Number ELSE PRINT not an Armstrong Number END IF end procedure
C Program for Armstrong Number
The implementation of the algorithm provides below. It is possible to alter the value of the num variable and implement and assess your program. We compute the number of digits within our program and calculate the sum of individual digits raise to the power number of digits. Whether this sum equals the input number, then the number will be the Armstrong number differently not.
/** * C program to check Armstrong number */ #include<stdio.h> int find_arm(int); int power(int, int); int main () { int num; printf("Enter any Number to Check its Armstrong Number or not :"); scanf("%d", &num); if (find_arm(num) == 1) printf("%d is an Armstrong number.\n", num); else printf("%d isn't an Armstrong number.\n", num); return 0; } int find_arm(int num) { int sum = 0, temp; int rem, digits = 0; temp = num; while (temp != 0) { digits++; temp = temp/10; } temp = num; while (temp != 0) { rem = temp%10; sum = sum + power(rem, digits); temp = temp/10; } if (num == sum) return 1; else return 0; } int power(int num, int r) { int loop; int prime = 1; for (loop = 1; loop <= r; loop=loop+1) prime = prime * num; return prime; }
Output of above program
How it works:
This table Shows Exactly What Occurs at each iteration of the while loop (assuming the num = 1634):
Iteration | rem | sum | num |
After 1st iteration | rem = 1634%10 = 4 | sum = 0 + (4)4 = 256 | num = 1634 / 10 = 163 |
After 2st iteration | rem = 163%10 = 3 | sum = 256 + (3)4 = 337 | num = 163 / 10 = 16 |
After 3st iteration | rem = 16%10 = 6 | sum = 337 + (6)4 = 1633 | num = 16 / 10 = 1 |
After 4st iteration | rem = 1%10 = 1 | sum = 1633 + (1)4 = 1634 | num = 1 / 10 = 0 |
To print out all armstrong numbers between 1 and 1000.
#include<stdio.h> #include<conio.h> void main() { int c,t,temp,sum,num; printf("Armstrong numbers between 1 and 1000 : \n"); printf ("Numbers like = cube(digit1) + cube(digit2) + cube(digit3) \n"); printf("l53 = cube(1) + cube(5) + cube(3)"); for (num=1; num<=1000; num++) { sum = 0; temp = num; while(temp>0) { t = temp%10; sum+= (t*t*t); temp = temp/10; } if (sum==num) { printf ("\nNumber %d is armstrong number", num); } } getch(); }
OUTPUT:
Explanation: In this program, we have to find all Armstrong numbers between 1 and 1000. For this, we firstly initialize (sum = 0) and temp = num = 1. Then we apply modulus operator(%) and various other operations to calculate new sum = sum + (t*t*t);. When we exit from for loop, we check whether the sum and number num is equal. If they are equal, then it is Armstrong’s number, and the value is printed.