1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
natta225 [31]
3 years ago
11

A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime be

cause it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6. Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate the function in a complete program. Tip: Recall that the % operator divides one number by another and returns the remainder of the division. In an expression such as num1 % num2, the % operator will return 0 if num1 is evenly divisible by num2.
Computers and Technology
1 answer:
Gemiola [76]3 years ago
4 0

Answer:

The c++ program to check prime numbers is shown below.

#include <iostream>

using namespace std;

bool isPrime(int n);

bool isPrime(int n)

{

   bool prime;

   int p=0;

   

   if(n==2 || n==3)

       prime = true;

   else if(n%2 == 0)

       prime = false;

   else

   {

       for(int k=3; k<n/2; k++)

       {

           if(n%k == 0)

               p++;

       }

   

   if(p>1)

       prime = false;

   else

       prime = true;

   }

   

   return prime;

}

int main() {

   int num;

   do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<2);

   

   cout<<"The "<<num<<" is prime. "<<isPrime(num)<<endl;

   

   

}

 

OUTPUT

Enter a positive number.

-4

Invalid number. Enter a positive number

0

Enter a positive number.

101

The 101 is prime. 1

Explanation:

The user input is validated for positivity. A do while loop along with if statement is implemented for verification.

do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<1);

The test for prime number is done by using multiple if else statements and a Boolean variable prime is used.

If user inputs 2 or 3, variable prime is set to true.

Else If user inputs an even number, variable prime is set to false. This is done by taking modulo of the number upon division by 2.

Else if user inputs neither an even number nor a number less than 3, the modulus of the number is taken with divisors beginning from 3 up to half of the input number. Here, an integer variable p is used and based on its value, variable prime is set to true or false.

For this, an integer variable p is initialized to 0. A number can be completely divisible by itself or by its factors.

If the number is divisible by any of the divisors, value of variable p is increased by 1. If value of p is greater than 1, this means that the user input is divisible by more than one divisor. Hence, the given number is not a prime number and the variable prime is set to false. Otherwise prime will be set to true.

The value 1 indicates true and 0 indicates false.

You might be interested in
What are two fundamental components of data structures?
Fed [463]
Organization and access method.

Hope this helped! :)

- Jujufire
8 0
4 years ago
Sends your response to everyone who received the initial email.
Ymorist [56]

Answer:

umm no, i will not tell you, the one who asked this question

8 0
3 years ago
A variable definition defines the name of a variable that will be used in a program, as well as:
tresset_1 [31]

Answer:

A) The type of data it will hold

Explanation:

In most programming languages, a variable must be decalared before it can be used. Variable declarations allows the compiller to make provision of memory space for the variable so the type of data must be defined accordingly with the name of the variable because different data types occuppy different spaces in memory for example

An integer is 32 bit integral value

A char  is 16 bit unicode value

A double is 64 bit floating point value

6 0
3 years ago
Which of the following is not an Operating System? Select one:
harina [27]
Internet Explorer is not an Operating System. Internet Explorer is a web browser.
6 0
3 years ago
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an in
ycow [4]

Answer:

import java.util.Scanner;

public class LabProgram {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       String inputMonth = sc.next();

       int inputDay = sc.nextInt();

       if (inputMonth.equals("January") && inputDay >= 1 && inputDay <= 31)

           System.out.println("winter");

       else if (inputMonth.equals("February") && inputDay >= 1 && inputDay <= 29)

           System.out.println("winter");

       else if (inputMonth.equals("April") && inputDay >= 1 && inputDay <= 30)

           System.out.println("spring");

       else if (inputMonth.equals("May") && inputDay >= 1 && inputDay <= 30)

           System.out.println("spring");

       else if (inputMonth.equals("July") && inputDay >= 1 && inputDay <= 31)

           System.out.println("summer");

       else if (inputMonth.equals("August") && inputDay >= 1 && inputDay <= 31)

           System.out.println("summer");

       else if (inputMonth.equals("October") && inputDay >= 1 && inputDay <= 31)

           System.out.println("autumn");

       else if (inputMonth.equals("November") && inputDay >= 1 && inputDay <= 30)

           System.out.println("autumn");

       else if (inputMonth.equals("March") && inputDay >= 20 && inputDay <= 31)

           System.out.println("spring");

       else if (inputMonth.equals("June") && inputDay >= 1 && inputDay <= 20)

           System.out.println("spring");

       else if (inputMonth.equals("June") && inputDay >= 21 && inputDay <= 30)

           System.out.println("summer");

       else if (inputMonth.equals("September") && inputDay >= 1 && inputDay <= 21)

           System.out.println("summer");

       else if (inputMonth.equals("September") && inputDay >= 22 && inputDay <= 30)

           System.out.println("autumn");

       else if (inputMonth.equals("December") && inputDay >= 0 && inputDay <= 20)

           System.out.println("autumn");

       else if (inputMonth.equals("December") && inputDay >= 21 && inputDay <= 30)

           System.out.println("winter");

       else if (inputMonth.equals("March") && inputDay >= 1 && inputDay <= 19)

           System.out.println("winter");

       else

           System.out.println("invalid");

   }

}

8 0
3 years ago
Other questions:
  • Why might a programmer prefer the top-down approach to programming
    7·2 answers
  • Write a program that takes nouns and forms their plurals on the basis of these rules:
    8·1 answer
  • When saving my template, I will need to select Word Template in the _____ dialog box.
    12·1 answer
  • If you hear that an airplane crashes into the Empire State Building, and you immediately think of the 9/11 terrorist attack on t
    9·1 answer
  • You are given a list of n positive integers a1, a2, . . . an and a positive integer t. Use dynamic programming to design an algo
    10·1 answer
  • What is the difference between requirements and controls in the security process? Give examples of each.
    11·1 answer
  • Since the value we want to receive is either true or false, it makes the most sense to use:
    14·1 answer
  • salvado has a flash disk of 1.5GB and wants to store more music files of 950KBs each . How many files will he be able to store o
    8·1 answer
  • Your company is planning to storage log data, crash dump files, and other diagnostic data for Azure VMs in Azure. The company ha
    12·1 answer
  • Pleaseeeee helppp
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!