Formatting the output..

Hi, My program prints out on the console numbers up to a number specified by a user and prints all the divisors for each number. Now I got some issues.

1) After the numbers get bigger the divisors just continue getting print right after the line ends.. Is it possible to print divisors every time they continue on a new line to start under the very first divisor?

2) Is it possible to get rid of the comma "," after the last divisor?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67


#include <iostream>
#include <conio.h>
#include "Divisors_of_Numbers_Library.h"

using namespace std;

int main()
{

// ************* VARIABLES *************
unsigned long long int upToNumber, n, sumOfDivisors;

 cout << "\n"
         << "\t\t\t\t\t\t"
         << " Divisors of Numbers "
         << "\n"
         << "\t\t\t\t\t\t"
         << "*********************"
         << "\n";

    cout << "\n"
          << "\t\t\t\t"
          << "    This program generates a list of numbers with"
          << "\n"
          << "\t\t\t"
          << "     their divisors up to a number specified by you (the user)."
          << "\n"
          << "\t\t\t"
          << "     It also shows the sum of all the divisors* of each number."
          << "\n"
          << "\t\t\t\t\t"
          << "  *Excluding the number itself."
          << "\n";

    cout << "\n"
           << "Up to what number you want to create a list of numbers with their"
           << " divisors? ";

    upToNumber = getPositiveInteger();

    cout << "\n";

    for (unsigned long long int counter = 1; counter <= upToNumber; counter++)
    {
        unsigned long long int number = counter;
        unsigned long long int numberHalved = static_cast<int>(number / 2.0);
        cout << "\n" << counter << "\t" << "|";

        sumOfDivisors = 0;
        for (unsigned long long int counter = 1; counter <= numberHalved; counter++ )
        {
            if (number % counter == 0)
            {
                cout << counter << ", ";
                sumOfDivisors = sumOfDivisors + counter;
            }
        }
        cout << "\t"
             << "total: "
             << sumOfDivisors;
}
getch();
return 0;



Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    for (unsigned long long int counter = 1; counter <= upToNumber; counter++) {
	unsigned long long int number = counter;
	unsigned long long int numberHalved = static_cast < int >(number / 2.0);
	cout << "\n" << counter;

	sumOfDivisors = 0;
	unsigned numOutput=0;	// number of divisors output on current line
	for (unsigned long long int counter = 1; counter <= numberHalved; counter++) {
	    if (number % counter == 0) {
		if (numOutput == 8) { // end of line
		    cout << '\n';
		    numOutput = 0;
		}
		if (numOutput++ == 0) { // new line
		    cout << "\t| ";
		} else {
		    cout << ", ";
		}
		cout << counter;
		sumOfDivisors = sumOfDivisors + counter;
	    }
	}
	cout << "\t" << "total: " << sumOfDivisors;
    }
 
Nice the outcome is great!

I have a question though.. I cant seem to understand what do you mean by :
1
2
3
4
5
6
7
8

if (numOutput++ == 0) { // new line
		    cout << "\t| ";
		} else {
		    cout << ", ";
		}



can you simplify/explain numOutput++ == 0?
This is a way of checking if you're at the beginning of a line (numOutput == 0) and incrementing numOutput at the same time. The value of numOutput++ is the value of numOutput, but it has the side effect of incrementing it after the evaluation.
Topic archived. No new replies allowed.