I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not.
can anyone see what the problem is . thanks in advance
#include <stdio.h>
#include <math.h>
#define TRUE 1;
#define FALSE 0;
void getNumber();
int main()
{
int number;
getNumber(&numb er);
if (isPrime(number ))
printf("\n%d is a prime number\n", number);
else
printf("\n%d is not a prime number\n", number);
return 0;
}
void getNumber(int *number)
{
printf("Please enter a positive number ");
if (scanf("%d", &number) = 2)
{
printf("Invalid number entered\n");
exit(1);
}
}
int isPrime(int number)
{
int count, s;
/* Every even number is not prime */
if (number % 2 == 0) return TRUE;
/* check every odd number up to the square root of the number */
s = sqrt(number);
for (count=3; count<=s; count+=2);
{
if (number % count == 0) return TRUE;
}
return FALSE;
}
5 2538 sicarie 4,677
Recognized Expert Moderator Specialist
I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not.
can anyone see what the problem is . thanks in advance
#include <stdio.h>
#include <math.h>
#define TRUE 1;
#define FALSE 0;
void getNumber();
int main()
{
int number;
getNumber(&numb er);
if (isPrime(number ))
printf("\n%d is a prime number\n", number);
else
printf("\n%d is not a prime number\n", number);
return 0;
}
void getNumber(int *number)
{
printf("Please enter a positive number ");
if (scanf("%d", &number) = 2)
{
printf("Invalid number entered\n");
exit(1);
}
}
int isPrime(int number)
{
int count, s;
/* Every even number is not prime */
if (number % 2 == 0) return TRUE;
/* check every odd number up to the square root of the number */
s = sqrt(number);
for (count=3; count<=s; count+=2);
{
if (number % count == 0) return TRUE;
}
return FALSE;
}
I see a few problems: -
if (scanf("%d", &number) = 2)
-
Here you set the number to equal 2. You want '==' to make a conditional. -
if (number % 2 == 0) return TRUE;
-
You return true for an even number? On a function titled 'isPrime()'? I believe you want the '!=' operator there.
If you're still getting odd values after that, I would recommend using parentheses around the individual statements, to show order of operations.
Banfa 9,065
Recognized Expert Moderator Expert
I see a few problems: -
if (scanf("%d", &number) = 2)
-
Here you set the number to equal 2. You want '==' to make a conditional.
I don't think this sets number, on the other hand I am surprised it compiles as I believe it is trying to set the return value of the function scanf which is not an lvalue. -
if (number % 2 == 0) return TRUE;
-
You return true for an even number? On a function titled 'isPrime()'? I believe you want the '!=' operator there.
On the right lines but not quite right, just because it isn't divisible by 2 doesn't make it prime!
Hi silversnake, your function is returning the logical inverse (i.e. true for false and false for true) of what it names suggests (the first clue is in the numbers you give 4 (not prime) as prime 5 (prime) as not prime). It would be more aptly named isNotPrime.
this is not da way u do prime chk.what u have 2 do is take a no. and chk with all its preceding no.s if it is divisible or not if so in any case( break;) after setting a flag=1. And chk if flg is 1 then not prime else otr way . this is da simplest way there are other methods also.try this.
Banfa 9,065
Recognized Expert Moderator Expert
this is not da way u do prime chk.what u have 2 do is take a no. and chk with all its preceding no.s if it is divisible or not if so in any case( break;) after setting a flag=1. And chk if flg is 1 then not prime else otr way . this is da simplest way there are other methods also.try this.
Actually once the mistakes are removed (I just spotted an erroneous ; following a for statement) the algorithm the OP has tried to implement works fine with the 1 exception of accidentally identifying the number 2 as not prime.
thanks for the help , it works now
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dag |
last post by:
Is there a python module that includes functions for working with prime
numbers? I mainly need A function that returns the Nth prime number and
that returns how many prime numbers are less than N, but a prime number
tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range
so it would have to fairly efficient
Dag
|
by: lostinpython |
last post by:
I'm having trouble writing a program that figures out a prime number.
Does anyone have an idea on how to write it? All I know is that n > 2
is prim if no number between 2 and sqrt of n (inclusivly) evenly
divides n.
|
by: don |
last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....
|
by: wudoug119 |
last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me...
int Prime(int prime) //declares isPrime as a function using integers
{
int primeCount = 2; //declares primeCount as an int variable and sets it equal to 2
while(primeCount < prime) //checks if primeCount is less than prime
{
|
by: Joel Mayes |
last post by:
Hi All;
I'm teaching myself C, and have written a prime number generator. It is
a pretty inefficient implementation of the Sieve of Eratosthenes to
calculate primes up to 1,000,000. If anyone has time to critic and offer
my some feedback I'd be grateful
Thanks
Joel
| |
by: rhle.freak |
last post by:
Here is my code to generate prime numbers.It works absolutely fine when
the range is
*not very large*. However on initializing i with a large integer it
produces erroneous results
(some numbers ending in 5 ..which obviously cannot be prime numbers)
can anyone please help me out with the reason??
/*Generate Prime Numbers*/
#include<stdio.h>
|
by: newstips6706 |
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?
|
by: QHorizon |
last post by:
Hello, I'm new to Python (I've learned everything up to iterators so
far) and fairly new to Programming. This would be my first real
program:
#Coordinate Geometry (The whole program is not shown)
import math
import sys
print "Welcome to the Coordinate Geometry Calculator!"
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |