0

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++;
	}
}
2
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by Dani
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.