test my programming and give advice

Hi I am working on a program that check whether it is a prime number or not.Please helpe test my program whether it runs ok or not. If any good advice, please post your words below my programming. thanks guys.
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
#include <iostream>
#include "CinReader.h"
using namespace std;
CinReader reader;
void factor(int number);
bool prime (int number);

int main()
{
	bool isQuit=false;
	do
	{
		cout << "wellcome to my project 5." << endl;
		cout<<"Enter first number. (between 1 - 1000)\n";
		int num1 = reader.readInt(1,1000);
		cout<<"Enter second number. (between 1 - 1000)\n";
		int num2 = reader.readInt(1,1000);
		factor(num1);
		factor(num2);
		prime(num1);
		prime(num2);
		cout<<"Do you want to continue or quit. [Y]es or [N]o"<<endl;
		char choice = reader.readChar("YyNn");
		if(toupper(choice) =='Y')
			isQuit=false;
		else{
			isQuit=true;
			}
		}while(!isQuit);
    return 0;
}

void factor(int number)
{
	cout<<"The factor of "<< number << " is :\n";
	for(int i=1; i<=number; i++)
	{
		if(number%i==0)
		cout<<i<<" ";
	}
	cout<<endl;
}

bool prime(int number)
{
	if(number ==1||number ==2)
	{
		cout<<number <<" is prime number.\n";
		return true;
	}
	else
	{
	for(int i=2; i<number; i++)
        if(number % i ==0)
	{
		cout<<number<<" is not a prime number\n";
			return false;
	}
	}
	cout<<number<<" is  prime number. \n";
	return true;
}


Seems ok. (can't compile it right now). One thing you could improve is that to find a factor (or to check if it is prime) you don't need to check all numbers from 1 to n. You only need to check from 1 to sqrt(n). So change your for loops to i*i <= number. In your factor function you needn't start from 1 because all integers can be divided by 1 without a remainder. Also, if you do the i*i thing you will have to say that a factor of n is n (as a separate case). Lastly, whats is CinReader? Why do you need it? Is it something you wrote yourself?
Thanks ur advice. u r right. if square, the program can save time...
CinReader is a class My friend write for output. it works well when using output with some conditions. You can change to cin cout.. it still works.
Topic archived. No new replies allowed.