Question

Write a function called "isPrime()” that checks if an integer number is prime or not. The...

Write a function called "isPrime()” that checks if an integer number is prime or not. The function returns integer 1 if the number is prime, or integer 0 if the number is not prime. Write a simple test program in main() to show that the function works properly.in C programming language.

Homework Answers

Answer #1
#include <stdio.h>

int isPrime(int n);

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (isPrime(n)) {
        printf("%d is a prime number.\n", n);
    } else {
        printf("%d is not a prime number.\n", n);
    }
    return 0;
}

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

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
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....
Write the following in C: 2. An integer is said to be prime if it is...
Write the following in C: 2. An integer is said to be prime if it is divisible by only 1 and itself. Write a function called prime() that takes in one integer number, and returns 1 (true) if the number is prime and 0 (false) otherwise. Write a program to generate six random numbers between 1 to 100 and calls function prime() on each one to determine if it is prime or not.
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
A prime number (or a prime) is an integer greater than 1 that is not a...
A prime number (or a prime) is an integer greater than 1 that is not a product of two smaller integer. Created a program on visual studio named PrimeNumberTest that does the following: 1) prompt the user for input of an integer 2) test if the integer is a prime number 3) display the test result  
Write a method which takes as input an integer, returns true if the integer is prime,...
Write a method which takes as input an integer, returns true if the integer is prime, and returns false otherwise. Do not import anything. If the input is negative, 0 or 1, false should be returned. If the input x is greater than 1, you can test if it is prime (inefficiently) by checking if it is divisible by any integer from 2 up to half of x. If it is not divisible by any of these numbers, then it...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a...
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a new array of that many double's initializes all elements to be 0 returns this array
(In Python) Complete the isPrime() function to take an int and return True if it is...
(In Python) Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not. Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test: if 7 remainder 6 is not 0, then if 7 remainder 5 is not 0, then if 7 remainder 4 is not...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Statistics Discussion: The accuracy of a forecasting technique is evaluated especially using the MSE (mean squared...
    asked 15 minutes ago
  • If the U.S. government manages to close a recessionary gap and achieve potential GDP with fiscal...
    asked 19 minutes ago
  • A block with mass 10kg is on a ramp angled at 20 degrees above the horizontal,...
    asked 19 minutes ago
  • I have a sample of 31 7thgrade girls who took an IQ test.  I calculated the sample...
    asked 27 minutes ago
  • A researcher wishes to estimate the proportion of adults who have​ high-speed Internet access. What size...
    asked 27 minutes ago
  • Brick column in the external corridor of a house, with section size of 440 mm X520...
    asked 28 minutes ago
  • 17.                             Mel has a(n) __________ lien on Ellen’s car after he replaced her clutch. The lien.
    asked 35 minutes ago
  • Jackson Company engaged in the following investment transactions during the current year. Feb 17,Purchased  430 shares of...
    asked 46 minutes ago
  • When might discrimination in the workplace be justified? Might discrimination on the basis of gender or...
    asked 57 minutes ago
  • The strength grade of materials used for brick masonry at a certain site is as follows:...
    asked 1 hour ago
  • Show (prove), from the original definition of the integers, that subtraction of integers is well defined....
    asked 1 hour ago
  • How is polarity of a "bond" different than polarity of a "molecule?" What makes a particular...
    asked 1 hour ago