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. }

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.