C++ Program to Print Sum of Natural Numbers From 1 to 100

Question : Write a C++ Program to Display Sum of Numbers From 1 to 100?

Explain: C++ Question about Print series of sum of natural numbers from 1 to 100. We are using for loop inside example.


Solution:

 /************************************************************ 
   C++ Program for Print Sum of Numbers From 1 to 100 using for Loop
 *************************************************************
***************** By cpplanguage.com *********************/

#include <iostream>                                     
using namespace std;                                    
int main()                                              
{                                                       
      int counter;       
        // initialize sum =0                               
      int sum=0; 
      // for loop condition for 1 to 100                                      
for(counter=1; counter<=100; counter++)                
{          
// add sum= sum + counter value until counter 100                                          
     sum=sum+counter;                                  
                                                      
}            
// display the total sum                                          
cout<<"Total sum of 1 to 100 Numbers = "<<sum<<endl;   
                                                        
return 0;                                              
}     

Output: 
C++ Program for Print Sum of Numbers From 1 to 100 using for Loop