Question

In: Computer Science

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

Solutions

Expert Solution

#include<iostream>

using namespace std;

int main(){

int num,i,n=0, flag=0; /*we take num as a variable where value is take. we set flag to 0*/

cout<<"Enter the number:"; /*take input like 19*/

cin>>num;

n=num/2; /*first we divide by 2 if we divide 19 by 2 it provide 9*/

for(i=2;i<=n;i++) /*we start loop by 2 and it goes upto 9*/

{

if(num%i==0) /*19%2 its remainder 1 then loop increase by 1 now 19%3 then return 1 then it work upto 19%9 then again return 1 . now flag is 0 */

{

cout<<"Number is not Prime."<<endl;  

          flag=1;  

          break;  

}

}

if (flag==0) /*it Show prime number*/

      cout << "Your number is Prime."<<endl;  

  return 0;  

}

Output: Enter the number:19

Your number is prime.

If you like my answer please thumbs up.


Related Solutions

Write a C++ program to check whether a number is prime or not. A prime number...
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...
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