The following code is for a programming class I'm taking. The program is supposed to prompt for a number, then prompt for a second number. Then it should use a while loop to multiply the first number by the second number. The second number does not change - the first number is continually multiplied until it equals 1000. Here's an example of what it should do:

enter 3
enter 3

3*3=9
9*3=27
27*3=81
81*3=243
243*3=729

And it should stop there. But, when I run the program I find that it continues to multiply up to some point - when I run the example of a=3, b=3, I get:

3*3=9
9*3=27
27*3=81
81*3=243
243*3=729
729*3=2187

How can I fix my code so it stops when either number reaches 1000?

``````#include <iostream.h>
#include <math.h>
#include <iomanip.h>

main()
{
int num = 1;
long a;
long b;
long c;

cout<<"Welcome to the Multi-Loop Program.                    \n";
cout<<"This program will prompt you to enter two numbers      \n";
cout<<"between 1 and 1000. It will then multiply the first   \n";
cout<<"number by the second number up to or equal to 1000.   \n"<<endl;

cout<<"To use the program, press 1. To exit, press 0. \n";
cout<<"Enter the number choice here. -> ";
cin>>num;
cout<<endl;

while(num==1)
{
a = 1;
b = 1;
c = 1;
cout<<"Enter the first number. (Between 1 and 1000) -> ";
cin>>a;
cout<<endl;
cout<<"Enter a number for the multiple. (Between 1 and 1000) -> ";
cin>>b;
cout<<endl;
cout<<setw(10)<<a<<setw(10)<<a<<endl;

while(a >= 0 && a <= 1000 && b >= 1 && b <= 1000 && c >=0 && c <= 1000)
{
c = a * b;
cout<<setw(10)<<a<<"*"<<b<<"="<<setw(7)<<c<<endl;
a = c * b;
cout<<setw(10)<<c<<"*"<<b<<"="<<setw(7)<<a<<endl;
}

cout<<endl;
cout<<"Enter 1 to continue or 0 exit. -> ";
cin>>num;
cout<<endl;
}

cout<<endl;
cout<<"Thank you for using the Multi-Loop Program.";

return 0;
}``````

## All 2 Replies

You could stick an if statement in front of the cout.

if (number < 1000) cout<<....

``````while(product <= 1000)
{
product = result * multiplier;
result = product;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.