A prime number (or a prime) is an integer greater than
1 that is not a product of two smaller integer. Created a program
on visual studio named PrimeNumberTest that does
the following:
1) prompt the user for input of an integer
2) test if the integer is a prime number
3) display the test result
Program in c++
in CPP
#include <iostream>
using namespace std;
int main() {
int i, number;
int isPrime=1;
// prompting user to enter number
cout << "Enter a positive integer: ";
// taking input from the user
cin >> number;
//A prime number (or a prime) is an integer greater than
1
if (number == 0 || number == 1) {
isPrime = 0;
}
else {
for (i = 2; i <= number / 2; ++i) {
if (number % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime==1)
cout << number << " is a prime number";
else
cout << number << " is not a prime
number";
return 0;
}
Java program PrimeNumberTest
import java.util.*;
class PrimeNumberTest
{
public static void main(String[] args)
{
Scanner scanObj=new
Scanner(System.in);
System.out.println("Enter
number:");
int number = scanObj.nextInt();
int i, m = 0, c = 0;
m = number / 2;
if (number == 0 || number == 1) {
System.out.println(number + " not a prime number");
} else {
for (i = 2; i <= m; i++) {
if (number % i == 0) {
System.out.println(number + " not a prime number");
c = 1;
break;
}
}
if (c == 0) {
System.out.println(number + " is a prime number");
}
}
}
}
Python Program for PrimeNumberTest
number = int(input("input of an integer: "))
#A prime number (or a prime) is an integer greater than 1
if number > 1:
# check for factors
for i in range(2,number):
if (number % i) == 0:
print(number,"is not a prime number")
print(i,"times",number//i,"is",number)
break
else:
print(number,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(number,"is not a prime number")
Get Answers For Free
Most questions answered within 1 hours.