A Prime Number Generator

darkscript 0 Tallied Votes 149 Views Share

This program takes a parameter, which is how many prime numbers you need. However if you want to make a program which checks whether a number is prime or not you can delete everything in the main function and put this line instead:

cout << isPrime (argv [1]) ? "It's a prime." : "It's not a prime."

Hope you find this useful, if it works correct ;).

#include <iostream>
#include <stdlib.h>

using namespace std;

bool isPrime (float number)
{
	if (number == 1.0f) {
		return true;
	}
	for (float i=2.0f; i<number; i+=1.0f) {
		if (((number/i) - (int)(number/i)) == 0)
			return false;
	}
	return true;
}

int main (int argc, char** argv)
{
	cout << "Hello its me the prime number god!, I will give you " << argv [1] << " Prime Numbers like you requested.\n";
	int nums =0;
	while (nums != atoi (argv [1])){
		static int i =0;
		if (isPrime (i)){
			cout << "The number : " << nums << " is : " << i << endl;
			i++;
			nums++;
		}else
			i++;
	}
}
darkscript 5 Newbie Poster

Sorry everybody, I chose C syntax instead of C++.

Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Moved :)

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.