C++ program to find the sum of prime numbers in a range using class

Given a range, we have to find the sum of prime numbers in that range using class and object approach.
Submitted by Shubh Pachori, on September 01, 2022

Example:

Input: 
Enter Range: 1 9

Output: 
Prime Numbers in the range 1 to 9 are 1 2 3 5 7 
Sum of prime numbers in range is 18

C++ code to find the sum of prime numbers in a range using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Prime {
  // declare private data members
 private:
  int start, end;

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

  // sumPrime() function to print prime numbers in between range
  int sumPrime() {
    // initilaize int type variables to perform operations
    int index_1, index_2, sum = 0, check = 1;

    cout << "Prime Numbers in the range " << start << " to " << end << " are ";

    // for loop to search prime number in the range
    for (index_1 = start; index_1 <= end; index_1++) {
      // counter variable
      int check = 0;

      // for loop for checking if the number is prime or not
      for (index_2 = 2; index_2 < index_1; index_2++) {
        // if condition to check if the index_1 
        // is divisible by the index_2
        if (index_1 % index_2 == 0) {
          // counter increased by 1
          check++;

          // break statement to break the loop
          break;
        }
      }

      // if the counter is zero then the index_1 number
      // is printed and added to the sum
      if (check == 0) {
        cout << index_1 << " ";

        // adding prime numbers
        sum = sum + index_1;
      }
    }

    // returning sum
    return sum;
  }
};

int main() {
  // create an object
  Prime P;

  // int type variable to store 
  // sum of prime numbers
  int sum;

  // calling getRange() function 
  // to insert range
  P.getRange();

  // sumPrime() function to add prime numbers 
  // in the range
  sum = P.sumPrime();

  cout << "\nSum of prime numbers in range is " << sum;

  return 0;
}

Output:

RUN 1:
Enter Range: 2 11
Prime Numbers in the range 2 to 11 are 2 3 5 7 11 
Sum of prime numbers in range is 28

RUN 2:
Enter Range: 1 100
Prime Numbers in the range 1 to 100 are 1 2 3 5 7 11 13 17 19 23 29 31 37 41 
43 47 53 59 61 67 71 73 79 83 89 97 
Sum of prime numbers in range is 1061

Explanation:

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

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