- Armstrong number is a number which order of n if
- xyz=x n + y n + z n;
- To check three digit Armstrong number in c programming
- 153=13 * 53*33
- In the same way we can also check four digit Armstrong number
- 1634=14+64+34+44 is Armstrong number.
- Now let us check a four digit number is armstrong or not by using recursive function in c.
- Armstrong number in c using recursion function.
- Four digit armstrong number in c. check below program for four digit or n digit armstrong number in c programming.
Program #1: Write a c program to check four / 4 digit number or N digit number is armstrong number or not by using recursive function
- #include <stdio.h>
- #include <stdlib.h>
- int powerfun(int, int);
- int main()
- {
- int n, sum = 0, temp, remainder, digits = 0;
- printf("Input number to check Armstrong or not");
- scanf("%d", &n);
- temp = n;
- // check the number of digits in given number
- while (temp != 0) {
- digits++;
- temp = temp/10;
- }
- temp = n;
- while (temp != 0) {
- remainder = temp%10;
- sum = sum + powerfun(remainder, digits);
- temp = temp/10;
- }
- if (n == sum)
- printf("%d is an Armstrong number.\n", n);
- else
- printf("%d is not an Armstrong number.\n", n);
- getch();
- }
- int powerfun(int num, int r) {
- int c, res = 1;
- for (c = 1; c <= r; c++)
- res = res*num;
- return res;
- }
Output:
No comments