View Full Version : Small problem
iggybaseball
04-09-02, 08:50 PM
this is my code in c:
#include <stdio.h>
main ()
{
int i;
int j;
printf ("Enter a number to see if it's prime\n");
for ( i = getchar (), j=2; j<=i; j++){
if (i == j) {
printf (" %d is a prime number\n", i);
break;
if (i%j == 0) {
printf (" %d is not prime, it can be divided by %d\n", i, j);
break;
}
return 0;
}
but whenever i type in a number 4, it says " 52 is not a prime number,it can be divided by 2"
and it does this to other numbers to. howcome its changin the numbers to diff numbers like in the 50's?
I bet 52 is the ascii char code for 4, because getchar is getting a char, not an int. You need to cast the char to an int, then do math on it.
Also (this may be me being rusty), your loop doesn't look quite right. I'd think you'd put j=0 in there, not j=2. Either that, or increment i. Its probably just me.
iggybaseball
04-10-02, 06:59 PM
Originally posted by XWRed1
I bet 52 is the ascii char code for 4, because getchar is getting a char, not an int. You need to cast the char to an int, then do math on it.
Also (this may be me being rusty), your loop doesn't look quite right. I'd think you'd put j=0 in there, not j=2. Either that, or increment i. Its probably just me.
i wrote the code last night fast an pretty late from memory of what i did earlier. i new it had to do with getchar. i didnt know u could use getint. would that make getfloat also? an what about unsigned long int?
Istari1
04-11-02, 12:24 AM
No, not getint() ...(gee not sure if that works..dont think it does). Do this
i = (int)getchar()
That will cast whatever getchar ses into the int primitive. havent used C (been stuck in ava world) in a long time. Was that your actualy final code? Cause you a misplaced curly bracket also. Theres faster ways of finding if something is a prime also, but hell if I know what they are, haha. I always hated math.
Josh
iggybaseball
04-11-02, 07:04 PM
Originally posted by Istari1
No, not getint() ...(gee not sure if that works..dont think it does). Do this
i = (int)getchar()
That will cast whatever getchar ses into the int primitive. havent used C (been stuck in ava world) in a long time. Was that your actualy final code? Cause you a misplaced curly bracket also. Theres faster ways of finding if something is a prime also, but hell if I know what they are, haha. I always hated math.
Josh
yea i just noticed that getint () didnt work. i will try (int)getchar() tonight. it is not my final code. i have it only testing odd numbers which i think i didnt put up their . plus i will get more complex after i get my first basic program. thanx a lot Istari
iggybaseball
04-11-02, 08:31 PM
(int)getchar () didnt work either it did the same thing
Istari1
04-12-02, 12:08 AM
I would just put the input statement outside the loop and use cin instead:
int i = 0;
cin >> i;
shrug, Sorry i dont ave a better answer for why the (int)getchar() didnt work, its been a while.
Josh
What you want to do is use getchar to store the character into a char, then use atoi() to convert the ascii to an int, and store that int in another variable.
iggybaseball
04-12-02, 07:59 PM
well since ive been having trouble doing it this way i just made a program that found out all the prime numbers from 1 to 1,000,000 and it came up with a number about 78, xxx something. does anybody know if this is right, i will try to repost with the actual number.
My program got 78,498 primes from 1 to 1,000,000. So far, it sounds like you're close.
JigPu
iggybaseball
04-12-02, 08:19 PM
how long does it take u to run? it takes mine about 10min, not sure how to integrate a time function yet. i have read some of ur old posts on prime numbers an im stuck on them like u r. i really like them an am going to find quicker ways to find them.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.