#include<iostream>

using namespace std;

class sum{

public:

int i,sum=0;

int findSum(int n)

{

for(i=1;i<=n;i++)

sum=sum+i;

return sum;

}

};

int main()

{

sum obj1;

int result,n;

cout<<"Please enter the value of n\n";

cin>>n;

result=obj1.findSum(n);

cout<<"The sum of first "<<n<<" natural numbers is "<<result<<endl;

return 0;

}

using namespace std;

class sum{

public:

int i,sum=0;

int findSum(int n)

{

for(i=1;i<=n;i++)

sum=sum+i;

return sum;

}

};

int main()

{

sum obj1;

int result,n;

cout<<"Please enter the value of n\n";

cin>>n;

result=obj1.findSum(n);

cout<<"The sum of first "<<n<<" natural numbers is "<<result<<endl;

return 0;

}

Output 1:

Please enter the value of n

5

The sum of first 5 natural number is 15

Output 2:

Please enter the value of n

10

The sum of first 5 natural number is 55

Output 3:

Please enter the value of n

15

The sum of first 5 natural number is 120

## No comments:

## Post a Comment