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].
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 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...
(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.
Write a java code to find the following. For numbers 501 to 999, how many numbers...
Write a java code to find the following. For numbers 501 to 999, how many numbers will have the sum of the digits equal to 10. 501, sum of digits=6 502, sum of digits=7 503, sum of digits=8 504, sum of digits=9 505, sum of digits=10 506, sum of digits=11
C++. Write a program that will read in id numbers and place them in an array.The...
C++. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please enter an...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT