adsense

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

Program in C++ to check a number in Armstrong or not Using class

Program in C++ to check a number in Armstrong or not Using class


Using Function with default constructor
#include<iostream>
#include<conio.h>
#include<math.h>
 using namespace std;
  class Arm
   {
       int n;
       int isArmStrong(int n);
    public:
      Arm() {   n=1; }
      Arm(int n1){ n=n1; }
     ~Arm(){}
     void input()
     {
         cout<<"Input a number:-";
         cin>>n;
     }
     void show()
     {
        int r=isArmStrong(n);
        if(n==r)
            cout<<n<<" is Armstrong No.";
        else
            cout<<n<<" is not Armstrong No.";
     }
   };
  int Arm::isArmStrong(int n)
   {
      int r,sum=0,digit=0,a,as;
       a=as=n;
       while(as!=0)   //Count Digits
        {
         as/=10;
         digit++;
  }
       while(n!=0)
       {
           r=n%10;
           sum+=pow(r,digit);
           n/=10;
       }
       return (sum);
   }
 int main()
 {
     Arm a1;
     a1.input();
     a1.show();
     getch();
 }
Output:-












Program in C++ to check a number in Armstrong or not Using class

 Using Constructor
#include<iostream>
#include<conio.h>
#include<math.h>
 using namespace std;
  class Arm
   {
       int n;
       int isArmStrong(int n);
    public:
      Arm() {   n=1; }
      Arm(int n1){ n=n1; }
     ~Arm(){}
     void input()
     {
         cout<<"Input a number:-";
         cin>>n;
     }
     void show()
     {
        int r=isArmStrong(n);
        if(n==r)
            cout<<n<<" is Armstrong No.";
        else
            cout<<n<<" is not Armstrong No.";
     }
   };
  int Arm::isArmStrong(int n)
   {
      int r,sum=0,digit=0,a,as;
       a=as=n;
       while(as!=0)  //count digits
        {
           as/=10;
           digit++;
  }
       while(n!=0)
       {
           r=n%10;
           sum+=pow(r,digit);
           n/=10;
       }
       return (sum);
   }
 int main()
 {
     Arm a1(54748);
     a1.show();
     getch();
 }
Output:-





Post a Comment

0 Comments