×

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 print happy numbers in a range using class

Submitted by Shubh Pachori, on October 08, 2022

Problem statement

Given a range, we have to print happy numbers in that range using the class and object approach.

Example:

Input: 
Enter Start : 1
Enter End : 10

Output: 
Happy Numbers in range 1 to 10 : 
1 7 10

C++ code to print happy numbers in a range using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Happy {
  // private data member
 private:
  int start, end;

  // public member functions
 public:
  // getRange() function to insert range
  void getRange() {
    cout << "Enter Start : ";
    cin >> start;

    cout << "Enter End : ";
    cin >> end;
  }

  // isHappy() function to print the happy number
  // lies in the range
  void isHappy() {
    // initialising  int type variables to perform operations
    int index, sum, remain, number;

    cout << "\nHappy Numbers in range " << start << " to " << end << " : "
         << endl;

    // for loop to check numbers lies in the range
    for (index = start; index <= end; index++) {
      number = index;

      sum = 0;

      // while loop to check the number
      while (sum != 1 && sum != 4) {
        sum = 0;

        // while loop to manipulate the number
        while (number) {
          remain = number % 10;
          sum = sum + (remain * remain);
          number = number / 10;
        }

        number = sum;
      }

      if (sum == 1) {
        cout << index << " ";
      }
    }
  }
};

int main() {
  // create an object
  Happy H;

  // calling getRange() function to
  // insert the range
  H.getRange();

  // calling isHappy() function to print
  // happy numbers lies in the range
  H.isHappy();

  return 0;
}

Output

RUN 1:
Enter Start : 10
Enter End : 30

Happy Numbers in range 10 to 30 : 
10 13 19 23 28

RUN 2:
Enter Start : 1
Enter End : 100

Happy Numbers in range 1 to 100 : 
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 

Explanation

In the above code, we have created a class Happy, two int type data members start and end to store the start and end of the range, and public member functions getRange() and isHappy() to store and print happy numbers in between that given range.

In the main() function, we are creating an object H of class Happy, reading a range by the user using getRange() function, and finally calling the isHappy() member function to print the happy numbers in the given range. The isHappy() function contains the logic to print happy numbers in the given range and printing the result.

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

Comments and Discussions!

Load comments ↻





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