×

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 numbers in the array are in arithmetic progression using class

Submitted by Shubh Pachori, on September 01, 2022

Problem statement

Given an array, we have to check if the numbers in the array are in arithmetic progression using the class and object approach.

Example:

Input: 
Enter Progression:
0:2
1:4
2:6
3:8
4:10
5:12
6:14
7:16
8:18
9:20

Output: 
Entered Progression:2,4,6,8,10,12,14,16,18,20

C++ code to check if the numbers in the array are in arithmetic progression using the class and object approach

#include <iostream>
using namespace std;

// create a class
class ArithmeticProgression {
  // private data members
 private:
  int array[10];

  // public member functions
 public:
  // getProgression() function to
  // insert the progression
  void getProgression() {
    cout << "Enter Progression:" << endl;

    for (int index = 0; index < 10; index++) {
      cout << index << ":";
      cin >> array[index];
    }
  }

  // putProgression() function to show
  // inserted progression
  void putProgression() {
    cout << "\nEntered Progression:";

    for (int index = 0; index < 10; index++) {
      cout << array[index] << ",";
    }

    cout << "\b \b";
  }

  // checkProgression() function to check whether
  // the inserted progression is in
  // arithmetic progression or not
  void checkProgression() {
    // initializing int type variables to perform operations
    int index, difference = 0;

    // bool variable to check condition
    bool condition = true;

    // calculating difference between first and
    // second number in the array
    difference = array[1] - array[0];

    // foe loop to check the inserted progression if
    // it is a arithmetic progression or not
    for (index = 0; index < 9 && condition; index++) {
      // initializing condition by false
      condition = false;

      // if condition to check further differences are same or not
      if (array[index] + difference == array[index + 1]) {
        // if same then condition is true
        condition = true;
      }
    }

    // if to check the condition
    if (condition) {
      cout << "\n" << endl;

      cout << "It is an arithmetic progression" << endl;
    }

    // else for condition = false
    else {
      cout << "\n" << endl;
      cout << "It is not an arithmetic progression" << endl;
    }
  }
};

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

  // calling getProgression() function to 
  // insert the progression
  A.getProgression();

  // calling putProgression() function to 
  // show the inserted progression
  A.putProgression();

  // calling checkProgression() function to 
  // check the inserted progression
  A.checkProgression();

  return 0;
}

Output

Enter Progression:
0:1
1:34
2:4
3:5
4:6
5:7
6:8
7:9
8:10
9:11

Entered Progression: 1,34,4,5,6,7,8,9,10,11

It is not an arithmetic progression

Explanation

In the above code, we have created a class ArithmeticProgression, one int type array data member array[10] to store the values, and public member functions  getProgression(), putProgression(), and checkProgression() to insert progression in the array, to show the inserted progression and to check the inserted progression.

In the main() function, we are creating an object A of class ArithmeticProgression, reading a progression values by the user of the array using the getProgression() function, and finally calling the checkProgression() member function to check the inserted progression if it is an arithmetic progression or not. The checkProgression() function contains the logic to find if the progression is an arithmetic progression or not and printing the result.

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

Comments and Discussions!

Load comments ↻





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