Question

In: Computer Science

write a code using c++. Find the first 10 prime numbers and store them in an...

write a code using c++. Find the first 10 prime numbers and store them in an array.

Solutions

Expert Solution

#include<iostream>
using namespace std;

// a function that returns true if the input integer x is prime, false otherwise
bool isPrime(int x){
   if(x < 2)
       return false;   // primes start from 2, so x is not prime

   // check for any possible division part from 1 or itself
   for(int n = 2; n < x; n++)
if(x % n == 0)                      
return false; // x is not prime
return true;           // x is prime
}

int main(){
   int N = 10; // the number of primes required
   int primesArray[N]; // array to hold the prime numbers

int x = 0;       // the starting guess of a prime
int i = 0;     // the counter form primes
   while(i < N){
       if(isPrime(x)){   // call the function to check if this x is prime
           primesArray[i] = x; // store x as a prime
           cout<<primesArray[i]<<endl;   // show contents of array in console
           i++;                // count this prime
       }
       x++; // next number to check if prime
   }

   return 0;
}

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~


Related Solutions

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.
Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
You must write a C program that prompts the user for numbers and multiplies them using...
You must write a C program that prompts the user for numbers and multiplies them using "a la russe" multiplication. No input is allowed at execution time (no command line input). The program MUST DISPLAY YOUR BANNER LOGO as part of a prompt to the user. A menu must allow the user to put in two numbers at the same time or each of two numbers one at a time. The valid range of values for each number is 0...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
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:...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending...
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending order. Use a stack to store the prime numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT