Question

In: Computer Science

IN C int count_primes(int start, int end) {   //TODO: return the count of prime numbers in...

IN C

int count_primes(int start, int end)

{

  //TODO: return the count of prime numbers in range [start, end] inclusive.

  return 0;

}

Solutions

Expert Solution

#include<stdio.h>
int count_primes(int start,int end)//function to return count of prime numbers
{
int flag, count=0, i, j;
for(i=start; i<=end; i++)
{
flag = 0;
//verifying whether number is divisible by any other number except 1
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0 && i>=2)
{
count++;//count the prime numbers
}
}
return count;
}
int main()//program execution starts here
{
int start, end;
printf("Enter starting number in range: ");
scanf("%d", &start);
printf("Enter ending number in range: ");
scanf("%d", &end);
//Prints count of prime numbers in given range[start,end]
printf("\n Number of prime numbers in the given range[start,end] = %d", count_primes(start,end));
return(0);
}


Related Solutions

Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 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:...
import kotlinx.coroutines.* // TODO 1 fun sum(valueA: Int, valueB: Int): Int {     return 0 }...
import kotlinx.coroutines.* // TODO 1 fun sum(valueA: Int, valueB: Int): Int {     return 0 } // TODO 2 fun multiple(valueA: Int, valueB: Int): Int {     return 0 } fun main() = runBlocking {     println("Counting...")     val resultSum = async { sum(10, 10) }     val resultMultiple = async { multiple(20, 20) }     // TODO 3     println() } TODO 1: Change it to suspend function with the following conditions: Has a waiting time of 3 seconds...
void printList(double * A, int start, int end ) { if (start == end) //base case...
void printList(double * A, int start, int end ) { if (start == end) //base case { cout << A[start] << endl; } else //recursive case { cout << A[start] << endl; printList(A, start + 1, end); } } int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 }; printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6 cout << endl; //HELP HERE: Using a recursive method on C++,...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
Write a program that prints the count of all prime numbers between A and B (inclusive),...
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 = The 5 digit unique number you had picked at the beginning of the semester 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,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT