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

C Program
#include<iostream>
 using namespace std;
 int isArmstrong(int n)
  {
    if( n == 0)
        return 0;
    else
        return (((n % 10) * (n % 10) * (n % 10)) + isArmstrong(n / 10));
  }
  int main()
   {
     int n;
     cout<<"Enter a number :- ";
     cin>>n;
     if(isArmstrong(n) == n)
        cout<<n<<" is Armstrong number.";
     else
        cout<<n<<" is not Armstrong number.";
     return 0;
   }
C++ Program
#include<stdio.h>
 int isArmstrong(int n)
  {
    if( n == 0)
        return 0;
    else
        return (((n % 10) * (n % 10) * (n % 10)) + isArmstrong(n / 10));
  }
  int main()
   {
     int n;
     printf("Enter a number :- ");
     scanf("%d",&n);
     if(isArmstrong(n) == n)
        printf("%d is Armstrong number.",n);
     else
        printf("%d is not Armstrong number.",n);
     return 0;
   }
Output :-









Recursive function




Post a Comment

0 Comments