Question

In: Computer Science

Write a C++ program to check whether a number is prime or not. A prime number...

  1. Write a C++ program to check whether a number is prime or not. A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. Eg: 2, 3, 5, 7, 11, 13, 17, ...

    Sample Output:
    Input a number to check prime or not: 13 The entered number is a prime number.

    Input a number to check prime or not: 28 The entered number is not a prime number.

    Enhance the program to list all factors if the number is not prime Input a number to check prime or not: 28
    The entered number is not a prime number.
    Factors: 1, 2, 4, 7, 14, and 28

Solutions

Expert Solution

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

int main()
{
int n, m = 1;
cout<<"Input a number to check prime or not: ";
cin>>n;
int *count = new int; // dynamic intialization of an array
count[0] = 1; //the number 1 is alwas divides every numbers and this is 1st index in the array
bool flag;
for(int i=2; i <= n/2; i++) //the index "i" count from 2 to falf of the given number
{
if(n%i == 0) // if the number n is divisible by i then it is not a prime number
{
count[m++] = i; //stores the all the factors of the given number n
flag = true; //set to true if the number is not a prime
}
}
if(flag == false)
cout<<"The entered number is a prime number";
else
{
cout<<"The entered number is not a prime number \n";
cout<<"Factors: ";
for(int i=0;i<m;i++)
cout<<count[i]<<", ";
cout<<"and "<<n;
}
return 0;
}


Related Solutions

Write a program in C++ to check whether a number is prime or not
Write a program in C++ to check whether a number is prime or not
C++ Create a program that checks whether a number is a prime number and displays its...
C++ Create a program that checks whether a number is a prime number and displays its factors if it is not a prime number. Console Prime Number Checker Please enter an integer between 1 and 5000: 5 5 is a prime number. Try again? (y/n): y Please enter an integer between 1 and 5000: 6 6 is NOT a prime number. It has 4 factors: 1 2 3 6 Try again? (y/n): y Please enter an integer between 1 and...
C Programming Question: Q) Write a C - program to check whether a given graph is...
C Programming Question: Q) Write a C - program to check whether a given graph is Bipartite using Breadth-first search (adjacency list) Do take your time but please do post full code and also please do it in C.
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
Write a code on C++ to find prime number series.
Write a code on C++ to find prime number series.
Write a program that takes an integer as input and returns whether it is a prime...
Write a program that takes an integer as input and returns whether it is a prime number or not in C++. (Using if, loops, or/and bool only)
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
For C++ IDE Write an C++ to check if a number is falling within the range...
For C++ IDE Write an C++ to check if a number is falling within the range j...k (inclusive). Ask the user to enter both ranges j, K and the number num you want to check. Output the suitable message based on the situation Within the range Outside the range Always prompt the user to enter a value using a suitable message. For example (multiple runs) Enter J - beginning of the range: 3 Enter K – end of the range:...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT