Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-04-09   #1 (permalink)
New to Overclock.net
 
Join Date: Oct 2009
Posts: 5

Rep: 0 loonyt7 Unknown
Unique Rep: 0
Trader Rating: 0
Default Prime numbers in C

I have the below program and it runs ok but when the program is suppose to be telling me if a number is prime or not it tells me my first fumer is prime and all others are not prime and that is not right. Can someone help me with this.

Code:
#include <stdio.h>

int isprime ( int number ); 

int main ( void ) {
int value;
int i = 0;
int number;
int modulo;
printf("Enter 10 integers\n");
printf("I will tell you whether each one is prime or not\n");


for ( i = 0 ; i < 9 ; i ++){
printf("Enter an int: ");
scanf("%d", &value);

if (i < isprime( number) ){
printf("%d is a prime number\n", value);
}
else
printf("%d is not a prime number\n", value);

}
system ("Pause");
return (0);
}

int isprime ( int number ) {
int i = 2;
int flag = 1;
for ( ; i < number / 2 ;  );
{
int modulo = number % i ;
if ( i = 0 ) flag = 0;
else flag = 1;
}

return flag;
}
loonyt7 is offline   Reply With Quote
Old 10-04-09   #2 (permalink)
ATI Enthusiast
 
gamervivek's Avatar
 
intel ati

Join Date: Oct 2007
Posts: 1,875

Rep: 166 gamervivek is acknowledged by manygamervivek is acknowledged by many
Unique Rep: 130
Trader Rating: 0
Default

just tried running your code and the first error it commits is that the variable number is not initialised.So if it's working on your end i am guessing it's using a garbage value.you have to use value for that.
your isprime function seems to be borked.I understand that you are trying to get the remainder from the division of the number by all numbers from 2 to number/2.
However your for loop doesn't work correctly because of the semicolon at the end indicating that it finishes before it starts,so it's basically.....

just went through it again,the code seems to be broken everywhere,hopefully someone else can chime in.

edit: can you explain why you used the statements you did?

anyway here is a modified version of your code that works fine for me-

Code:
#include <stdio.h>

int isprime ( int number ); 

int main ( ) 
{
int value;
int i ;
int number;
int modulo;
printf("Enter 10 integers\n");
printf("I will tell you whether each one is prime or not\n");


for ( i = 0 ; i < 9 ; i ++)
 {
   printf("Enter an int: ");
   scanf("%d", &value);

if ( isprime(value) )
printf("%d is a prime number\n", value);

else
printf("%d is not a prime number\n", value);

}

system ("Pause");
return 0;
}

int isprime ( int number ) 
{
//int i = 2;
//int flag = 1;
int modulo;
for ( int i= 2; i <= (number / 2) ; i++  )
{
 modulo = number % i ;
 if ( modulo == 0 ) 
 return 0;
}

return 1;
}
__________________
there are only 10 kind of people in this world, those who understand ternary, those who don't, and then those who don't give a damn.

I see in Fight Club the strongest and smartest men who've ever lived. I see all this potential, and I see squandering...

So bye-bye, miss american pie,gamervivek has left the building.

Ati Tray Tools vs CCC

System: living on the edge BSODs galore
CPU
e2140@3.2Ghz
Motherboard
abit IP35-E
Memory
4GB 667@800 5-5-5-15
Graphics Card
HIS IceQ4 4850
Sound Card
soundblaster live! 24-bit
Power Supply
Corsair 450VX@stock
CPU cooling
Q6600's HSF
OS
win xp 32 bit

Last edited by gamervivek : 10-04-09 at 12:19 PM
gamervivek is offline   Reply With Quote
Old 10-04-09   #3 (permalink)
New to Overclock.net
 
Join Date: Oct 2009
Posts: 5

Rep: 0 loonyt7 Unknown
Unique Rep: 0
Trader Rating: 0
Default

The code I had posted was part of a code I was given to debug. What was there was all I had to work with. I had gotten through all of the problems except this last one that you helped me with. Thank you so much for the help on this.
loonyt7 is offline   Reply With Quote
Old 4 Weeks Ago   #4 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,263

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

Code:
if(num == 1) return true;
for(x = 2; x < num; x++) {
    if(num % x == 0) return false; }
return true;
That should do fine.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 4 Weeks Ago   #5 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,471

Rep: 368 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 276
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by loonyt7 View Post
I have the below program and it runs ok but when the program is suppose to be telling me if a number is prime or not it tells me my first fumer is prime and all others are not prime and that is not right. Can someone help me with this.

Code:
#include <stdio.h>

int isprime ( int number ); 

int main ( void ) {
int value;
int i = 0;
int number;
int modulo;
printf("Enter 10 integers\n");
printf("I will tell you whether each one is prime or not\n");


for ( i = 0 ; i < 9 ; i ++){
printf("Enter an int: ");
scanf("%d", &value);

if (i < isprime( number) ){
printf("%d is a prime number\n", value);
}
else
printf("%d is not a prime number\n", value);

}
system ("Pause");
return (0);
}

int isprime ( int number ) {
int i = 2;
int flag = 1;
for ( ; i < number / 2 ;  );
{
int modulo = number % i ;
if ( i = 0 ) flag = 0;
else flag = 1;
}

return flag;
}
The problem you describe occurs because of this line:
Code:
...
if (i < isprime( number) ){
...
The function isprime returns 1 or 0. It is being compared to i, which is only less than 1 on the first iteration of the input loop. If you remove the comparison to i the code will no longer exhibit that problem:

Code:
...
 if (isprime( number) ){
...
I know the OP is two weeks old and his assignment is probably long done by now, but I figured it might be good to have the answer anyway.
__________________
Congratulations! You have foundthe secret text! You get a cookie.

System: ½
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Reply


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



All times are GMT -5. The time now is 09:44 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.11596 seconds with 8 queries