Check if number is prime

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 1TF: Mark the following statements as true or false: a. To use a predefined function in a program, you...
icon
Related questions
Question
18. Check if number is prime. Need C, C++, Java, Python, or Perl code for the below question. Important: Do not copy codes from prepinsta.com.
Question: Write a code to check whether a number is prime or not. Condition use
function check0 to find whether entered no is positive or negative, if negative then
enter the no, And if yes pas no as a parameter to prime() and check whether no is
prime or not?
. Whether the number is positive or not, if it is negative then print the message
"please enter the positive number"
It is positive then call the function prime and check whether the take positive
number is prime or not.
C Code:
#include <stdio.h>
void prime(int n)
{
Transcribed Image Text:Question: Write a code to check whether a number is prime or not. Condition use function check0 to find whether entered no is positive or negative, if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not? . Whether the number is positive or not, if it is negative then print the message "please enter the positive number" It is positive then call the function prime and check whether the take positive number is prime or not. C Code: #include <stdio.h> void prime(int n) {
Expert Solution
Program execution

This is very simple.

I have written a simple Python code to solve the problem. I have also attached the output in Step 2.

----------------------------------------------- Python Code -------------------------------------------------

def prime(n):
    if n > 1:
        for i in range(2, n):
            if (n % i) == 0:
                print(n,"is not a prime number ")
                break
        else:
            print(n,"is a prime number ")
            return

def check(n):
    if n < 0:
        print(" Please enter a positive number ")
        n = int(input(" Now enter a positive number (NOT 0) : "))
        prime(n)
    else:
        prime(n)
        
 

if __name__ = "__main__":    
     N = int(input(" Enter a number : "))
      print(check(N))

        

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Binary numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr