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
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;
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
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
"
#include <iostream>
usingnamespace 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;
}
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
#include <iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
elsebreak;
}
}
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;
}