Search & Find out

Sunday, December 6, 2009

SUM OF EVEN NUMBERS


Write a program to accept a number 'n' from the user and print the sum of all even numbers from 2 to number 'n'.


#include "iostream.h"
#include "conio.h"
class demo
{
 public:
 int a,b,i;
 void sum ();
};
void demo ::sum()
{
 cout<<"\n\nEnter a number  : ";
 cin >> a;
 for(i=2,b=0;i<=a;i++)
 {
        if(i%2==0)
          {
          b= b+i;
          cout<
            if(i              {
               cout<<"+";
               }
            }
 }
cout<<"="<

 }
 void main()
 {
  clrscr();
  demo d;
  cout <<"Welcome to find no. & total of even no.s\n\n";
  d.sum();
  getch();
 }



Get the executable ( .exe ) file for this program..
Download

0 comments:

Post a Comment

 

My Blog List

Term of Use