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(); }
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.