GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2004, 06:59
X-Man X-Man is offline
New Member
 
Join Date: May 2004
Location: Kingdom of Bahrain
Posts: 4
X-Man is on a distinguished road

Plz help me to write this program..


Hi All,

Who can i write a program that tests a number if its "prime" or not


regards
  #2  
Old 07-May-2004, 07:25
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
You must start on your own and come up with something and then ask specific questions when you are stuck. can't write your code for you
Hint: prime numbers are not naturally divisible by anything except themselves and 1.
Hint: all the prime numbers are odd except 2.
Examples: 2(only even prime number), 3, 5, 7, (not 9), 11 ....
  #3  
Old 07-May-2004, 07:47
X-Man X-Man is offline
New Member
 
Join Date: May 2004
Location: Kingdom of Bahrain
Posts: 4
X-Man is on a distinguished road
Hi

Thanks machinated,

Thats My program

________________________
CPP / C++ / C Code:
#include<stdio.h>

int main()
{
int a;

printf ("Enter a number to test it: ");
sancf ("d%",a);

if (a%2 !! a%3 !! a%5 == 0)  printf ("The number is not prime");
else printf ("The number is prime");


}
_________________

What do you say?

Regards
Last edited by dsmith : 07-May-2004 at 07:55. Reason: Please use [c] & [/c] for syntax highlighting
  #4  
Old 07-May-2004, 08:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi X-man, welcome to GIDForums.

First of all let me direct you to this thread. Therein is a lengthy discussion of prime numbers with various solutions and code snippets.

As for your code, I think that you are on the right track, but it is not complete. 49 is at least one number I can think of off the top of my head that would be shown as prime by your program. I think using what you have and some of the algorithms shown in that other thread, you should be able to solve this thing quite easily.

Good Luck,
d
  #5  
Old 07-May-2004, 09:38
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by X-Man
CPP / C++ / C Code:
if (a%2 !! a%3 !! a%5 == 0)  printf ("The number is not prime");
"!!" is not an operator... go to this webpage for a list of valid C++ operators.
__________________
-Aaron
  #6  
Old 07-May-2004, 11:28
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
i help u code 1 example of prime number
actually there is many way to write
but is almost similar
i not sure my code is short enough and efficiency
you try to understand it
you can reference but try not copy, cause programming is not just have a solution
they is many solution, is either efficiency or not.

CPP / C++ / C Code:
int main()
{
int a;
int is_prime=0;  // this is counter to check whether is any num and mod to 0
cin>>a;  // receive a digit from user
cout<<endl; // next line

for (int i=2; i<a; i++)  // check any num can be divided 
{                            // i must be start at 2 to (a-1)  e.g a=9, so i=2;i<9;i++
if(a%i==0)  //check any number can be mod to 0
is_prime++;  
}
if(is_prime==0)  // mean no any num can mod to 0
cout<<a <<" It is prime number "<<endl;

else // mean is_prime is >0
cout<<"Not prime number"<<endl;
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #7  
Old 09-May-2004, 09:25
X-Man X-Man is offline
New Member
 
Join Date: May 2004
Location: Kingdom of Bahrain
Posts: 4
X-Man is on a distinguished road

Thanks All


Hi all,

Thanks for your replay

I wrote the program befor i see your replys

Here is my "program"

CPP / C++ / C Code:
#include<stdio.h> 

int main() 
{ 


int a,b,num; 

b = 0 ; 

printf ("Enter a number: "); 
scanf ("%d",&num); 


for ( a = 2 ; a < 1000 ; a ++) 
{ 

if (a == num ) continue; 

if ( num%a == 0 ) b = 1; 



} 

if (b==1) printf ("Its not prime"); 
if (b==0) printf ("Its prime"); 

} 


  #8  
Old 10-May-2004, 01:37
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
I don't see why there is a need for a 'for' loop of 1000 when you have the number to validate. Well, you may not looking forward for effeciancy, but it is a good practice!

__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #9  
Old 10-May-2004, 02:05
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by X-Man
Hi all,

Thanks for your replay

I wrote the program befor i see your replys

Here is my "program"

CPP / C++ / C Code:
#include<stdio.h> 

int main() 
{ 
    int a,b,num; 
    b = 0 ; 

    printf ("Enter a number: "); 
    scanf ("%d",&num); 
    for ( a = 2 ; a < 1000 ; a ++)
    { 
        if (a == num ) continue; 
        if ( num%a == 0 ) b = 1; 
    } 

    if (b==1) printf ("Its not prime"); 
    if (b==0) printf ("Its prime"); 
} 
Fisrt off, watch your formatting. This program should be formatted like above.

The for loop should definitely be changed. Following are progressively better loops:
1) Don't loop to 1000. The number is your absolute max:
for ( a = 2 ; a <= num ; a ++)

2) Thinking about the loop, you also obviously only have to go thru half the numbers:
for ( a = 2 ; a < num/2 ; a ++)

3) Not so obviously, the real limit is the square root of the number:
for ( a = 2 ; a < sqrt(num) ; a ++)
For this you need to add the math header file.

4) Check for 2 first, the the loop can start at 3 and skip all even numbers:
CPP / C++ / C Code:
if (num == 2)
{
    prime = 1;
}
else
{
    for ( a = 3 ; a < sqrt(num) ; a+=2)
    {
    }
}

Finally, inside the loop all you need is:
CPP / C++ / C Code:
for ( a = 3 ; a < sqrt(num) ; a+=2)
{ 
    if ( num%a == 0 )
    {
        b = 1; 
        break;  // no need to continue thru the loop so leave
    }
} 

So the entire program can become:
CPP / C++ / C Code:
#include<stdio.h> 

int main() 
{ 
    int a,b,num; 
    b = 0 ; 

    printf ("Enter a number: "); 
    scanf ("%d",&num); 
    if (num == 2)
    {
        prime = 1;
    }
    else
    {
        for ( a = 3 ; a < sqrt(num) ; a+=2)
        {
            if ( num%a == 0 )
            {
                b = 1; 
                break;  // no need to continue thru the loop so leave
            }
        }
    }    for ( a = 2 ; a < 1000 ; a ++)

    if (b==1) printf ("Its not prime"); 
    if (b==0) printf ("Its prime"); 

    return 0;    // don't forget the return, main() is an int function
}
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #10  
Old 10-May-2004, 10:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Oops, the line prime = 1; should be b = 1; in the final code above.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06
Will pay money for someone to write a semi complex program bstan Computer Programming Advertisements & Offers 0 23-Feb-2004 04:17
How to write simple program like ........? laputa9000 C++ Forum 3 29-Oct-2003 13:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:11.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.