Hello, the code below is what I got so far. I would like the loop to check numbers to 2-10000 and stop at 10000 and begins at 2. The question I have how do you make a function into a loop for. -
/* This program checks if a number is prime or not.
-
*/
-
-
-
#include <stdio.h>
-
-
int isPrime (int n, int f);
-
-
// Function Declarations
-
int main (void)
-
// Local Definitions
-
{
-
int n,f, isprime;
-
// Statements
-
-
for ( n = 2; n<=10000; n++)
-
{
-
if(n < 2)
-
break;
-
if(n>=2)
-
n = f;
-
if(n>=2)
-
isPrime(int n,int f)
-
}
-
return 0;
-
}
-
-
/* ==================== isPrime ====================
-
This function checks if a number is prime or not
-
using a simple algorithm that tries to find a divisor.
-
It stop searching at 1+ the integer part of the squre
-
root of the number being tested.
-
Pre num
-
Post returns 1 if num is prime, 0 otherwise
-
*/
-
int isPrime(int n,int f)
-
{
-
if(n==f)
-
printf("1");
-
-
if(n%f==0 || n==1)
-
printf("0");
-
-
return;
-
-
}
-
-
12 4418 RedSon 5,000
Recognized Expert Expert
You have a for loop and a function call already in your program. What are you asking about? You want to loop through all the numbers between 2 and 10000, but you already do that. You want it to loop back from 10000 to 2 when it finally reaches 10000, that can be easily done by checking if the current value is >= 10000 and if it is assigning a value of 2 to start all over again.
If you need other answers you are going to have to describe your question in more detail.
RedSon 5,000
Recognized Expert Expert -
/* ==================== isPrime ====================
-
This function checks if a number is prime or not
-
using a simple algorithm that tries to find a divisor.
-
It stop searching at 1+ the integer part of the squre
-
root of the number being tested.
-
Pre num
-
Post returns 1 if num is prime, 0 otherwise
-
*/
-
int isPrime(int n,int f)
-
{
-
if(n==f)
-
printf("1");
-
-
if(n%f==0 || n==1)
-
printf("0");
-
-
return;
-
-
}
-
-
Also your isPrime method does not return anything. I would assume that the compiler has complained about that to you already. If it hasn't I would suggest using another compiler.
You have a for loop and a function call already in your program. What are you asking about? You want to loop through all the numbers between 2 and 10000, but you already do that. You want it to loop back from 10000 to 2 when it finally reaches 10000, that can be easily done by checking if the current value is >= 10000 and if it is assigning a value of 2 to start all over again.
If you need other answers you are going to have to describe your question in more detail.
Sorry, let me try to make it more clear. I want to make a loop that is in main that loops from 2 to 10000. The loop will go through one function "isPrime". isPrime will check to see if a number is a prime or not and will return 1 if it is and if not return 0.
Also your isPrime method does not return anything. I would assume that the compiler has complained about that to you already. If it hasn't I would suggest using another compiler.
I notice that and changed it to -
int isPrime(int f, int temp)
-
{
-
-
if(f==temp)
-
return 1;
-
else
-
if(f%2==0 || f==1)
-
return 0;
-
-
The question I was asked was
Write a function named checkPrime that checks if a number is prime or not. In main() call checkPrime in a loop, to display all prime numbers between 2 and 10000. An integer is prime if its only positive divisors are itself and 1. For instance: 2, 3, 5, 7, 11, 13, 17, 19, 23 are prime numbers, and 4, 10,15, 22, 30 are not prime. A simple way of checking if a number is prime is to try to find a divisor other than 1 and itself; if there is at least one divisor the number is not prime, otherwise the number is prime. When to stop searching for divisors? For example, if we are checking if 47 is prime, we can stop at 7 which is 1 + the integer part of the square root of 47.
RedSon 5,000
Recognized Expert Expert
Sorry, let me try to make it more clear. I want to make a loop that is in main that loops from 2 to 10000. The loop will go through one function "isPrime". isPrime will check to see if a number is a prime or not and will return 1 if it is and if not return 0.
Your main method already has a loop in it that starts at 2 and goes to 10000 and it calls isPrime. Do you have more questions about that?
As for your isPrime method, it looks like you have got a good setup but it is going to require a bit more design in order to work properly.
Your main method already has a loop in it that starts at 2 and goes to 10000 and it calls isPrime. Do you have more questions about that?
Yeah. Am I correctly checking if its a prime or is it wrong? The result I get is completely wrong.
RedSon 5,000
Recognized Expert Expert
Yes the code you have in your isPrime method does not really do anything useful, perhaps it might be a good idea to spend some time trying to design a good algorithm for this. I do not recommend you just jump right in and try to program it straight away. Take some time to think about it first, and then post your results.
DeMan 1,806
Top Contributor
When to stop searching for divisors? For example, if we are checking if 47 is prime, we can stop at 7 which is 1 + the integer part of the square root of 47.
Exactly.....
(Also, just to be pedantic, your spec says you need a method checkPrime and your using isPrime....)
RedSon 5,000
Recognized Expert Expert
Exactly.....
(Also, just to be pedantic, your spec says you need a method checkPrime and your using isPrime....)
I saw that but I wasn't going to say anything, you are just splitting hairs there.
Exactly.....
(Also, just to be pedantic, your spec says you need a method checkPrime and your using isPrime....)
Actually, my teacher told me to use isPrime instead of checkPrime.
RedSon 5,000
Recognized Expert Expert
Actually, my teacher told me to use isPrime instead of checkPrime.
Well it doesn't really matter, as Shakespeare said "A rose by any other name..."
I cant quite see what youre trying to do with your bit of code but if you want to find if a number is prime why dont you try something like this:
1. Declare a variable, call it 'div' and initialise equall to 3.
2. Get the user to type in a number to be checked.
3. Divide that number by 2. If there is no remainder the number is not prime, Yes? If there is a remainder go to step 3.
4. Divide the number by the variable 'div'. If there is no remainder the number is not prime, if there is a remainder increment 'div' by 2 and repeat step 4.
Continue step 4 untill you either (a) you find the number is not prime or (b) div becomes larger than or equall to the number input by the user, in which case it is a prime number.
Hope it makes sense.
Stu
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Rahul |
last post by:
HI.
Python , with its support of arbit precision integers, can be great
for number theory. So i tried writing a program for testing whether a
number is prime or not. But then found my function...
|
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...
|
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
{
...
|
by: SweetLeftFoot |
last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a 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...
| |
by: Caffiend |
last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
|
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...
|
by: jamaicaboy |
last post by:
This is the code that i need some help with...........
#include<iostream.h>
#include<iomanip.h>
class test
{
public:
void store( int);
|
by: veki |
last post by:
Hello,
What shoud I change in this code to check, if some number is december:
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
|
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...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |