Question

mes A. Hotz Programming Challenge: Prime qumber Array Values from Canvas: ICE12A Please name your source file: PC7101.CPP A p
this is c++ and please let the code works on vs2017. the output should be same as the question.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey here is answer to your question

#include <iostream>
#include <iomanip>
using namespace std;

//Function to check if number is prime
bool isPrime(int n)
{ //looping over number 2 to n (not starting with 1 as everything is can be divided by one)
    for (int i = 2; i < n; i++)
       //check if i can divide n
        if (n % i == 0)
           //if i can divide n then its not a prime number
           // as a prime number can only divide by 1 or itself
            return false;
    // if it si not divided by anything then its a prime number
    return true;
}

int main () {
   //counter which will be used to print new line
   int counter=0;

   // array to store prime numbers where each element in starting is 0
   int primeArray[1000]={0};

   //looping over 1 to 1000
   for (int i = 1; i <= 1000; i++)
       // checking if numebr is prime
       if (isPrime(i))
           // if number is prime add it to the array
           primeArray[i]=i;
  
   // looping 0 to 1000 to check array
   for (int i = 0; i < 1000; i++){
       //as we had all elemets in array "0" and 0 cant be a prime numeber
       //so we use that to print upto the last prime numebr upto 1000
       if (primeArray[i]!=0){
           // used setw()
           cout<< setw(8);
           // printing prime number
           cout<<primeArray[i];
           // increaseing the counter
           counter++;
      
       // now here the counter is used
       // we need to print 10 numbers in each row
       // and we incremented the counter so if it is 10 print a new line
       if(counter==10){
           printf("\n");
           counter=0;
           }
       }
   }
   //printing a new line at the end
   printf("\n");
  
return 0;
}

In case of any doubt please comment. Happy Learning :)

root@kali.-/HomeworkLib/april/isprime# g++ isprime.cpp root@kali.-/HomeworkLib/april/isprime# ./a.out 3 101 103 157 211 269 331 389 449 5

Add a comment
Know the answer?
Add Answer to:
this is c++ and please let the code works on vs2017. the output should be same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a prime number is a number that is only evenly divisible by itself and 1. for...

    a prime number is a number that is only evenly divisible by itself and 1. for example the number 5 is prime because it can only be evenly divided by 1 and 5 the number 6 however is not prime because it can be divided evenly by 1,2,3 and 6. write a function name isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. use this functuion in a...

  • A prime number is a number that can be evenly divided by only itself and

    A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5. The number 6, however isn't prime because it can be evenly divided by 1,2,3, and 6. Write a bool function named isprime that takes an integer as an argument and returns true if the argument is a prime number or false otherwise. Use the function in...

  • A prime number is a method that is evenly divisible by only itslef and 1. Write...

    A prime number is a method that is evenly divisible by only itslef and 1. Write a method called isPrime, which takes only one integer as an argument and returns true if the number is prime or false if the number is not prime. Create a method to store a list of all the prime numbers from 1 to 100 in a file called Primes.txt. Use the package primes; at the beginning of your code and be sure to document...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

    Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4: A palindromic prime number is a number that is both prime number and a palindrome number. For example, 131, 313, and 757 are palindromic prime numbers. Design (pseudocode) and implement (source code) a program (name it PalindromicPrime) to display the first 50 palindromic prime numbers, 10 per line separated by one space. The program defines the following methods: Method isPalindome() to check if a...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • Please paste your code and a screenshot of your output! 1. An integer n is divisible...

    Please paste your code and a screenshot of your output! 1. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program to determine whether or not the following numbers are divisible by 9: n= 154368 n 621594 n-123456 2. A number is said to be perfect if the sum of its divisors (except for itself) is equal to itself. For example, 6 is a perfect number because the sum of...

  • I need a code and its java Blue jay please its almost due Week 8 Quiz...

    I need a code and its java Blue jay please its almost due Week 8 Quiz Submitting a file upload File Types pdf Due Friday by 11:30am Points 10 Available after Oct 25 at 10:45am With your team, write a program that inputs a 3-digit integer, like 731. Then the program should multiply the number by 1001, which would make 731 become 731731. Next, write a for loop with the following header: for (int i =7;i<=13; i+=2) In the for...

  • Write a procedure stol in scheme that outputs a set of the M smallest prime numbers...

    Write a procedure stol in scheme that outputs a set of the M smallest prime numbers in increasing order. Use the algorithmic idea known as a sieve. Let S be the solution list, initially empty. Begin with the consecutive list of numbers L = 2,3,4, ... ,N where N is sufficiently large, which means at least equal to the Mth smallest prime number (unfortunately one cannot assume that the Mth smallest prime number is known when the code is run)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT