Question

C++ Create a program that tells the user if the integer is a prime number or...

C++ Create a program that tells the user if the integer is a prime number or not. Please do not use flag or bool.

Homework Answers

Answer #1

Check  the integer is a prime number or not without using flag or boolean:

code:

#include <iostream>
using namespace std;

int main() {
int i, n; /* variable declaration */
cout <<"Enter a positive integer: ";
cin >> n; /* take user input */
  
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
cout<<n<<" is not prime"<<endl;
}
else {
for (i = 2; i<n; ++i) { /* run the loop 2 to n */
if (n % i == 0 && i!=n) {
cout<<n<<" is not prime"<<endl;
break;
}
}
}
if (i==n)
cout << n << " is a prime number"; /* print the output*/

return 0;
}

output screen:

//if you  have a any doubt please comment ...........

Know the answer?
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
Similar Questions
Create a program to ask the user for an integer number, positive only. If the number...
Create a program to ask the user for an integer number, positive only. If the number is between 0 and 255, printout the number in binary format to the screen. This will involve checking the bits, one by one, starting from the most significant place: 128. Use c++ 1. Modify the program to find the binary number, using a FOR loop, instead of the manual checking of each bit separately 2. What are the bitwise XOR and INVERSION operators? Find...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5
Create a program that prompts the user for an integer number and searches for it within...
Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? in java
A prime number (or a prime) is an integer greater than 1 that is not a...
A prime number (or a prime) is an integer greater than 1 that is not a product of two smaller integer. Created a program on visual studio named PrimeNumberTest that does the following: 1) prompt the user for input of an integer 2) test if the integer is a prime number 3) display the test result  
[JAVA] Write a program that prompts the user for an integer and displays if the provided...
[JAVA] Write a program that prompts the user for an integer and displays if the provided integer is a prime number or not. A prime number is a number that is divisible only by 1 and itself. First few prime numbers are 2,3,5,7,11,13 and so on. [using if-statements, and if-else-statement ]
Create a C++ Program that prompts the user to enter a number and compute for the...
Create a C++ Program that prompts the user to enter a number and compute for the factorial value. SAMPLE OUTPUT: ENTER A NUMBER: -5 zero & positive numbers only!!! ENTER A NUMBER: 5 Factorial of 5 : 5*4*3*2*1 = 120 DO NOT USE FUNCTION FOR THIS PROBLEM. THANK YOU   
Write a C program to check whether given number is prime or not by performing following...
Write a C program to check whether given number is prime or not by performing following steps: Define an integer variable as given_number.   Use the C function scanf to read the integer variable given_number. Use the conditional statement to find out given_number is prime or not prime. Print the final output as The number is prime or The number is not prime. Compile and test the program on CS Linux systems
Create a flowchart that asks the user to enter a number. (Assume this number is integer!)...
Create a flowchart that asks the user to enter a number. (Assume this number is integer!) If the number is multiple of 5 then show a message that says “HiFive”. If the number is multiple of 3 then show a message that says “HiThree”. If the number is multiple of 2 then show a message that says “HiTwo”. Then end the Flowchart. (Tip: Use the % symbol for modulus operation) (Tip2: If the number is not a multiple of 5,...
Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts...
Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts of the problem or you will lose points. Provide comments to explain each step. a. Write a function that takes as a parameter a positive integer and returns a list (array) of the prime factors of the given integer. For example, if the parameter is 20, you should return 2 2 5. b. Write a function that tests the above function by asking the...