Question

In: Computer Science

Write a program that finds and prints all of the prime numbers between 3 and X...

Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …).

One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divides n, then n must be prime.

Note: do not use break statements, jumps, and user defined functions.

also tell how many prime numbers are there from 2 to n.

the language to be used is c++

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main(){
   int X,i,j,c;
   cin>>X;
   for(i=3;i<=X;i++){
       c = 0;
       for(j=1;j<=i;j++){
           if(i%j == 0)
               c = c+1;
       }
       if(c == 2)
           cout<<i<<" ";
   }
  
   return 0;
}

Output :

First line is input

second line number are outputs


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:...
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the while loop. question2: Write a C++ program that prints all numbers between 10 to 1000 that are divisible by 5
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT