if(minrange<maxrange)
	{
		cout <<"The numbers that are divisible by " <<increment<<" are ";
		cout <<endl;
		for(count=minrange; count<=maxrange; count++)
		{
			if(count%increment==0)
			{
				
				cout <<count <<" , ";
				sum1 = sum1+count;
			}
		}

this is the code that i wrote. The code is working fine. However, my problem is that every 5 numbers that its going to print out there has to endl.
For example,
5, 10, 15, 20, 25
30, 35, 40, 45, 50
55, 60, 65, 70, 75

like this.

However my code will write 5, 10, 15, 20, 25, 30, 45, 50, 55, 60, 65, 70 all in one line which is causing me problem.
How would i solve this problem? since its in for loop i can't put endl in for loop because it will read the endl command every loop and make the numbers look like this

5
10
15
20
25
30
35
etc...

plz help me!!

Recommended Answers

All 6 Replies

Put another if condition that test count % 5 == 0

Put another if condition that test count % 5 == 0

I don't see how that is going to help.
the user of this code will type the increment (divisor) , minimum and maximum range of numbers it will test to see if it is divisible or not. My code does check and list out the divisible numbers. However, my code lists all the numbers that are divisible in one line which is a problem for me.
Every line, it can only have 5 numbers and it will have to move to next line. Currently, I cannot see how to get around with this.

Just add a if statement

if(minrange<maxrange)
{
cout <<"The numbers that are divisible by " <<increment<<" are ";
cout <<endl;
for(count=minrange; count<=maxrange; count++)
{
if(count%increment==0)
{

cout <<count <<" , ";
sum1 = sum1+count;

     if(sun1%5==0)
     {
     cout <<endl;
     }

}
}

Just add a if statement

if(minrange<maxrange)
{
cout <<"The numbers that are divisible by " <<increment<<" are ";
cout <<endl;
for(count=minrange; count<=maxrange; count++)
{
if(count%increment==0)
{

cout <<count <<" , ";
sum1 = sum1+count;

if(sun1%5==0)
{
cout <<endl;
}

}
}

if i did that wouldn't it make endl every time it writes a number?

dat would make is look like

5
10
15
20
25
30

which doesn't solve my problem. I need to have it

5, 10, 15, 20, 25
30, 35, 40, 45, 50

like this

if(minrange<maxrange)
{
    cout <<"The numbers that are divisible by " <<increment<<" are ";
    cout <<endl;
    int num_printed = 0;
    for(count=minrange; count<=maxrange; count++)
    {
          if(count%increment==0)
          {             
             cout <<count;
             num_printer++;
             if(num_printed < 5)
                cout ", ";
             else
             {
                 cout "\n";
                 num_printed = 0;
              }
             
             sum1 = sum1+count;
          }
}

Here's the result I get

The numbers that are divisible by 5 are
0, 5, 10, 15, 20
25, 30, 35, 40, 45
50, 55, 60, 65, 70
75, 80, 85, 90, 95
100, Press any key to continue . . .
#include <iostream>
using namespace std;

int main()
{
int minrange = 0;
int maxrange = 100;
int increment = 5;
int count = 0;
int sum1 = 0;
if(minrange<maxrange)
{
    cout <<"The numbers that are divisible by " <<increment<<" are ";
    cout <<endl;
    int num_printed = 0;
    for(count=minrange; count<=maxrange; count++)
    {
          if(count%increment==0)
          {             
             cout <<count;
             num_printed++;
             if(num_printed < 5)
                cout << ", ";
             else
             {
                 cout << "\n";
                 num_printed = 0;
              }
             
             sum1 = sum1+count;
          }
    }
}
}
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.