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
vichka [17]
3 years ago
6

Write a program in python to test if given number is prime or not.

Computers and Technology
2 answers:
Lerok [7]3 years ago
7 0

num = int(input("Enter a number to test if it's prime or not: "))

if num%2 == 0:

   if num == 2:

       print(num,"is prime")

   else:

       print(num,"is not prime")

else:

   count = 0

   for x in range(2,num):

       if num % x == 0:

           count+=1

   if count==0:

       print(num,"is prime")

   else:

       print(num,"is not prime")

I wrote my code in python 3.8. I hope this helps!

Paraphin [41]3 years ago
4 0

Answer:

Answer:

def main():

num = int(input("Input a number to check for prime: "))

if num > 1:

 for i in range(2,num):

  if (num % i) == 0:

   print("%d is not a prime number" % num)

   break

  else:

   print("%d is a prime number" % num)

   break

else:

 print("%d is not a prime number" % num)

 

if __name__ == "__main__":

main()

Explanation:

Solution retrieved from programiz.com.

Note, this program uses the idea of the Sieve of Eratosthenes to validate the input number by using the modulo operator to determine primeness.

The program will output to the user if the number input is indeed prime or not.

Cheers.

Explanation:

You might be interested in
Write a method maxMagnitude() with two integer input parameters that returns the largest magnitude value. Use the method in a pr
yawa3891 [41]

Answer:

The program in Java is as follows:

import java.util.*;

import java.lang.Math;

public class Main{

public static int maxMagnitude(int num1, int num2){

    int mag = num2;

    if(Math.abs(num1) > Math.abs(num2)){

        mag = num1;

    }

    return mag;

}

public static void main(String[] args) {

 int num1, num2;

 Scanner input = new Scanner(System.in);

 System.out.print("Enter two integers: ");

 num1 = input.nextInt();

 num2 = input.nextInt();

 System.out.println(maxMagnitude(num1,num2));

}

}

Explanation:

The method begins here

public static int maxMagnitude(int num1, int num2){

This initializes the highest magnitude to num2

    int mag = num2;

If the magnitude of num1 is greater than that of num2

    if(Math.abs(num1) > Math.abs(num2)){

mag is set to num1

        mag = num1;

    }

This returns mag

    return mag;

}

The main  method begins here

public static void main(String[] args) {

This declares num1 and num2 as integer

 int num1, num2;

 Scanner input = new Scanner(System.in);

This prompts the user for two integers

 System.out.print("Enter two integers: ");

This gets input for the first integer

 num1 = input.nextInt();

This gets input for the second integer

 num2 = input.nextInt();

This calls the maxMagnitude method and prints the number with the highest magnitude

 System.out.println(maxMagnitude(num1,num2));

}

3 0
3 years ago
Find the area of square whose perimeter is 260.8m​
inessss [21]
4251.04

The perimeter (260.8) over 4 is equal to each side length (65.2)

65.2^2 = 4251.04
5 0
2 years ago
Most project files will contain:
Leno4ka [110]

Explanation:

not A or D,

about B and C I'm not sure

7 0
3 years ago
Which types of networks cover large geographical areas such as several states? PAN
aleksley [76]

MAN

Explanation:

Metropolitan Area Network

Local Area Network

Wide area Network

7 0
3 years ago
What two actions does the RETI instruction perf orm? Why must these two actions be done in a single instruction, as opposed to a
Bas_tet [7]

Answer:

In assembly language, two instructions control the use of the assembly language procedure.

  • CALL
  • RET

CALL pushed the control to the return address onto the stack and transferred the control.

RET instruction returns the address that placed on the stack by a call instruction.

Explanation:

Action RET instruction

  • The RET instruction pops the address and returns off the stack, which is pointed by the stack pointer.
  • The stack is LIFO in memory at a particular location, and the pointer points offset from the stack location.

RET instruction does its job by consulting the register and memory state at the point when it is executed.

In RET instruction, only register and memory state is executed. Call instruction must save that address that figure out in a register and memory location.

5 0
3 years ago
Other questions:
  • Ports on the motherboard can be disabled or enabled in ____ setup. RAM Firmware Northbridge BIOS
    8·1 answer
  • 50 points!!!!!
    9·2 answers
  • Pick 2 different operating systems and discuss how they can be used in daily operations.
    5·1 answer
  • ) Which is true about the agile method?
    7·1 answer
  • Which of the following are examples of how a company might use consumer data it had collected? a To decide what types of product
    10·1 answer
  • One of the first signals that an organization is making progress in the development of its IR program, specifically in the devel
    6·1 answer
  • What is unique about the TODAY and NOW functions?
    15·2 answers
  • Which of the following accessories would you use to create a drawing? A. Calculator B. Notepad C. Paint D. WordPad
    14·2 answers
  • Which is the highest level of the hierarchy of needs model?
    14·1 answer
  • Edhesive 3.4 practice 1
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!