Question

Write a C program that prompts the user to input as many unsigned(n) as the user...

Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number

You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double

You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number.

For each number the user types, output whether that number is prime or not.

You won't need a die function for this program, but you will need a prime function:

int prime(unsigned n);

prime does no I/O. It's job is to return whether n is prime (1 or 0).

If you know a correct algorithm for determining whether n is prime, feel free to use it. Or, you're free to use the following algorithm:

if n is less than 4, then we're done: it's prime if it's greater than 1

if n is even, then we're done: it's not prime

for fac taking on the values 3, 5, 7, 9, ...

if fac is greater than n/fac, we're done: n is prime  

if n is divisible by fac, we're done: n is not prime

Homework Answers

Answer #1


// C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <unistd.h>

int prime (unsigned int n)
{
     if (n <= 1) return 0;
     if (n % 2 == 0 && n > 2) return 0;
     for(int i = 3; i < n / 2; i+= 2)
     {
         if (n % i == 0)
             return 0;
     }
     return 1;
}

int main()
{
    unsigned int n;

    while(1)
    {
        printf("Enter n: ");
        scanf("%u",&n);

        if(n == 0)
            break;

        else
        {
            if(prime(n) == 1)
                printf("%d is prime\n\n",n);
            else
                printf("%d is not prime\n\n",n);
        }
    }

    return 0;
}

/*
output;

Enter n: 12
12 is not prime

Enter n: 22
22 is not prime

Enter n: 23
23 is prime

Enter n: 0

*/

Know the answer?
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
Similar Questions
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
C++ Write a program that will ask the user to input n positive numbers. The program...
C++ Write a program that will ask the user to input n positive numbers. The program will terminate if one of those number is not positive. please respond quickly and clearly, thank you!
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
in C++ Write a program. The program should prompt the user for the number of shares...
in C++ Write a program. The program should prompt the user for the number of shares purchased, purchase price per share, the commission for the purchase, sale commission paid, and the price the shares were sold at. The program should validate each of these values to ensure they are acceptable (none can be less than zero) and should then pass them to a function called numberfunction.The function should use the following formula to determine the profit: Profit = ((number of...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT