C++ Divisors

Im new to C++ and I have no idea "how to ask for multiple inputs and display the divisors for each of the inputs." I tried to use loop to ask for inputs but apparently its not working. I know this is working code but i dont think this is a good code to use. Any helps would be greatly appreciated. Thank you


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>
using namespace std;

int main ()
{
	int a, num1, num2, num3, divisors;

	cout << "Please Enter a Number 1 : ";
	cin >> num1 >> num2 >> num3;
	

    if (num1 > 0 && num2 > 0 && num3 > 0) {
    
    		for (a = 1; a <= num1; a++)
    		{     
                
    			divisors = num1 % a;
    			int result = divisors;
                
    			if (result == 0)
    			{
    			cout << a << " ";
    			}
    		}
    		cout << endl;
    		
    		for (a = 1; a <= num2; a++)
    		{     
                
    			divisors = num2 % a;
    			int result = divisors;
    
    			if (result == 0)
    			{
    			cout << a << " ";
    			}
    		}
    		cout << endl;
    		
    		
    		
    		for (a = 1; a <= num3; a++)
    		{     
                
    			divisors = num3 % a;
    			int result = divisors;
    
    			if (result == 0)
    			{
    			cout << a << " ";
    			}
    		}
    		cout << endl;
    } else {
           cout << "Please enter positive value" << endl;
           }
	
	system ("PAUSE");
	return 0;

}
Last edited on
I think that it would look more organized if you rewrote this section to what I've written below.
1
2
3
4
5
6
7
8
int a, num1, num2, num3, divisors;

	cout << "Please Enter a Number 1: ";
	cin >> num1;
        cout << "Please Enter a Number 2: ";
        cin >> num2;
        cout << "Please Enter a Number 3: ";
        cin >> num3;
Last edited on
whats your problem here? and whats is the purpose of line 4
Sorry about that. I tried to use function in my code and forgot to remove that. The problem is I want to use 'for loops' to ask for a user inputs and display divisiors for each of the value. Thank you
do you know how to use arrays?
it usually happens to me too lol.
i see. my bad.
i create 2 functions , one to hold the computation for divisor and one to read the number.
with the getDivisor function you can compute infinite numbers in one computation.
and this solves your
"
but i dont think this is a good code to use
"
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
#include <iostream>
using namespace std;
int getDivisor(int);
int getNumber();
int main ()
{
    char b = '\0';
    for ( int a = 0;; a++ ){
int number = getNumber();
getDivisor(number);
cout << "Do you want to enter a number again? Y/N: ";
cin >> b;
if ( b == 'n' || 'N' )
    break;
    }
}
int getDivisor(int num) {
    int divisors;
    if (num > 0) {
    		for (int a = 1; a <= num; a++)
    		{
    			divisors = num % a;
    			int result = divisors;
    			if (result == 0)
    			{
    			cout << a << " ";
    			}
    		}
    		cout << endl;
    } else {
           cout << "Please enter positive value" << endl;
           }

    }

int getNumber() {
        int num1;
cout << "Please Enter a Number : ";
cin >> num1;
return num1;
         }


Last edited on
it usually happens to me too lol.
i see. my bad.
i create 2 functions , one to hold the computation for divisor and one to read the number.
with the getDivisor function you can compute infinite numbers in one computation.
and this solves your


Thank you so much for your help. I also tried to use do while but i still cant figure it out how can i display the results. E.g

Please Enter a Number: 5
Please Enter a NUmber: 6
Please Enter a Number: 7

1 5
1 2 3 6
1 7

Something like this. Code stated below. Thank you


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
#include <iostream>
using namespace std;

int main ()
{
	int num[3];
	int count = 0;
	
	do {
        cout << "Please Enter a Number: ";
        cin >> num[count];
        count++;
} while (count < 3);

          for (int i = 1; i < count; i++) {
                int divisor = count % i;
    			
    			if (divisor == 0)
    			{
    			cout << i << " " << endl;
    			}
              }
            
	

	system ("PAUSE");
	return 0;

}
i asked you if you knew how to use arrays and you ignored me

1
2
3
4
5
6
7
8
9
10
11
12
13
int numbers[1000];
    int x=0;
    int size=0;

    cout<<"how many numbers would you like to enter ? ";
    cin>>size;
    for(x=0;x<size;x++)
    {
            cout<<"enter a number ";
            cin>>numbers[x];

    }


this is what i would do were i in your position

and then do a separate nested for loop for the divisors
oh sry. i didnt realize your comment. Thank you so much. I will try the code.
sorry i just got home. are you sure you only want to input 3 numbers?
nope not really...i posted my 2nd code. i put 3 as a test data so that later on i can change.
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
#include <iostream>
using namespace std;

int main ()
{
	int num[3];
	int count = 0;
	
	do {
        cout << "Please Enter a Number: ";
        cin >> num[count];
        count++;
} while (count < 3);

          for (int i = 1; i < count; i++) {
                int divisor = count % i;
    			
    			if (divisor == 0)
    			{
    			cout << i << " " << endl;
    			}
              }
            
	

	system ("PAUSE");
	return 0;

}
Last edited on
oh i gave you the typo code.
sorry.
fix code
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
#include <iostream>
using namespace std;
int getDivisor(int);
int getNumber();
int main ()
{
    char b = '\0';
    for ( int a = 0;; a++ ){
  int number = getNumber();
getDivisor(number);
cout << "Do you want to enter a number again? Y/N: ";
cin >> b;
if ( b == 'Y'  || b ==  'y' )
 continue;
else
    break;
    }
}
int getDivisor(int num) {
    int divisors;
    if (num > 0) {
    		for (int a = 1; a <= num; a++)
    		{
    			divisors = num % a;
    			int result = divisors;
    			if (result == 0)
    			{
    			cout << a << " ";
    			}
    		}
    		cout << endl;
    } else {
           cout << "Please enter positive value" << endl;
           }

    }

int getNumber() {
        int num1;
cout << "Please Enter a Number : ";
cin >> num1;
return num1;
         }
Topic archived. No new replies allowed.