Question

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.

Homework Answers

Answer #1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int isPrime(int num)
{
   int i;
   if (num <= 1) return 0;
   if (num % 2 == 0 && num > 2) return 0;
   for (i = 3; i <= sqrt(num); i += 2)
   {
      if (num % i == 0)
         return 0;
   }
   return 1;
}

int main()
{
    int n, i;
   srand(time(NULL));
    
    for(i = 0;i<6;i++){
       n = 1+rand()%100;
        if(isPrime(n)){
            printf("%d is prime\n",n);
        }
        else{
            printf("%d is not prime\n",n);
        }
   }
   return 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
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...
/* 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...
An integer number is said to be a perfect number if it is equal to the...
An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a method called isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, call this method in the main method that determines and prints all the perfect numbers between 2 and 2000. WRITE...
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.
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.
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
Write a public method called containsDivPair that: • accepts three integer parameters • returns true if...
Write a public method called containsDivPair that: • accepts three integer parameters • returns true if any two of the numbers are divisible by 5, false otherwise
(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...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
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....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT