|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
This is what I have to do in C++ but I'm stuck on how to go about it.
In this exercise write a program that detects if the integer inputed by the user is a prime number or a composite number. If you have not heard of these terms before then a prime number is a number that can only be divided by itself and one. A composite number is a number that has 2 or more factors. A factor is a number that will divide evenly into a number. If the number inputed by the user is a prime number then the program will output something similar to this Enter An Integer: 13 Your number 13 is a prime number If the number is a composite number then the program's output will be something like this Enter An Integer: 24 Your number 24 is a composite number. It's factors are 2, 3, 4 ,6, 8, 12 If you can complete this exercise without any difficulties then you can proceed to the next lesson. If you found that you had any problems with this exercise then contact your instructor about the problems and attach any working code so that we can discuss where your solutions lay. |
| Sponsored Links |
|
|
|
|||
|
Here my code for this function:
Code:
bool IsPrime( unsigned int number )
{
if( number<2 ) return false;
if( number==2 ) return true;
for( int i=2;i<=number/2;++i )
if( number%i==0 ) return false;
return true;
}
|
| Sponsored Links |
|
|
|
|||
|
I just wanted help getting started, anyway I found some help on another site and came up with this code:
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
int i;
int c=0;
cout<<"enter a number:";
cin>>n;
for(i=1;i<=n;i++) // below is the statements to test for a prime number
{
if(n%i==0)
{
c += 1;
}
}//end of for
if(c==2)
{
cout<<n<<" is a prime number";
}//end of if
else
{
cout<<n<<" is a composite number it's factors are";
for(int k=1; k< n;k++)
{
if(n%k==0)
{
cout<<","<<k;
}//end of if
}//end of for
}//end of else
return 0;
}// end of mainA
Last edited by WingedPanther; 05-12-2008 at 11:45 AM. Reason: add code tags |
|
|||||
|
dvdtomkins:
your code seems a little inefficient. I would run through the loop once, building the factors into a string. If the string is empty, output "prime", otherwise output the factors with 1 and n added to the list. This would also make your loop shorter, as you could loop from 2 to n/2. Also, please use code tags in the future.
__________________
Code:
int main() {
set<string> Math, Programming;
assert(Programming<Math);
return 0; }
Last edited by WingedPanther; 05-12-2008 at 11:49 AM. Reason: add note |
|
|||||
|
Yes, you only need to check the numbers between 2 and half of n, as once you go above halfway, it's impossible to fit in a whole number?
@ Winged, is there a keyboard shortcut for code tags? I know Ctrl-B is bold, I is Italic, U is underlined... how about code? |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Request] Numbers Programm! | pormadori | Visual Basic Programming | 4 | 03-15-2008 01:03 PM |
| Prime Numbers.. | TcM | Programming Theory | 8 | 01-15-2008 11:17 AM |
| finding square root and prime numbers.? | aladin | General Programming | 1 | 11-07-2007 07:49 PM |
| complex numbers | kenna | General Programming | 6 | 11-06-2007 10:32 AM |
| Javascript: Prime or not | reachpradeep | Javascript | 4 | 08-23-2007 08:58 AM |
No contest at this time. Please check back later.