Home »
C++ Programs
C++ program to check if the numbers in the array are in geometric 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 geometric progression using the class and object approach.
Example:
Input:
Enter Progression:
0:1
1:2
2:3
3:4
4:5
5:6
6:7
7:8
8:9
9:10
Output:
Entered Progression:1,2,3,4,5,6,7,8,9,10
It is not a geometric progression
C++ code to check if the numbers in the array are in geometric progression using the class and object approach
#include <iostream>
using namespace std;
// create a class
class GeometricProgression {
// 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
// geometric 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 geometric 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 a geometric progression" << endl;
}
// else for condition = false
else {
cout << "\n" << endl;
cout << "It is not a geometric progression" << endl;
}
}
};
int main() {
// create an object
GeometricProgression G;
// calling getProgression() function
// to insert the progression
G.getProgression();
// calling putProgression() function
// to show the inserted progression
G.putProgression();
// calling checkProgression() function
// to check the inserted progression
G.checkProgression();
return 0;
}
Output
Enter Progression:
0: 2
1: 4
2: 8
3: 16
4: 32
5: 64
6: 128
7: 256
8: 512
9: 1024
Entered Progression:2,4,8,16,32,64,128,256,512,1024
It is a geometric progression
Explanation
In the above code, we have created a class GeometricProgression, 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 G of class GeometricProgression, 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 a geometric progression or not. The checkProgression() function contains the logic to find if the progression is a geometric progression or not and printing the result.
C++ Class and Object Programs (Set 2) »