Question

A prime number (or a prime) is an integer greater than 1 that is not a...

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  

Homework Answers

Answer #1

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")


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
Activity 6.6. (a) A positive integer that is greater than 11 and not prime is called...
Activity 6.6. (a) A positive integer that is greater than 11 and not prime is called composite. Write a technical definition for the concept of composite number with a similar level of detail as in the “more complete” definition of prime number. Note. A number is called prime if its only divisors are 1 and itself. This definition has some hidden parts: a more complete definition would be as follows. A number is called prime if it is an integer,...
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
For any integer n > 0, n!(n factorial) is defined as the product n * n...
For any integer n > 0, n!(n factorial) is defined as the product n * n - 1 * n − 2 … * 2 * 1. And 0! is defined to be 1 Create function that takes n as input and computes then returns the accurate value for: n!= n * n - 1 * n − 2 … * 2 * 1 prompt the user to enter an integer n, call functions to compute the accurate value for...
4. Prove that if p is a prime number greater than 3, then p is of...
4. Prove that if p is a prime number greater than 3, then p is of the form 3k + 1 or 3k + 2. 5. Prove that if p is a prime number, then n √p is irrational for every integer n ≥ 2. 6. Prove or disprove that 3 is the only prime number of the form n2 −1. 7. Prove that if a is a positive integer of the form 3n+2, then at least one prime divisor...
Please Write code in visual studio Roman Numeral Converter Write a program that asks the user...
Please Write code in visual studio Roman Numeral Converter Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Input Validation: Decide how the program should handle an input that is less then 1 or greater than 10.
Each natural number greater than 1 is either a prime number or is a product of...
Each natural number greater than 1 is either a prime number or is a product of prime numbers