# How do I get program to identify prime number?

• (2 Pages)
• 1
• 2

## 19 Replies - 1192 Views - Last Post: 27 January 2013 - 11:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309531&amp;s=50e62b4bb273ef7a62b6a8c83eec877f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

# How do I get program to identify prime number?

Posted 26 January 2013 - 05:08 PM

Here is my code thus far:
```#include <math.h>
#include <algorithm>

int main()
{
int a, b, c, d, e, f, g, h, i, j, s, x;
int largest, smallest;

printf("Enter 10 digits\n");
scanf("%d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);

if (a > b && a > c && a > d && a > e && a > f && a > g && a > h && a > i && a > j)
largest = a;
else if (b > a && b > c && b > d && b > e && b > f && b > g && b > h && b > i && b > j){
largest = b;}
else if (c > a && c > b && c > d && c > e && c > f && c > g && c > h && c > i && c > j){
largest = c;}
else if (d > a && d > b && d > c && d > e && d > f && d > g && d > h && d > i && d > j){
largest = d;}
else if (e > a && e > b && e > c && e > d && e > f && e > g && e > h && e > i && e > j){
largest = e;}
else if (f > a && f > b && f > c && f > d && f > e && f > g && f > h && f > i && f > j){
largest = f;}
else if (g > a && g > b && g > c && g > d && g > e && g > f && g > h && g > i && g > j){
largest = g;}
else if (h > a && h > b && h > c && h > d && h > e && h > f && h > g && h > i && h > j){
largest = h;}
else if (i > a && i > b && i > c && i > d && i > e && i > f && i > g && i > h && i > j){
largest = i;}
else if (j > a && j > b && j > c && j > d && j > e && j > f && j > g && j > h && j > i){
largest = j;}

if (a < b && a < c && a < d && a < e && a < f && a < g && a < h && a < i && a < j)
smallest = a;
else if (b < a && b < c && b < d && b < e && b < f && b < g && b < h && b < i && b < j){
smallest = b;}
else if (c < a && c < b && c < d && c < e && c < f && c < g && c < h && c < i && c < j){
smallest = c;}
else if (d < a && d < b && d < c && d < e && d < f && d < g && d < h && d < i && d < j){
smallest = d;}
else if (e < a && e < b && e < c && e < d && e < f && e < g && e < h && e < i && e < j){
smallest = e;}
else if (f < a && f < b && f < c && f < d && f < e && f < g && f < h && f < i && f < j){
smallest = f;}
else if (g < a && g < b && g < c && g < d && g < e && g < f && g < h && g < i && g < j){
smallest = g;}
else if (h < a && h < b && h < c && h < d && h < e && h < f && h < g && h < i && h < j){
smallest = h;}
else if (i < a && i < b && i < c && i < d && i < e && i < f && i < g && i < h && i < j){
smallest = i;}
else if (j < a && j < b && j < c && j < d && j < e && j < f && j < g && j < h && j < i){
smallest = j;}

s = sqrt(double(smallest));

printf("Your smallest number is: %d\n", smallest);
for(int x = 2; x <= s + 1; x++)
if(!(smallest % x)){
printf("%d is prime", smallest);}

printf("Your largest number is: %d", largest);

getch();
return(0);
}

```

This outputs:
Enter 10 digits
blah blah entering 10 digits
Your smallest number is: (gives correct smallest number)
Your largest number is: (gives correct largest number).

So this basically works... I need it to sort through 10 randomly entered number for largest and smallest and it does this, but I also need it to detect if the largest and smallest are prime or not, and if they are prime, a message must say so...

After some digging online I found to do this:
s = sqrt(double(smallest));

printf("Your smallest number is: %d\n", smallest);
for(int x = 2; x <= s + 1; x++)
if(!(smallest % x)){
printf("%d is prime", smallest);}

but... No print screen ever comes up between "your smallest number is:" and "your largest number is:"

