Looping issue
Feb 28, 2019 at 5:40am UTC
I can't figure out this formatting loop. The program finds out if a number is divisible by a certain number and if it is then it outputs the numbers it is divisible by in a row. My issue is that the rows can only be 8 numbers long with spaces and then it needs to start a new row with the rest of the numbers. And after that I need to reverse the output so small numbers-->bug and vise versa. The way I have it now does not stop at 8 and only goes from big to small and I can't figure it out.
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
#include<iostream>
#include<iomanip>
using namespace std;
int num1,n=2,total;
int main()
{
cout<<"Enter a number: " ;
cin>>num1;
cout<<num1<<" is divisible by: " ;
while (n<num1)
{
total=num1/n;
if (num1%n==0)
{
cout<<total<<" " ;
}
n++;
total=0;
}
return 0;
}
output:
Enter a number: 360
360 is divisible by: 180 120 90 72 60 45 40 36 30 24 20 18 15 12 10 9 8 6 5 4 3 2
//needs to show as--- 360 is divisible by:
180 120 90 72 60 45 40 36
30 24 20 18 15 12 10 9
8 6 5 4 3 2
360 is divisible by:
2 3 4 5 6 8 9 10
12 15 18 20 24 30 36 40
45 60 72 90 120 180
Last edited on Feb 28, 2019 at 6:26am UTC
Feb 28, 2019 at 6:12am UTC
Your spec is unclear. Show a couple example input/outputs.
Feb 28, 2019 at 6:27am UTC
alright I updated it to show what it is outputting and how the output is supposed to look
Feb 28, 2019 at 6:49am UTC
Quick and dirty:
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
#include<iostream>
int main()
{
const int row_count = 8;
std::cout << "Enter a number: " ;
int num1;
std::cin >> num1;
std::cout << '\n' ;
std::cout << num1 << " is divisible by:\n" ;
int n = 2;
int count = 0;
while (n < (num1 / 2 + 1))
{
if (num1 % n == 0)
{
int total = num1 / n;
std::cout << total << '\t' ;
count++;
if (count % row_count == 0)
{
std::cout << '\n' ;
}
}
n++;
}
std::cout << '\n' ;
std::cout << "\nReversed, " << num1 << " is divisble by:\n" ;
n = num1 / 2 + 1;
count = 0;
while (n > 1)
{
if (num1 % n == 0)
{
int total = num1 / n;
std::cout << total << '\t' ;
count++;
if (count % row_count == 0)
{
std::cout << '\n' ;
}
}
n--;
}
std::cout << '\n' ;
}
Enter a number: 360
360 is divisible by:
180 120 90 72 60 45 40 36
30 24 20 18 15 12 10 9
8 6 5 4 3 2
Reversed, 360 is divisble by:
2 3 4 5 6 8 9 10
12 15 18 20 24 30 36 40
45 60 72 90 120 180
Topic archived. No new replies allowed.