Collapse
Write a program that prompts the user to input a positive
integer. It should then output a message indicating whether the
number is a prime number.
(Note: An even number is prime if it is 2. An odd integer is prime
if it is not divisible by any odd integer less than or equal to the
square root of the number.)
Turn in:
Your source code for with
The name of your program as a comment at the top of the file
Your IPO chart incorporated as comments after the name of the file
A screen shot or picture of the results after running your program with your test data.
==========================================
#include<iostream>
int main()
{
//Declare all required variables
int n;
int count=0;
cout<<"Enter a number: ";
cin>>n;
//Check all the numbers from 1 to n are
divisible by given number
for(int i=1;i<=n;i++)
{
//Condition to check number is
divisible by current i
if(n%i==0)
{
//If divisible
increment counter
count=
count+1;
}
}
//If count is 2 it is prime number
if(count==2)
{
cout<<n<<" is a prime
number."<<endl;
}
else
{
cout<<n<<" is not a
prime number."<<endl;
}
return 0;
}
==================================================================
IPO CHART:
--------
Input:
--------
Enter a number n
-------------
Processing:
-------------
Determine if the given number is
prime or not using division of each number <=n by below
logic
//Check all the numbers from 1 to n
are divisible by given number
for(int i=1;i<=n;i++)
{
//Condition to
check number is divisible by current i
if(n%i==0)
{
//If divisible increment counter
count= count+1;
}
}
----------
Output:
----------
If the count ==2 , display
prime
else display not a prime
==================================================================
Get Answers For Free
Most questions answered within 1 hours.