Program in C and C++ to check a given number is Armstrong or not Using recursive function
- Another method
C Program
#include<stdio.h> #include<math.h> int digits(int n){ int d = 0; while(n != 0) { d++; n /= 10; } return d; } int isArmstrong(int n, int d){ if( n == 0) return 0; return (pow((n % 10),d) + isArmstrong(n / 10, d)); } int main(){ int n; printf("Enter a number :- "); scanf("%d",&n); if(isArmstrong(n, digits(n)) == n) printf("%d is Armstrong number.",n); else printf("%d is not Armstrong number.",n); return 0; }
C++ Program
#include<iostream> #include<math.h> using namespace std; int digits(int n){ int d = 0; while(n != 0){ d++; n /= 10; } return d; } int isArmstrong(int n, int d){ if( n == 0) return 0; return (pow((n % 10),d) + isArmstrong(n / 10, d)); } int main(){ int n; cout<<"Enter a number :- "; cin>>n; if(isArmstrong(n, digits(n)) == n) cout<<n<<" is Armstrong number."; else cout<<n<<" is not Armstrong number."; return 0; }
Output :-
Related Programs
- Program in C and C++ to print reverse of a given number using recursive function
- Program in C and C++ to print reverse of a given string using recursive function
- Program in C and C++ to print Nth natural numbers in reverse order using recursive function
- Program in C and C++ to find sum of digits of a given number using recursive function
- Program in C and C++ to find sum of nth numbers Using recursive function
0 Comments