Write a C++ Program to find Sum of odd numbers between 1 and 100 using class

By | 28.12.2016

Write a C++ Program to find Sum of odd numbers between 1-100 using class

#include<iostream>
using namespace std;
class sum
{
   private:
        int n,s=0;
        public:
          void calc();
                void display();
                
};
void sum::calc()
{
        for(n=1;n<=100;n+=2)
        s=s+n;
        
}
void sum::display()
{
        cout<<"Sum of all odd numbers between 1-100:"<<s;
}
int main()
{
        sum s;
        s.calc();
        s.display();
        
}

 

4.4 9 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
krishil sheth

an easier way for doing this would be ,
for eg: if I want to find the summation first 5 odd numbers,
the answer will be 5^2.
like in this case we have to find summation of first 100 odd numbers , thus the ans will be 100^2=10000.