×

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 the table of number entered using class

Submitted by Shubh Pachori, on September 12, 2022

Problem statement

Given an integer number, we have to print the table of number entered using the class and object approach.

Example:

Input:
Enter Number:19

Output:
19*1 = 19
19*2 = 38
19*3 = 57
19*4 = 76
19*5 = 95
19*6 = 114
19*7 = 133
19*8 = 152
19*9 = 171
19*10 = 190

C++ code to print the table of number entered using the class and object approach

#include <iostream>
using namespace std;

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

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

  // table() function to print table
  // of inserted number
  void table() {
    // for loop to print the table
    for (int index = 1; index <= 10; index++) {
      // this statement will print the table in form of 2*1=2
      cout << number << "*" << index << " = " << number * index << endl;
    }
  }
};

int main() {
  // creating an object
  TABLE T;

  // calling getNumber() function to 
  // insert number
  T.getNumber();

  // calling table() function to print table 
  // of inserted number
  T.table();

  return 0;
}

Output

Enter Number: 10
10*1 = 10
10*2 = 20
10*3 = 30
10*4 = 40
10*5 = 50
10*6 = 60
10*7 = 70
10*8 = 80
10*9 = 90
10*10 = 100

Explanation

In the above code, we have created a class TABLE, one int type data members number to store the numbers, and public member functions getNumber() and table() to store the number and to print table of inputted number.

In the main() function, we are creating an object T of class TABLE, inputting the number by the user using the getNumber() function, and finally calling the table() member function to print table of inputted number. The table() function contains the logic to print table of inputted number.

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

Comments and Discussions!

Load comments ↻





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