Is that even remotely correct to detect a prime number? and if so how do I get the printf to pop up?

This post has been edited by jimblumberg: 26 January 2013 - 07:20 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Is This A Good Question/Topic? 0

## Replies To: How do I get program to identify prime number?

### #2 stackoverflow

Reputation: 166
• Posts: 545
• Joined: 06-July 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 06:05 PM

### #3 xtreampb

• D.I.C Regular

Reputation: 14
• Posts: 343
• Joined: 20-June 07

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 07:59 PM

well mod results in a float most of the time. you are storing a float in an integer. That may lead you in a better direction.

### #4 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 09:03 PM

lol that stack overflow kiinda helped i guess..... I have now:
printf("Your smallest number is: %d\n", smallest);
{
for(int x = 1; x <= smallest; x++)
{
if(smallest%2 !=0);{
printf("%d is prime\n", smallest);}
}
}

Only problem now is that it thinks every smallest number is a prime... and it repeats the message like 20 times.... gettin there... lol

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,709
• Joined: 08-August 08

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 09:14 PM

Putting aside optimizations, a number is prime if it is only divisible by itself and 1. That means that if it is divisible by any other number it is not prime.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 09:16 PM

Bawnawgwa, on 26 January 2013 - 09:03 PM, said:

lol that stack overflow kiinda helped i guess..... I have now:
printf("Your smallest number is: %d\n", smallest);
{
for(int x = 1; x <= smallest; x++)
{
if(smallest%2 !=0);{
printf("%d is prime\n", smallest);}
}
}

Only problem now is that it thinks every smallest number is a prime... and it repeats the message like 20 times.... />/>/>/>/>/> gettin there... lol

Delete the semi-colon at the end of the if statement. Never put a semi-colon on the end of an if statement.

You should be testing from 2 up through the square root of the number you want to test. So mod 2 is wrong. It should be mod x, not mod 2. The parameter for the function should be int number, since you need to pass in first the smallest number, and then the largest number.

And any integer mod any integer, will ALWAYS be an integer, never a float.

```   //in the calling function:
if(isPrime)
printf("Smallest number: %d, is prime\n",smallest);
else
printf("Smallest number: %d, is not prime\n",smallest);

//repeat the above with the largest number.

{
int limit = sqrt(number)+1;
for(int x = 2; x < limit; x++)
{
if(number%x ==0) {
printf("%d is NOT prime\n", number);
return 0;
}
}
return 1;
}

```

This post has been edited by Adak: 26 January 2013 - 10:40 PM

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 09:29 PM

WTF is line 12 through line 53??

Dear God, make it stop! />

This post has been edited by Adak: 26 January 2013 - 09:32 PM

### #8 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 09:34 PM

Ok, well update. I now have a program that recognizes lets me enter 10 digits, picks out correct smallest and largest, and recognizes if they are prime or not , and everything works smashing if they are not prime, but if they are prime... it repeats my "(number) is prime" statement over and over and over and over again... how do i only make it produce that text once?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 10:14 PM

You've been given the right logic in code, in reply #6. If you choose to ignore it, then why are you here?

### #10 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 10:18 PM

Cuz I had a code that recognized primes, but just spat the sentence at me tons of times. I tried your code in the order you listed it and Visual studio was giving me tons of problems that were not actually there... Biggest one was with sqrt... I have never been able to get it to work.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 10:37 PM

Try this, and study it. I can't tell you how to fix your code.

P.S. Just because you get a right answer sometimes, doesn't mean your code is correct in it's logic.

