my task is to type 20 integer numbers from 0 to 99 as the input data.
few tasks are to check the maximum and minimum, and find the total number of even and odd numbers. (this one i already got the answer). But i have problem for my next task, which is to display prime numbers from the input data.

this is what i've done. and i dont know how to check the prime numbers. hope someone can help me.

#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
    int i,j,n=10, max=0, min=99, x, even=0, odd=0, prime=0; // x0=0,x1=0, x2=0, x3=0, x4=0; prime=2;

    cout << "Enter 20 integer numbers from 0 to 99: "<<endl;

    for (i=1;i<=n;i++)
    {
        cout << "Input " << i <<":";
        cin >> x;
        if (x>max)
        max=x;
        if (x<min)
        min=x;
        if (x%2==0)
            even++;
        else
            odd++;
        if (x%x==0 && x%1==x)
            prime=x;
        if(prime)
        {
                 cout << i << " is prime" << endl;
         }


    }

    cout<<"max is"<<max<<endl;
    cout<<"min is"<<min<<endl;
    cout<<"Total number of even is "<<even<<endl;
    cout<<"Total number of odd is "<<odd<<endl;


    getch();
}

Recommended Answers

All 9 Replies

Please explain exactly how you are checking for a prime. I see no code at all that does it.

Well a prime number can only be divided by 1 and itself. With that you can write a function that you pass the number to check and return a bool for the resault. To find out if a number is prime you need to use the mod operator on it by all numbers up to the square root of the number your checking

bool isPrime(int number)
// add check for number == 1 or number == 2
// add check to see if number is even
for i = 3 to i * i < number // start at 3 since 1 and 2 are prime
    if (number % i == 0) // if there is no remainder than it is not prime
        return false
return true // if we get here it is prime

Line 24 is wrong, x%x is always 0 and x%1 is also always 0 so it is the equivilent of

if (0 == 0 && 0 == x)

Calculating is a number is prime is not trivial (except for the first few) and I would suggest that you use a function to do the calculation and have the function return a true or false

bool isPrime(int x)
{
    // Code to calculate if a number is prime
}

Since your input is limited to values in the range 0-99 I would say you have 2 choices in the isPrime function; implement an algorith to check if the number is prime or just have an array of all the prime numbers in the range 0 - 99 and check to see if the number is in the array.

my task is to type 20 integer numbers from 0 to 99 as the input data.
But i have problem for my next task, which is to display prime numbers from the input data.

2, 3, 5 and 7 are prime numbers. If an integer between 8 and 99 is evenly divisible by 2, 3, 5 or 7, it is not a prime number; otherwise it is a prime number.

for i = 3 to i

I suppose ( in general ) running loop till square root of the number, in consideration would be better.

// start at 3 since 1 and 2 are prime

Actually nowa days 1 is generally considered to be not prime because it is just so darn special in its own right.

Read this for a better explaination of why http://primes.utm.edu/notes/faq/one.html

for i = 3 to i * i < number // start at 3 since 1 and 2 are prime

Shouldn't it be i * i <= number? Otherwise some square numbers will be improperly considered prime (e.g. 9 or 25).

@np complete - I am running to the sqaure root by doing i * i < number.

@nmaillet - You are correct it should be i * i <= number. Thanks for pointing that out.

thank you for all the help.
now i know my mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.