Learn C++ beginer programs

July 8, 2009

write a program to calculate sum of all even numbers upto n

Filed under: cin, cout, else if, for loop, if, if else, loop, numbers — snehilkhanor @ 1:49 pm
A number is taken from user and sum of all even numbers upto that number is calculated.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. {
  11. if(i%2==0)
  12. sum=sum+i;
  13. else
  14. continue;
  15. }
  16. cout<<sum;
  17. getch();
  18. }

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.