NEED HELP

Hi, I am struggling with this program in my C++ Class.

Create a C++ program that accepts two integers from the user and then determines the parity of the first integer (even or odd), the sign of the first integer, if the first integer is an element of 2, 3, 5, 7, 11, 13, 17, 19, 23,29 ( first ten primes), and if the two integers are consecutive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream> 

 using namespace std; 
 int main() 
 { 
 cout<<"This program will determine whether a positive integer is even or odd.\n"; 

 int num, large, small, count = 0; 


 while(num) 
 { 
 cout<<"Enter a positive integer. Enter 0 to exit the program.\n"; 
 cin>>num; 
 if(num%2==0) 
 cout << num<<" is an even number." << end;; 
 else 
 cout<<num<<" is an odd number." << endl; 
 cout<<endl; 
What's the problem? Right now you're only reading in one number, when your assignment asks for two. The parity check looks good. Here's a code sample with user input and parity check already done. It shows the pieces that you should break the assignment into. Get one piece working at a time and test. Don't try to do it all at once.
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
#include <iostream>

/*
Create a C++ program that
1] accepts two integers from the user
2] determines the parity of the first integer (even or odd)
3] the sign of the first integer
4] if the first integer is an element of 2, 3, 5, 7, 11, 13, 17, 19, 23,29 (first ten primes)
5] if the two integers are consecutive.
*/
int main()
{
    //****
    // 1
    //****
    int firstNum, secondNum;
    std::cout << "Enter the first number: ";
    std::cin >> firstNum;

    std::cout << "Enter the second number: ";
    std::cin >> secondNum;

    //****
    // 2
    //****
    if (firstNum % 2 == 0)
    {
        std::cout << "The first number is even.\n";
    }
    else
    {
        std::cout << "The first number is odd.\n";
    }

    //****
    // 3
    //****
    // the sign of the first integer
    // if it is less than zero it is negative...
    // structurally, this will look a lot like the check
    // already done for parity (even or odd) above

    //****
    // 4
    //****
    // if the first integer is one of the first 10 primes
    // create some container, an array perhaps, that holds the first 10 primes
    // use a loop to check the first integer against each item in the container
    // if you find a match, then the first num is one of the first 10 primes

    //****
    // 5
    //****
    // What does it mean to be consecutive? That means the two integers differ
    // by exactly one. Subtract them. If the answer is 1 or -1, then they're
    // consecutive.

    return 0;
}
Topic archived. No new replies allowed.