One integer instead of two
Oct 22, 2012 at 12:20am UTC
How can I change it so that I input only one integer instead of two?
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
#include <iostream>
using namespace std;
int main()
{
int n;
int y;
int i;
int prime;
cout <<"Enter two integers : " ;
cin >> n >> y;
while (y >= n)
{
prime=0;
cout << "\n" << n << ":\t" ;
for (i=2;i<n;i++)
if (n%i==0)
{
cout << i;
prime=1;
}
if (prime==0)
cout <<"Prime" ;
n++;
}
cout << "\n" ;
system ("pause" );
return 0;
}
Oct 22, 2012 at 12:22am UTC
cin >> n;
I think that's what you mean anyway.
Oct 22, 2012 at 12:24am UTC
Oh okay thank you i did that but i never deleted the while (y >= n)
now it works thank you.
Oct 22, 2012 at 12:25am UTC
No problem, happy to help!
Oct 22, 2012 at 12:34am UTC
One more thing I have a code that lists if the number is a prime number or a perfect number i want to incorporate the other one where it lists the divisors how would i do that?
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <string>
using namespace std;
int checkprime(int n)
{
int i;
int prime=1;
for (i=2;i<n/2;i++)
{
if (n%i==0)
{
prime=0;
break ;
}
}
return prime;
}
int isperfect(int x)
{
int sum=0;
int i;
for (i=1;i<x;i++)
{
if (x%i==0)
{
sum=sum+i;
}
}
if (sum==x)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
string input;
do
{
int b;
cout << "*******************************" <<endl;
cout << "Enter number: " ;
cin >> b;
cout << "*******************************" <<endl;
if (isperfect(b)==1)
{
cout<<b<<" is a perfect number. \n" ;
}
else
{
cout << b << " is not a perfect number. \n" ;
}
if (checkprime(b)==1)
{
cout << b << " is a prime number. \n" ;
}
else
{
cout << b << " is not a prime number. \n" ;
}
cout <<"*******************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
} while ((input == "N" ) || (input == "n" ));
return 0;
}
Oct 22, 2012 at 12:39am UTC
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
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int num, countA, countB;
cout << "Please enter a positive maxiumum integer value you wish to find divisors: \n" ;
cin >> num;
if (num <= 0)
{
cout << "The input entered was invalid. Please input a positive whole integer.\n" ;
}
else
{
for (countA = 1; countA <= num; countA++)
{
cout << "Divisors of " << countA << ": " ;
for (countB = 1; countB<= num; countB++)
{
if (countA % countB == 0)
{
cout << countB << " " ;
}
}
cout << endl;
}
}
return 0;
}
This is a short example of how you could do something like that.
Oct 22, 2012 at 5:11am UTC
How can I make it so that after inputting an integer not within the range, you have the option to quit or to continue? I have it set like that if you do enter an integer within the range but can't figure out how to have it setup the same.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#include <iostream>
#include <string>
using namespace std;
int checkprime(int n)
{
int i;
int prime=1;
for (i=2;i<n/2;i++)
{
if (n%i==0)
{
prime=0;
break ;
}
}
return prime;
}
int isperfect(int x)
{
int sum=0;
int i;
for (i=1;i<x;i++)
{
if (x%i==0)
{
sum=sum+i;
}
}
if (sum==x)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
string input;
do
{
int b;
int i;
int prime=1;
cout << "*******************************" <<endl;
cout << "Enter number: " ;
cin >> b;
cout << "*******************************" <<endl;
if (b<0|| b>1000)
{
cout << "Please enter an integer that is within the range." <<endl;
system ("pause" );
break ;
}
if (isperfect(b)==1)
{
cout<<b<<" is a perfect number. \n" ;
}
else
{
cout << b << " is not a perfect number. \n" ;
}
if (checkprime(b)==1)
{
cout << b << " is a prime number. \n" ;
}
else
{
cout << b << " is not a prime number. \n" ;
}
{
prime=0;
cout << "Divisors of " << b << ":" << " " ;
for (i=2;i<b;i++)
if (b%i==0)
{
cout << i << " " ;
prime=1;
}
b++;
}
cout <<"\n*******************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
} while ((input == "N" ) || (input == "n" ));
return 0;
}
Topic archived. No new replies allowed.