3 Replies - 4036 Views - Last Post: 01 November 2009 - 02:43 PM Rate Topic: -----

#1 klinger23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-November 09

using while loop

Posted 01 November 2009 - 11:01 AM

write program that prompts user to input two integers,num1 and num2 (num1 should be less than num2)use while loop to perform the following steps:
a)output all odd numbers between num1 and num2 (including num1 and num2)

b)output the sum of all even numbers between num1 and num2

c)display information on two different lines

//Lab Assignment____
//Part 1 Using While Loop

#include <iostream>
using namespace std;

int main()
{
	int num1,num2,		//The users input
			total,
			 even;
	//Tell about the program and prompt user to input
	//two integers.
	cout << "This program will show you the odd numbers \n";
	cout << "and sum of even numbers between two integers.\n";
	cout << "Please enter two integers,the first one should \n";
	cout << "be less than the second one.\n";
	cin >> num1 >> num2;
	
	
	while (num1 < num2)
	{	
		num1++;
		if (num1 % 2 != 0)
			cout << " " << num1;		
		
		else 
			total = num1 + num1;
			cout << total;
	}	
		
	
	
	return 0;

}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 01 November 2009 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: using while loop

#2 Jay93  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 92
  • Joined: 04-August 09

Re: using while loop

Posted 01 November 2009 - 11:11 AM

Should it not be the other way round?
while (num2 < num1)
{
//your code
num2++;
}


Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: using while loop

Posted 01 November 2009 - 11:11 AM

What problems are you having with your code?

PLEASE POST CODE IN BETWEEN THE [CODE] [ /CODE] TAGS
Was This Post Helpful? 0
  • +
  • -

#4 mcrmcrmcr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-October 09

Re: using while loop

Posted 01 November 2009 - 02:43 PM

I put some boundaries to the program. :blush:

#include <iostream>
using namespace std;

int main()
{
	int num1,num2; //The users input
	
	int temp=0;
	
	//Tell about the program and prompt user to input
	//two integers.
	cout << "This program will show you the odd numbers \n";	 
	cout << "and sum of even numbers between two integers.\n";		   
	cout << "Please enter two integers,the first one should \n";
	cout << "be less than the second one.\n";
	cin >> num1 >> num2;	
	 
	
	while (num1 <=num2)
	{ 
		if(num1==num2 || num1>num2)
				break;
		if (num1 % 2 != 0)
		{
			
			temp+=num1+1;		
			cout << " " << num1;		
			num1+=2;
			
		}
		
		else 
		{		temp+=num1;	 		  
		num1+=1;			
		}	
		
		
	}
	cout<<endl;
	if (temp==0)
	{cout<<"wrong entry!!!!";}
	cout<<"total number is :"<<temp;
	
	
	return 0;
	
} 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1