3 Replies - 2688 Views - Last Post: 03 September 2009 - 05:50 AM Rate Topic: -----

#1 amazing92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-August 09

Program that will display all even numbers from 2 to 28 and compute th

Post icon  Posted 03 September 2009 - 05:30 AM

Write a Program that will display all even numbers from 2 to 28 and compute the sum of all even numbers..

I can't get THE RIGHT OUTPUT HERE..please help me..thanks
#include<iostream.h>
main()
{
int count;
double sum;
char ans;
sum=0;
do
{
count=2;
while(count<=28)
{
cout<<"\n"<<count<<endl;
sum=sum+count;
count=count+2;
cout<<"\nDo you want to repeat[Y/N]:";
cin>>ans;
cout<<\nThe sum of all even number is: <<sum<<endl;
sum=sum+count;
}
}
while((ans=='y')||(ans=='Y'));

return 0;
}



***added code tags -jjsaw5

Is This A Good Question/Topic? 0
  • +

Replies To: Program that will display all even numbers from 2 to 28 and compute th

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Program that will display all even numbers from 2 to 28 and compute th

Posted 03 September 2009 - 05:47 AM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Program that will display all even numbers from 2 to 28 and compute th

Posted 03 September 2009 - 05:48 AM

#include <iostream>
using namespace std;

int main(){
	int count = 2;
	int sum = 0;
	char ans;

	do{
		if (count % 2);
		else {
			cout << count << " ";
			sum += count;
		}
		count++;
	}while(count <= 28);

	cout << endl;
	cout << "Sum is " << sum << endl;

}



Was This Post Helpful? 0
  • +
  • -

#4 dgirdhar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 13-June 08

Re: Program that will display all even numbers from 2 to 28 and compute th

Posted 03 September 2009 - 05:50 AM

Check following correction...

#include<iostream.h>
int main()
{

int count;
double sum;
char ans;
sum=0;
do
{
count=2;
while(count<=28)
{
cout<<"\n"<<count<<endl;
sum=sum+count;
count=count+2;
}
cout<< "\nThe sum of all even number is: " << sum << endl;
cout<<"\nDo you want to repeat[Y/N]:";
cin>>ans;
sum = 0;
}
while((ans=='y')||(ans=='Y'));


return 0;
}
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1