Computer Science, asked by Innu7035, 11 months ago

Write a c++ program to calculate sum of 1st fifty odd nos. Using constructor program.

Answers

Answered by swathi45
0

#include <iostream>

using namespace std;

class Oddsum

{

private:int sum = 0;

int n = 50;

void gen_odd_sum(){

for ( int i = 1;i <n;i +=2){

sum+=i;

}

public:

Oddsum(int num) {

n = num;

gen_odd_sum();}

Oddsum()

{

gen_odd_sum();}

void print_odd_nos() {

cout<<sum;}

};

int main()

{

Oddsum ob;

ob.print_odd_nos();

return 0;

}

Similar questions