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!!

Dani AI

Generated

The goal is to print the numbers that are divisible by increment, five per line, with commas between numbers on the same line and no trailing comma at line ends. The newline should be driven by how many items have been printed, not by the numeric value itself — that is the distinction was hinting at. 's snippet contains the right idea but had a small typo (sun1) that would break it. Collecting the matches first separates concerns (finding vs formatting) and makes the grouping logic simpler and less error-prone.

A straightforward implementation: collect divisible values into a std::vector<int> and then print them in groups of five, printing a comma when the element is not the last on its line and avoiding a trailing comma on the very last element.

#include <iostream>
#include <vector>

void print_divisible(int minrange, int maxrange, int increment)
{
    if (increment == 0 || minrange > maxrange) return;
    std::vector<int> hits;
    for (int n = minrange; n <= maxrange; ++n)
        if (n % increment == 0)
            hits.push_back(n);

    for (size_t i = 0; i < hits.size(); ++i) {
        std::cout << hits[i];
        bool endOfLine = ((i + 1) % 5 == 0);
        bool last = (i + 1 == hits.size());
        if (!last) {
            if (endOfLine) std::cout << '\n';
            else std::cout << ", ";
        } else {
            std::cout << '\n';
        }
    }
}

Notes: guard against increment == 0 to avoid divide-by-zero, decide whether minrange/maxrange are inclusive, and handle the case of fewer than five matches naturally. If memory is a concern, an equivalent single-pass approach can print on the fly while keeping a small printed-item counter (the same idea suggested by ) — the key is to base newlines on the printed-item index, not on the numeric value.

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.