```#include <stdio.h>
#include <math.h>

int main() {      //should always be int main(), never void main().

int i,min,max,isPrime=0;
int num[10]={7,11,4,6,12,9,13,5,8,10};

min=num[0];
max=num[0];
for(i=0;i<10;i++) {
if(num[i] < min)
min=num[i];

if(num[i] > max)
max=num[i];

}
//in the calling function:
if(isPrime)
printf("Smallest number: %d, is prime\n",min);
else
printf("Smallest number: %d, is not prime\n",min);

//repeat the above with the largest number (max).

return 0;
}

int limit = sqrt(number)+1;
printf("limit:%d\n",limit);
for(int x = 2; x < limit; x++)
{
if(number%x ==0) {
return 0;
}
}
return 1;
}

```

This post has been edited by Adak: 26 January 2013 - 10:39 PM

### #12 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 10:58 PM

error C2668: 'sqrt' : ambiguous call to overloaded function
could be 'long double sqrt(long double)'
or 'float sqrt(float)'
or 'double sqrt(double)'

I keep getting this error with sqrt . And I need to be able to insert any 10 digits into this. Doesn't your program limit it to the ones listed? Or can those be substituted for variables?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 11:08 PM

Bawnawgwa, on 26 January 2013 - 10:58 PM, said:

error C2668: 'sqrt' : ambiguous call to overloaded function
could be 'long double sqrt(long double)'
or 'float sqrt(float)'
or 'double sqrt(double)'

I keep getting this error with sqrt />/>. And I need to be able to insert any 10 digits into this. Doesn't your program limit it to the ones listed? Or can those be substituted for variables?

It appears you are compiling in C++, instead of C, but using math.h, which is a C header file. The above runs fine (no errors or warnings), in C.

I can't help you in C++. For C compiling, you need to make sure your programs file extension is ".c", and not ".cpp".

If your C is older, change the line of code with sqrt() to this:
```int limit = (int)sqrt(number)+1;  //note the cast to int.

```

This is just for you to study. I'm not going to copy your mess of lines 12-50+. That is just code from hell. You should use an array, (like here), and if you want different values, then you enter different values, using a for loop.

Short and sweet.

This post has been edited by Adak: 26 January 2013 - 11:10 PM

### #14 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 11:25 PM

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
int a, b, c, d, e, f, g, h, i, j, s, x;
int smallest, largest, isPrime = 0, isPrime1 = 0, number;

printf("Enter 10 digits\n");
scanf("%d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);

(Mess of code)

if(isPrime)
printf("Smallest number: %d, is prime\n", smallest);
else
printf("smallest number: %d, is not prime\n", smallest);

if(isPrime1)
printf("Largest number: %d, is prime\n", largest);
else
printf("Largest number: %d, is not prime\n", largest);

getch();
return 0;
}

int x;
int limit = sqrt(number) + 1;
printf("limit: %d\n", limit);
for(x = 2; x < limit; x++)
{
if(number%x ==0){
return 0;
}
}

ok... Now it just tells me everything is not prime. Even though 13 is a prime number... Oh dear lol...

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do I get program to identify prime number?

Posted 26 January 2013 - 11:44 PM

[quote name='Bawnawgwa' date='26 January 2013 - 11:25 PM' timestamp='1359267947' post='1791220']

```#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
int a, b, c, d, e, f, g, h, i, j, s, x;
int smallest, largest, isPrime = 0, isPrime1 = 0, number;

printf("Enter 10 digits\n");
scanf("%d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);

(Mess of code)

if(isPrime)
printf("Smallest number: %d, is prime\n", smallest);
else
printf("smallest number: %d, is not prime\n", smallest);

if(isPrime1)
printf("Largest number: %d, is prime\n", largest);
else
printf("Largest number: %d, is not prime\n", largest);

getch();
return 0;
}

int x;
int limit = sqrt(number) + 1;
printf("limit: %d\n", limit);
for(x = 2; x < limit; x++)
{
if(number%x ==0){
return 0;
}
}

```

Quote

ok... Now it just tells me everything is not prime. Even though 13 is a prime number... Oh dear lol...

You need to add back in the last line of code.

return 1;

And the last closing brace of the isPrime function.

And for heaven's sake, put code tags around your program, or your code will be ignored!