Write a C/C++ program which consists of a user-define function Prime. This function will take a number and check that the number is prime or not if the number is prime Display The number is Prime other wise display The number is not PrimeCall this function in main program.
Write a C/C++ program which consists of a user-define function Prime. This function will take a number and che
#include %26lt;iostream%26gt;
// The following function works on positive numbers only
bool Prime(int number);
int main( )
{
int number = 0;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; number;
if(Prime(number))
cout %26lt;%26lt; "The number is prime" %26lt;%26lt; endl;
else
cout %26lt;%26lt; "The number is NOT prime" %26lt;%26lt; endl;
return 0;
}
bool Prime(int number)
{
for(int i = 2; i %26lt; number - 1; i++)
{
if((number % i) == 0)
return false;
}
return true;
}
Reply:Don't post the question from your assignment to Internet; try to use your own imagination and skills you learnt from lectures; rather these assignments are brain teasers to make you ready for exams.
Reply:First thing you need to do is set a cin%26gt;%26gt; statement for the user to enter a number. Then have the an if-then statement that checks the number against the formula for determining if a number is prime (I dont remember if off the top of my head but its something like a number thats only divisable by itself). Then you can set your cout%26lt;%26lt; after the if as "The number",x,"is prime." and then for the else cout%26lt;%26lt; "The number",x,"is not prime." Should work out fine. No need to make it more complicated than it needs to be.
Reply:The code given by Silver is pretty good. I am only suggesting minor improvements to the same code
#include %26lt;iostream%26gt;
// The following function works on positive numbers only
// Haidry Comments:
// long is larger in size and hence can accommodate larger
// numbers. The prototype could be
// bool Prime(long number).
// Haidry Comments End
bool Prime(int number);
int main( )
{
int number = 0;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; number;
// Haidry Comments:
// Use of 'abs' function can eliminate the
// problem with negative numbers. The call could be like this
// if (Prime(abs(number))
// Haidry Comments End
if(Prime(number))
cout %26lt;%26lt; "The number is prime" %26lt;%26lt; endl;
else
cout %26lt;%26lt; "The number is NOT prime" %26lt;%26lt; endl;
return 0;
}
// Haidry Comments:
// If you changed the prototype above make necessary
// change here as well
// bool Prime(long number)
// Haidry Comments End
bool Prime(int number)
{
// Haidry Comments:
// The loop does not need to run till 'number -1'. It only
// needs to check integers up to half of the 'number'. So the
// for loop could be as following
//for(int i = 2; i %26lt; number / 2; i++)
// Haidry Comments End
for(int i = 2; i %26lt; number - 1; i++)
{
if((number % i) == 0)
return false;
}
return true;
}
flower girl dresses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment