Question

Application program           Program name      PrimeNumbers.cpp           Problem background:        &n

Application program

          Program name      PrimeNumbers.cpp

          Problem background:

                   In mathematics, a PRIME number is a number that is divisible by only

                   itself and 1.

          Examples:

                   2, 3, 5, 7, 11 are all primes

                   these are not primes

the number 4        divisible by 2

the number 6        divisible by 2 and 3

the number 12       divisible by 2, 3, 4, and 6

Write a C program to find all the prime numbers between 0 and 151

(so check the numbers from 1 ~ 150)

          Output should look like:

                   The following numbers

                     are prime numbers

                             1

                             2

                             3

                             5

                             Etc

          hint – remember the mod ( % ) operator.   X % D means that what is the remainder after X / D

                                            or

                    returns the remainder after the left operator if divided by the right operator

                    so…15%5 is 0    15%2 is 1

                    if one number divides evenly into another, the mod is 0

                   so, for a given number (say X), divide all the numbers less than X into X. If none of the

                   numbers divide evenly (the result of the % operator is 0) into X, then X is a prime

                   number.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:-


As per Mathematics, prime number is the number greater than 1 that is divisible by itself and 1 only.

1 is not considered to be prime number.

Code:-

#include<stdio.h>
int main()
{
int l = 0,r = 151;//given values
bool k ;//boolean variable that shows whether number is prime or not. (true if prime)

printf("The following numbers are prime numbers\n");//print on screen

//run loop from given range l to r (both exclusive)
for(int i=l+1;i<r;i++)
{
k=true;//assume that number is prime
//run from 2 to number less than number to checked
for(int j=2;j<i;j++)
{
if(i%j==0)//if the number is divisible by number less than it except 1 then it is not prime
{
//if any of the number less than i divide the number evenly then
//k become false and comes out of inner loop by break statement
k = false;
break;

}
}
//1 should not be considered as prime
if(k==true && i>1)//if the number is prime.
//As per mathematics, prime number is number greater than 1 that is divisible by itself and 1 only.
{
printf("%d\n",i);//print the number followed by newline
}

}
return 0;
}

Output:-

Please appreciate the work by giving a thumbs up.

If any doubt, ask in comments.

Stay safe and keep Chegging.

Add a comment
Know the answer?
Add Answer to:
Application program           Program name      PrimeNumbers.cpp           Problem background:        &n
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
  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    17. Prime Numbers 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 S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or...

  • 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...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • 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...

  • 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...

  • In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13,...

    In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. A positive integer between 1 and 1000 (inclusive), other than the first 11 prime integers, is prime if it is not divisible by 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number...

  • 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...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

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
ADVERTISEMENT