×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to check if the number is automorphic or not using class

Submitted by Shubh Pachori, on October 09, 2022

Problem statement

Given a number, we have to check whether it is automorphic number or not using the class and object approach.

Example:

Input:
Enter Number : 76

Output:
It is an Automorphic Number!

C++ code to check if the number is automorphic or not using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Automorphic {
  // private data member
 private:
  int number;

  // public member functions
 public:
  // getNumber() function to insert number
  void getNumber() {
    cout << "Enter Number : ";
    cin >> number;
  }

  // isAutomorphic() function to check if the number
  // is automorphic is or not
  void isAutomorphic() {
    // initialising  int type variables to perform operations
    int check = 1, square = number * number;

    // while loop to check the number
    while (number) {
      // if condition to check the last digit of number and its square
      if (number % 10 != square % 10) {
        check = 0;
      }

      number = number / 10;
      square = square / 10;
    }

    if (check) {
      cout << "It is an Automorphic Number!" << endl;
    } else {
      cout << "It is not an Automorphic Number!" << endl;
    }
  }
};

int main() {
  // create an object
  Automorphic A;

  // calling getNumber() function to
  // insert the number
  A.getNumber();

  // calling isAutomorphic() function to check the
  // number if it is a automorphic number
  A.isAutomorphic();

  return 0;
}

Output

Enter Number : 76
It is an Automorphic Number!

Explanation

In the above code, we have created a class Automorphic, one int type data member number to store the number, and public member functions getNumber() and isAutomorphic() to store and to check the given integer number is an automorphic number or not.

In the main() function, we are creating an object A of class Automorphic, reading an integer number by the user using the getNumber() function, and finally calling the isAutomorphic() member function to check the given integer number. The isAutomorphic() function contains the logic to given check the number and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.