Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2008, 04:12 AM
dvdtomkins dvdtomkins is offline
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0
dvdtomkins is on a distinguished road
Default Prime numbers

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-08-2008, 09:08 AM
Irfanerol's Avatar   
Irfanerol Irfanerol is offline
Newbie
 
Join Date: May 2008
Posts: 8
Rep Power: 0
Irfanerol is on a distinguished road
Default Re: Prime numbers

I've written the program and it works very well.

You'll need a while and if loop. Moduler division will works!
I think you can solve the problem now, or if you want, I can send it here? I'm waiting your answer.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-08-2008, 11:06 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,584
Rep Power: 22
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Prime numbers

dvdtomkins, we do not do people's homework here, but will help if you post your code so far or let us know what issue you're having getting started.
__________________
Code:
int main() {
  set<string> Math, Programming;
  assert(Programming<Math);  
  return 0; }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-08-2008, 03:00 PM
Xav's Avatar   
Xav Xav is offline
Speaks fluent binary
 
Join Date: Mar 2008
Location: London, England
Posts: 1,303
Rep Power: 11
Xav is a jewel in the roughXav is a jewel in the roughXav is a jewel in the rough
Send a message via MSN to Xav
Default Re: Prime numbers

This looks like spam to me. I'm reporting.

EDIT:
Dunnit! Lihong, you're goin' down!
__________________
Xav, the power of youth
Web Site | Email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-10-2008, 11:52 AM
soku11 soku11 is offline
Newbie
 
Join Date: May 2008
Location: Warsaw, Poland
Posts: 10
Rep Power: 0
soku11 is on a distinguished road
Default Re: Prime numbers

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;
}
It returns true if the numer is a prime or false if it isnt.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-12-2008, 06:56 AM
dvdtomkins dvdtomkins is offline
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0
dvdtomkins is on a distinguished road
Default Re: Prime numbers

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-12-2008, 11:48 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,584
Rep Power: 22
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Prime numbers

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-12-2008, 01:59 PM
Xav's Avatar   
Xav Xav is offline
Speaks fluent binary
 
Join Date: Mar 2008
Location: London, England
Posts: 1,303
Rep Power: 11
Xav is a jewel in the roughXav is a jewel in the roughXav is a jewel in the rough
Send a message via MSN to Xav
Default Re: Prime numbers

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?
__________________
Xav, the power of youth
Web Site | Email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-13-2008, 02:23 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,584
Rep Power: 22
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Prime numbers

left-bracket + "CODE" + right-bracket. code goes here. left-bracket + "/CODE" + right-bracket.
__________________
Code:
int main() {
  set<string> Math, Programming;
  assert(Programming<Math);  
  return 0; }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-13-2008, 02:26 PM
Xav's Avatar   
Xav Xav is offline
Speaks fluent binary
 
Join Date: Mar 2008
Location: London, England
Posts: 1,303
Rep Power: 11
Xav is a jewel in the roughXav is a jewel in the roughXav is a jewel in the rough
Send a message via MSN to Xav
Default Re: Prime numbers

Code:
Oh ha ha. Now seriously, is there a keyboard shortcut?
__________________
Xav, the power of youth
Web Site | Email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 01:02 AM.

Contest Stats

No contest at this time. Please check back later.

Ads