adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to check a given number is Armstrong or not Using recursive function


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


Post a Comment

0 Comments