Question

Q2. To check and print if entered integer is prime or not. Prompt the user to...

Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return value from the called function in main and check it to print whether the integer is prime or not. For input 1 - your program should print “Neither prime nor composite”. 2 is a prime.

Use c program and please write the steps required. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int is_prime(int n) {
    if (n <= 1) {
        return 0;
    } else {
        int i;
        for (i = 2; i < n; ++i) {
            if (n % i == 0) {
                return 0;
            }
        }
        return 1;
    }
}

int main() {
    int n;
    while (1) {
        printf("Please input a positive integer or 0 to exit: ");
        scanf("%d", &n);
        if (n == 0) break;
        if (n < 0) {
            printf("%d is not a valid positive integer\n", n);
        } else {
            if (n == 1) {
                printf("%d is Neither prime nor composite\n", n);
            } else {
                if (is_prime(n)) {
                    printf("%d is a prime number\n", n);
                } else {
                    printf("%d is NOT a prime number\n", n);
                }
            }
        }
    }

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Q2. To check and print if entered integer is prime or not. Prompt the user to...
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
  • a) You will write a void function to get a positive integer between 1 and 12...

    a) You will write a void function to get a positive integer between 1 and 12 inclusive. The twelve numbers represent the months of the year. The user will be asked to enter an integer between 1 and 12 inclusive. If the number entered by the user is not within range, an appropriate error message will be displayed, and the user will be asked to try again until a valid number is entered. b) You will write a void function...

  • Write a Python program to prompt the user for an integer number. Assign this integer to...

    Write a Python program to prompt the user for an integer number. Assign this integer to a variable X. Then assign a variable Y to the X**3 (X to the power 3). Finally, assign the variable Z to the square root of Y and print the values of X, Y, and Z as shown in the example below. Use main() function to define and call the program. Example: Enter an integer number: 3 X = 3 if Y=X**3 then Y...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • Please write a MIPS program to prompt the user to input three positive integers and then print out their greatest common prime factor.

    Please write a MIPS program to prompt the user to input three positive integers and then print out their greatest common prime factor.What to submit:1. Your MIPS program file.2. The log file of simulation in SPIM. The log file should contain the Registers,  Text Segments, Data Segments, and Console.

  • A Prime Number is an integer which is greater than one, and whose only factors are...

    A Prime Number is an integer which is greater than one, and whose only factors are 1 and itself. Numbers that have more than two factors are composite numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. The number 1 is not a prime number. Write a well-documented, Python program - the main program, that accepts both a lower and upper integer from user entries. Construct a function, isPrime(n), that takes...

  • In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...

    In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt user to enter an integer to store to the parameter. 2. Function isPerfectSquare take an integer parameter and checks whether it's a perfect square, that it's square root is an integer. 3. Function min3 return the mainimum value of the parameter values, it shouldn’t use any if statement to compare the parameters but call min2 to do the comparison. Extra Credit: Function printPrimeFactorization print...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Write a C++ program that will provide a user with a grade range if they enter...

    Write a C++ program that will provide a user with a grade range if they enter a letter grade. Your program should contain one function. Your function will accept one argument of type char and will not return anything (the return type will be void), but rather print statements to the console. Your main function will contain code to prompt the user to enter a letter grade. It will pass the letter grade entered by the user to your function....

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Using Perl Program: Write a program with a subroutine to prompt the user for any message...

    Using Perl Program: Write a program with a subroutine to prompt the user for any message (like "what is your name?"). The sub will collect the user's answer and return it back to the call (in the main program above the subroutine). The sub does not accept any arguments. Print out the returned string (name) from the sub.

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
ADVERTISEMENT