- 1

Prime numbers using functions. Writing 1-100 prime numbers to a file? Need help with programming assignment

21st Feb 2017, 6:33 PM
Kyle Martin
Kyle Martin - avatar
25 Respostas
0
woah man I'm not looking for someone to do my homework....im frustrated because I can't figure it out.
21st Feb 2017, 7:03 PM
Kyle Martin
Kyle Martin - avatar
0
I was trying to do it for 8 hours yesterday.
21st Feb 2017, 7:04 PM
Kyle Martin
Kyle Martin - avatar
0
And again, you didn't ask a question, or specified the language Post the lines of code you made in 8 hours yesterday
21st Feb 2017, 7:07 PM
Dago VF
Dago VF - avatar
0
#include<iostream> using namespace std; bool isPrime(int, int); int main() { int number; int number2 = 1; cout << "Please enter a number between 1 and 100.\n\n"; cin >> number; isPrime(number, number2); if (isPrime(number, number2)==true) { cout << number << " is a prime number."; } else cout << number << " The number is not a prime number."; system("pause"); } bool isPrime(int number, int number2){ bool status; if (number % number2 == 0) { status = true; } else if (number % number2 > 0) { status = false; } return status; }
21st Feb 2017, 7:11 PM
Kyle Martin
Kyle Martin - avatar
0
Like I said I'm not trying to get someone to do my homework..im at a frustrated level and my school doesn't offer any help...
21st Feb 2017, 7:13 PM
Kyle Martin
Kyle Martin - avatar
0
and I'm trying to learn coding. I'm not trying to get someone to do it for me
21st Feb 2017, 7:14 PM
Kyle Martin
Kyle Martin - avatar
0
Your program ask for a number from 1-100 and then you check if it is prime And I think it is wrong I think they are requesting you a program that prints ALL the prime numbers from 1 to 100 And you are printing in terminal, and you need to print the numbers in a file
21st Feb 2017, 7:17 PM
Dago VF
Dago VF - avatar
0
A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function name isPrime , which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Use the isPrime function in a program that stores a list of all the prime numbers from 1 through 100 in a file named allprimes.dat. TIP: Recall that the % operator divides one number by another, and returns the remainder of the division. In an expression such as num1 % num2 , the % operator will return 0 if num1 is evenly divisible by num2 .
21st Feb 2017, 7:19 PM
Kyle Martin
Kyle Martin - avatar
0
with mine its returning every number the user types in as true for a prime number..
21st Feb 2017, 7:20 PM
Kyle Martin
Kyle Martin - avatar
0
That's because you declared variable number2 is equal to 1 And any number divided by 1, gives you a reminder of 0 You only need one parameter in your function isPrime
21st Feb 2017, 7:25 PM
Dago VF
Dago VF - avatar
0
oh duh....but if I put number%1 !=0 it gives me an error
21st Feb 2017, 7:28 PM
Kyle Martin
Kyle Martin - avatar
0
why would u use a decrement instead of an increment?
21st Feb 2017, 7:43 PM
Kyle Martin
Kyle Martin - avatar
0
#include<iostream> using namespace std; bool isPrime(int, int); int main() { int number; int number2 = 1; cout << "Please enter a number between 1 and 100.\n\n"; cin >> number; isPrime(number, number2); if (isPrime(number, number2)==true) { cout << number << " is a prime number."; } else cout << number << " The number is not a prime number."; system("pause"); } bool isPrime(int number, int number2) { bool status; //this is what I changed. why wouldn't it work? for (number = number2; number2 = number2++;) { } if (number % number2 == 0) { status = true; } else if (number % number2 != 0) { status = false; } return status; }
21st Feb 2017, 7:46 PM
Kyle Martin
Kyle Martin - avatar
0
#include<iostream> using namespace std; bool isPrime(int, int); int main() { int number; int number2 = 1; cout << "Please enter a number between 1 and 100.\n\n"; cin >> number; isPrime(number, number2); if (isPrime(number, number2)==true) { cout << number << " is a prime number."; } else cout << number << " The number is not a prime number."; system("pause"); } bool isPrime(int number, int number2) { bool status; for (number = number2; number2 = number2++;) { if (number % number2 == 0) { status = true; } else if (number % number2 != 0) { status = false; } } return status; } // I mean all of this...it stops after you input the number
21st Feb 2017, 7:52 PM
Kyle Martin
Kyle Martin - avatar
0
That's because your function is declared in the top with one parameter, but after you request the number, that function is expecting 2 parameters You have to modify your function, isPrime(number) The for loop and if needs to be equal to ones I posted
21st Feb 2017, 9:24 PM
Dago VF
Dago VF - avatar
0
it has two parameters in the function, both are int
21st Feb 2017, 9:32 PM
Kyle Martin
Kyle Martin - avatar
0
Only one is needed And you are only asking for one to user
21st Feb 2017, 9:34 PM
Dago VF
Dago VF - avatar
0
#include<iostream> using namespace std; bool isPrime(int); int main() { int number; cout << "Please enter a number between 1 and 100.\n\n"; cin >> number; isPrime(number); if (isPrime(number)==true) { cout << number << " is a prime number.\n\n"; } else cout << number << " The number is not a prime number.\n\n"; while (number > 0) { cout << "Please enter a number between 1 and 100.\n\n"; cin >> number; isPrime(number); if (isPrime(number) == true) { cout << number << " is a prime number.\n\n"; } else cout << number << "is not a prime number.\n\n"; } system("pause"); }
21st Feb 2017, 11:25 PM
Kyle Martin
Kyle Martin - avatar
0
its still not working properly.. I put in a while loop
21st Feb 2017, 11:26 PM
Kyle Martin
Kyle Martin - avatar
0
All wrong
21st Feb 2017, 11:28 PM
Dago VF
Dago VF - avatar