C++ program to print ugly numbers in a range using class

Given a range, we have to print ugly numbers in that range using the class and object approach.
Submitted by Shubh Pachori, on October 04, 2022

Example:

Input: 
Enter Start : 1
Enter End : 20

Output: 
Ugly Numbers in range 1 to 20 : 
2 3 4 5 6 8 9 10 12 15 16 18 20

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

#include <iostream>
using namespace std;

// create a class
class Ugly {
  // 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;
  }

  // isUgly() function to print the ugly number
  // lies in the range
  void isUgly() {
    // initialising  int type variables to perform operations
    int index, check, temp;

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

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

      // while loop to check the number in temp
      while (temp != 1) {
        check = 1;

        // if condition to check if temp
        // is a multiple of 5
        if (temp % 5 == 0) {
          temp = temp / 5;
        }

        // else if condition to check if temp
        // is a multiple of 3
        else if (temp % 3 == 0) {
          temp = temp / 3;
        }

        // else if condition to check if temp
        // is a multiple of 2
        else if (temp % 2 == 0) {
          temp = temp / 2;
        }

        // else condition if temp
        // is not a multiple of 2, 3 and 5
        else {
          check = 0;
          break;
        }
      }

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

int main() {
  // create an object
  Ugly U;

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

  // calling isUgly() function to print
  // ugly numbers lies in the range
  U.isUgly();

  return 0;
}

Output:

RUN 1:
Enter Start : 10
Enter End : 30

Ugly Numbers in range 10 to 30 : 
10 12 15 16 18 20 24 25 27 30

RUN 2:
Enter Start : 1
Enter End : 100

Ugly Numbers in range 1 to 100 : 
2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 
45 48 50 54 60 64 72 75 80 81 90 96 100 

Explanation:

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

In the main() function, we are creating an object U of class Ugly, reading a range by the user using getRange() function, and finally calling the isUgly() member function to print the ugly numbers in the given range. The isUgly() function contains the logic to print ugly 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.