Help me out with this program!

- solved -






- Brandon
Last edited on
a bit stuck
- well the code you posted above doesn't really count as it is in effect a blank page on which to write. Usually we expect people to make a start in actually writing code, and then we will help when you encounter problems with what you have tried.
closed account (i8bjz8AR)
I started it out a bit for you.. You will have to go through and tweek some things, although i suggest retyping it so you can follow the logic. In particular, you'll need to account for the user inputting a negative number. Also, you will need to output the numbers it is divisible by. I also used a function, not sure if you have began using them yet.. If not, just let me know. Hope this helps!

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;

bool isPrime (int num);         //function prototype
int main ()
{
    int num=0;

    cout << "Enter a number and I'll tell you whether it is prime: ";
    cin >> num;  //users input of the number being tested

    if (isPrime(num)==true)  //calling the function
        cout << num << " is prime.";  //outputing is prime
    else
        cout << num << " is NOT prime."; //outputting is not prime


    return 0;
}
bool isPrime(int input) //function definition below
{
    if(input<1) //automatically not prime
        return false;
    else if (input == 1||input ==2 ||input==3) //accounting for users input being 1,2, or 3
    {                                          //which are automatically prime by definition
        return true;
    }
    else
    {
        for(int i=2; i<input; i++) //running through an if loop to test if users input is divisible by
        {                          //i,w which is incrementing to test for user input values
            if(input%i==0)  //modulus division
                return false; //if not prime
        }
        return true; //if prime
    }
}
Thanks so much, it's a great start to go off of!
Topic archived. No new replies allowed.