I'm trying to write a C program which prints "Yahoo!" if the variable testingnumber is prime, and "Google." otherwise. I know, I'm crazy. It's for something else, but I'm starting low-level to test my function. No matter what I set testingnumber to, my program prints "Google." Of course, this is terrible. What's wrong with my program (below)?
I'm not sure how the break will react on line 14, but I'd remove lines 16-19, there is no need for it, atleast not that I'm aware of. And line 14 can be simplified by just putting return false; and then completely remove the variable prime as if it makes it the whole way through the for loop, it must be prime.
And I was under the impression the "bool" datatype didn't exist in C, only C++. You're also mixing up the true and TRUE/false and FALSE. As I don't know much more about C, that is about the best I can help you.
#include <stdio.h>
#include <simpio.h>
#include <genlib.h>
#include <math.h>
bool isPrime (int N)
{
int check, num;
for (check=2; check<=sqrt(num); check++)
{
if (num%check==0)
{
returnfalse;
}
}
}
main()
{
int testingnumber=7;
if (isPrime(testingnumber)==TRUE)
{
printf ("Yahoo!\n");
}
else
{
printf ("Google.\n");
}
system ("PAUSE");
}
This still does not work. While I truly appreciate the fact that you are trying to help me, there are some things which you said that aren't true and/or aren't really valid.
1. The capitalization of True/False does not matter. I have tested this myself.
2. The break; is required, since the program wouldn't make sense otherwise.
Your tips about shortening the program make sense, however, and, again: I appreciate it very much! However, none of what you have mentioned has turned out to make the program work. I'm not blaming you. I'm having trouble myself on this. But, proof that my function doesn't have any problems lies in the fact that the following program (made for solving a problem) works:
I was doing research about the book data type in C and the results all said the same. Datatype bool is not standard in C. With that being said, I'll reply shortly with some code that should work. Do you get the same kind of results using C++ as you do using the C code?
Like I said, I'm not 100% sure about C syntax, but I do know some basic things. I will attempt to do my best to solve this while staying within the requirements of C.
Parameter supplied to isPrime() is not used later in function. Thus, program does not even consider the input ( in this case 7 ). Variables num and check are not initialized, so the result of num % check is always 0 or false (isPrime() always returns false). Hence "Google" keeps on poping out.
As already it was said your function does not use passed to it argument. Instead of N it uses uninitialized variable num.
I would rewrite the function the following way
1 2 3 4 5 6 7 8 9 10 11 12 13
_Bool isPrime( int n )
{
if ( ( n = abs( n ) ) < 2 ) return ( 0 );
_Bool prime = 1;
for ( int divisor = 2; prime && divisor <= sqrt( n ); divisor++ )
{
prime = n % divisor;
}
return prime;
}
typedefunsignedshort BOOL;
#define FALSE 0
#define TRUE 1
BOOL isPrime(int num)
{
if (num <= 1)
return FALSE;
if (num <= 3) //to avoid confusion, 2 or 3, if it was 1 or less it would've exited already
return TRUE;
for (int i = 2; i <= num/2; i++)
if (num % i == 0)
return FALSE;
return TRUE;
}
Compiles as C, works standalone, returns TRUE or FALSE.
#include <stdio.h>
#include <genlib.h>
#include <simpio.h>
#include <math.h>
main()
{
int check, num;
unsignedlonglong Sum=0;
bool prime;
for (num=50; num<=100; num++)
{
for (check=2; check<=sqrt(num); check++)
{
if (num%check==0)
{
prime=FALSE; break;
}
else
{
prime=TRUE;
}
}
if (prime==TRUE)
{
printf ("%d\n", num);
Sum=Sum+num;
}
}
printf ("Sum = %d\n", Sum+328);
system ("PAUSE");
}
^ This works. Yet, the program given on the original post of this thread does not work. All my syntax is correct. I'm not seeing any obvious errors. Please forgive me. I have posted in the Beginners forum because I am a beginner. I seem to be missing something quite obvious but I fail to understand what it is.
Thanks, again, for your attempts to help me!
~ Ahaan.
Are you programming in C or C++? A lot of your code looks like C, but I think some things aren't valid in C but only valid in C++, and vice versa.
If you're using C++ I can help understand it a little better, but again, I have no idea what language you're using. Your most recent program can be greatly simplified as well.
Also, when you print out sum, why do you add 328 to it?
I took your first program and ran it with gcc and got a bunch of issues with the genlib and simpio headers so I removed them. Maybe these have declarations for TRUE/FALSE? But here is your first program, WORKING and simplified:
#include <stdio.h> // Used for printf()
#include <math.h> // Used for sqrt()
// Returns true is num is prime
bool isPrime (int num)
{
for (int check=2; check<=sqrt(num); check++)
// If num is divisible by check, it is not prime; return false
if (num%check==0)
returnfalse;
// num was prime; return true
returntrue;
}
int main()
{
int testingnumber=7;
if (isPrime(testingnumber))
printf ("Yahoo!\n");
else
printf ("Google.\n");
return 0;
}
Edit: I just read up on the simpio and genlib header files and they're used to add features into the C language...but since you said FALSE and false were the same values, you're using C++ which means you don't need those header files anyways. Hope this helps.
I am using C, even though I have been told in the past that what I am using does look kind of like a hybrid of C and C++. =P
About the 328, I was doing it for a problem. Nothing to do with the program itself. While I understand what I am being told, my question is still unanswered. Yes, my original version was unnecessarily complicated. But it worked! So, it should work here as well. My question is why it isn't working here.
Your original version's function, as has been pointed out previously, ignored the passed integer it was supposed to check. Instead, it used an uninitialized local variable, which is very likely not to be prime.