Question
In python please

4. Write the code that will define a function that takes any given random number as input and it will check to see if that nu
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code: (Python 3 implementation)

def primeCheck():
n = int(input('Enter a number: '))

flag = 1
for i in range(2, n):
if(n%i == 0):
flag = 0

if(flag == 0):
print('{} is not a prime number.'.format(n))
else:
print('{} is a prime number.'.format(n))

primeCheck()

1 def primeCheck() 2 n-int (input ( Enter a number: )) flag1 for i in range (2, n) 4 flag 0 if (flag0) 10 print(is not a p

Sample results :

Enter a number: 31 31 is a prime number.

Enter a number: 32 32 is not a prime number

Explanation:

A prime number is divisible only by 1 and the number itself.

So, if a number 'n' is divisible by any number between 1 and n(2, ... , n-1), then 'n' is not a prime number.

primeCheck() is the Python function that takes a random number as input and will print whether the given number is a prime number or not.

input('Enter a number: ') is used to take the input from the user along with a message "Enter a number: ", which returns a string datatype. To convert the string to integer datatype, we typecast it using int().

Initially, the variable flag is set to 1, which represents a prime number. If flag is 0, then the number is not a prime number.

Then, using a for loop, we iterate from 2 to n-1. If the input number 'n' is divisible by any number from 2 to n-1(remainder of the division n%i is 0), we set the flag to 0(denoting a non prime number).

Finally, after the for loop, if the value of flag variable is unchanged(flag is 1), then the number is a prime number. Else(flag is 0), then the number is not a prime number.

(If the given number is 1 (or 2), then the for loop is not executed, so the flag value remains 1, denoting that 1 (or 2) is a prime number.)

Add a comment
Know the answer?
Add Answer to:
In python please 4. Write the code that will define a function that takes any given...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can I see that source code in python Write the following value-returning function: encoded - Takes...

    Can I see that source code in python Write the following value-returning function: encoded - Takes a string as parameter and returns the string with each vowel substituted by its corresponding number as follows: a -1 e-2 0-4 。u_5 The function MUST USE string slicing Write a main program that will take an input string (text) from command line and it will display the input text encoded. You will call the encoded function to get the text encoded.

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • Python code define a function car that takes 'swift' as a parameter function should count how...

    Python code define a function car that takes 'swift' as a parameter function should count how many letters in the string function should return a dictionary where key is lower case alpha caracter and value is the number of times that character appears in sentence. ex- input- that big car is so expensive output- 't':1, 'h':1, 'a':2, 'b':1, 'i':3,......

  • Write a Python Code for a Function: you need to come up with code for shift_char....

    Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...

  • PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of...

    PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of positive integer numbers (a, b) such that c = a + b is also a whole number and 1 <c<n. Include only those triples that are co-prime (do not have any common divisors other than 1). For example, (3, 4, 5) is okay but (30, 40, 50) should be skipped. Help: As a starting example, examine the function pythagorian_triples that yields all triples. Modify...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • Please write code in Python! You are asked to implement the following checksum formula to validate...

    Please write code in Python! You are asked to implement the following checksum formula to validate an identifi- cation number given to you. The formula works as follows. Using the original number, double the value of every other digit. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits individually). The identification number is valid if the resulting sum is divisible by 10. Write a function called validateID that takes...

  • Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using...

    Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using only while loops and if statements (no string multiplication). ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### 26) Write code that asks the user for a size and prints a triangle of stars of that size that increases by two at each level. You can only use one while loop and any number of variables and counters you want, but you are limited to these tools....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT