using while loop

Page 1 of 1

3 Replies - 6635 Views - Last Post: 01 November 2009 - 02:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=135872&amp;s=758a0918b1f1aa2de260994c3d28d31a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 klinger23

Reputation: 0
• 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;

}
```

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

Reputation: 7
• Posts: 93
• 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)
{
num2++;
}

```

#3 erik.price

• D.I.C Lover

Reputation: 485
• 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

#4 mcrmcrmcr

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

Re: using while loop

Posted 01 November 2009 - 02:43 PM

I put some boundaries to the program.

```#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;

}

```