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 code on C++ to find prime number series.
Write a code on C++ to find prime number series.
Use VB.net to create a loop that posts the first 100 prime numbers. Then, write code...
Use VB.net to create a loop that posts the first 100 prime numbers. Then, write code to confirm the numbers are prime.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
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...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT