473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[HOMEWORK] How to check if a number is prime or not

64 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. /*    This program checks if a number is prime or not.
  2. */
  3.  
  4.  
  5. #include <stdio.h>
  6.  
  7. int isPrime (int n, int f);
  8.  
  9. //    Function Declarations 
  10. int main (void)
  11. //    Local Definitions 
  12. {
  13. int n,f, isprime;
  14. //    Statements 
  15.  
  16.    for ( n = 2; n<=10000; n++)
  17.     {   
  18.     if(n < 2)
  19.     break;
  20.     if(n>=2)
  21.     n = f;
  22.     if(n>=2)
  23.     isPrime(int n,int f)
  24.     }
  25.    return 0;
  26. }
  27.  
  28. /*    ==================== isPrime ====================
  29.     This function checks if a number is prime or not
  30.     using a simple algorithm that tries to find a divisor.
  31.     It stop searching at 1+ the integer part of the squre
  32.     root of the number being tested.
  33.        Pre   num
  34.        Post  returns 1 if num is prime, 0 otherwise
  35. */
  36. int isPrime(int n,int f)
  37. {
  38.      if(n==f)
  39.           printf("1");
  40.  
  41.      if(n%f==0 || n==1)
  42.           printf("0");
  43.  
  44.  return;
  45.  
  46. }
  47.  
  48.  
Feb 27 '07 #1
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.
Feb 27 '07 #2
RedSon
5,000 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. /*    ==================== isPrime ====================
  2.     This function checks if a number is prime or not
  3.     using a simple algorithm that tries to find a divisor.
  4.     It stop searching at 1+ the integer part of the squre
  5.     root of the number being tested.
  6.        Pre   num
  7.        Post  returns 1 if num is prime, 0 otherwise
  8. */
  9. int isPrime(int n,int f)
  10. {
  11.      if(n==f)
  12.           printf("1");
  13.  
  14.      if(n%f==0 || n==1)
  15.           printf("0");
  16.  
  17.  return;
  18.  
  19. }
  20.  
  21.  
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.
Feb 27 '07 #3
td0g03
64 New Member
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.
Feb 27 '07 #4
td0g03
64 New Member
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

Expand|Select|Wrap|Line Numbers
  1. int isPrime(int f, int temp)
  2. {
  3.  
  4.      if(f==temp)
  5.           return 1;
  6.      else
  7.      if(f%2==0 || f==1)
  8.           return 0;
  9.  
  10.  
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.
Feb 27 '07 #5
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.
Feb 27 '07 #6
td0g03
64 New Member
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.
Feb 27 '07 #7
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.
Feb 27 '07 #8
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....)
Feb 27 '07 #9
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.
Feb 27 '07 #10
td0g03
64 New Member
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.
Feb 27 '07 #11
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..."
Feb 27 '07 #12
meatstu
13 New Member
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
Feb 27 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2707
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...
11
5919
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...
2
4220
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 { ...
4
5203
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...
10
4401
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...
7
4113
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......
2
2575
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...
7
1842
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);
5
1692
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(){
0
6920
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...
0
7059
Oralloy
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,...
0
7103
jinu1996
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...
1
6758
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...
0
5362
agi2029
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,...
0
4499
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...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.