|
|
|
#1 (permalink) |
|
New to Overclock.net
|
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;
}
|
|
|
|
|
|
#2 (permalink) | |||||||||
|
ATI Enthusiast
![]() |
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
Last edited by gamervivek : 10-04-09 at 12:19 PM |
|||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Code:
if(num == 1) return true;
for(x = 2; x < num; x++) {
if(num % x == 0) return false; }
return true;
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Off By 340 Undecillion
![]() |
Quote:
Code:
...
if (i < isprime( number) ){
...
Code:
...
if (isprime( number) ){
...
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|