Write C++ program to find sum of even numbers between 1 to n


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int i, num, sum=0;
  7.  
  8. //Reading number
  9. cout<<"Enter any number: "<<endl;
  10. cin>>num;
  11.  
  12. for(i=2; i<=num; i+=2)
  13. {
  14. //Adding current even number to sum variable
  15. sum += i;
  16. }
  17. cout<<"Sum of all even number between 1 to " << num << ": "<<sum;
  18.  
  19. return 0;
  20. }
  21.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts