Question

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.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

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

int main(){
   int n;
   cout<<"Enter a number: ";
   cin>>n;
   cout<<"Primes from 2 to "<<n<<" are:"<<endl;
   for(int i = 2;i<=n;i++){
      if(is_prime(i)){
         cout<<i<<endl; 
      }
   }
   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
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
1) Write a function in C that takes in a single integer parameter and returns an...
1) Write a function in C that takes in a single integer parameter and returns an array of factors. The function signature should be : int* get_factors(int number) To make things a little easier, assume that the input (number) will never have more than 100 factors. The returned array should be null-terminated. 2) Write a main() function that asks a user for a number, calls the function above, then prints the results.
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.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
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...
Write a program that takes a positive integer argument N, and prints out the average, minimum,...
Write a program that takes a positive integer argument N, and prints out the average, minimum, and maximum (in that order) of N uniform random numbers that it generates. Note that all three statistics should be over the same sequence of N random numbers.
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Python question: Write a function that takes a number and an integer (X,N) in a time...
Python question: Write a function that takes a number and an integer (X,N) in a time complexity of O(log(N)). for an example: calling power(2, 2) would return 4
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.