# Thread: Please help me mod this project (prime numbers between n and m, user entered)

1. ## Please help me mod this project (prime numbers between n and m, user entered)

Hi,
I have been writing some code for my intro computer class. The purpose of this code is to determine and print all the prime numbers between two user entered numbers for the range.

So here's my code so far:
insert
Code:
```#include <math.h>
#include <stdio.h>

int main(void){

int i, j, isPrime, numberone, numbertwo;

printf("Enter the lower of two positive, whole numbers: ");
scanf(" %d", &numberone);

printf("\n");

printf("Enter the higher of two positive, whole numbers: ");
scanf(" %d", &numbertwo);

if (numberone >=numbertwo)
{
printf("Number one needs to be smaller than number two, try again.\n");
return 1;
}
for(i = 2 ; i <= (numbertwo - numberone) ; i++){
isPrime = 1;
for(j = 2 ; j <= i ; j++) {
if( i == j)
continue;
else if( i % j == 0)
isPrime = 0;
}

if(isPrime)
printf("%d, ", i);
}

printf("\n");

return 0;

}```
My question is: How do I modify this so that the user can enter the numbers regardless of whether the first number is greater or lesser than the second?

I want it to print the numbers regardless of whether or not the first number is higher than the second, or vice versa.

Thank you for your help

2. So ask for two numbers (a and b, say), then let numberone be the smaller of the two and numbertwo be the larger.

3. so it'd be like...

scanf("%d", &a)

then later
scanf("%d", &b)

if (a< b)
a=lowernumber

else if (b<a)

b=lowernumber

? something like that?

4. Assignments work right-to-left, not left-to-right.

5. Code:
```for(j = 2 ; j <= i ; j++)
{
if( i == j)
continue;
else if( i % j == 0)
isPrime = 0;
}```
1. if you do not want to process j == i - just replace the loop condition with j < i
Code:
```for(j = 2 ; j < i ; j++)
{
if( i % j == 0)
isPrime = 0;
}```
2. if you found some devider - no need to continue looping
Code:
```for(j = 2 ; j < i ; j++)
{
if( i % j == 0)
{
isPrime = 0;
break;
}
}```
3. you do not need to go as high as i - sqrt(i) is enough
Code:
```for(j = 2 ; j*j <= i ; j++)
{
if( i % j == 0)
{
isPrime = 0;
break;
}
}```

6. ## thanks

thank you for the help I will make some changes and repost if there's anything else.

7. Here is what my non computer programming mind came up with over the course of 15 minutes.
By non computer programming mind, I mean that the extend of my formal computer programming training consisted of 5 weeks of FORTRAN -).

Code:
```#include <stdio.h>
#include <stdlib.h>

void prime(int m, int n) {
int i;
for(i = m; i < n; i++) {
if (i == 1) {
} else if (i == 2 || i == 3)
(void)printf("%d ", i);
else if ((i % 2) == 0 || (i % 3) == 0) {
} else {
(void)printf("%d ", i);
}
}
(void)printf("\n");
}

int main(void)
{
int m, n;
int temp;

(void)printf("Enter 2 positive integers: ");
fflush(stdout);
if ((scanf("%d %d", &m, &n)) == 2) {
if (m <= 0 || n <= 0) {
(void)fprintf(stderr, "Invalid numer range\n");
exit(1);
}
if(m > n) {
temp = m;
m = n;
n = temp;
}
prime(m, n);
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}

return 0;
}```
And here is the output
[cd@localhost oakland]\$ gcc -Wall -Wextra prime.c -o prime
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 1 13
2 3 5 7 11
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 4 34
5 7 11 13 17 19 23 25 29 31
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 22 5
5 7 11 13 17 19
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 65 1
2 3 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61
[cd@localhost oakland]\$

8. Love your handle, Overworked_PhD, but your program does not find primes. It finds numbers which ain't divisible by 2 or 3. But you've captured the idea that when the limit numbers are in the wrong order they need to be switched.

9. Yeah, apparently I forgot what a prime number is.

10. That's okay: it gives LightYear more stuff to practice.

11. ## Final product

Well here's what I was going to turn in, thanks for all the help btw. If there are any suggestions in regards to standards or some style points I might miss, please go ahead and say what it is.

Code:
```// This program asks the user for two positive, whole numbers and then prints a
list of prime numbers between the user entered range of numbers.

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

int main(void)
{

int i, j, A, B, isPrime, lowernumber, highernumber;

printf("Enter the first of two positive, whole numbers: ");
scanf(" %d", &A);

printf("\n");

printf("Enter the second of two positive, whole numbers: ");
scanf(" %d", &B);

if (A<B)
highernumber=B, lowernumber=A;
else if (B<A)
highernumber=A, lowernumber=B;

while (0>A || 0>B)    // If user enters negative number, input is invalid.
{
printf("One of your numbers is negative, invalid input.\n");
return 1;
}

for(i = 2 ; i <= (highernumber - lowernumber) ; i++) //high-low=range
{

isPrime = 1;
for(j = 2 ; j <= i ; j++)
{

if( i == j)
continue;
else if( i % j == 0)
isPrime = 0;

}

if(isPrime)                  //If number is prime, prints the prime number
printf("%d, ", i);

}
printf("\n");

return 0;

}```

works perfect btw

12. And here is my second attempted solution.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define MAX 1000

void printme(int s[], int min, int max)
{
int i;

for (i = min; i < max; i++) {
if (s[i] == 0)
(void)printf("%d ", i);
}
(void)printf("\n");
}

void prime(int low, int high)
{
int n;
int x;
static int arr[MAX];

if(high > MAX) {
(void)fprintf(stderr, "Number is too high\n");
exit(1);
}

if (low == 1)
low = low + 1;

for (n = low; n < high; n++) {
for (x = low; x < n; x++) {
if ((n % x) == 0) {
++arr[n];
break;
}
}
}

printme(arr, low, high);
}

int main(void)
{
int m, n;
int temp;

(void)printf("Enter 2 positive integers: ");
fflush(stdout);
if ((scanf("%d %d", &m, &n)) == 2) {
if (m <= 0 || n <= 0) {
(void)fprintf(stderr, "Invalid numer range\n");
exit(1);
}
if(m > n) {
temp = m;
m = n;
n = temp;
}
prime(m, n);
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}

exit(0);
}```
And the output...

[cd@localhost oakland]\$ gcc -Wall -Wextra prime.c -o prime
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 1 1000
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
[cd@localhost oakland]\$ ./prime
Enter 2 positive integers: 1000 1
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
[cd@localhost oakland]\$

Popular pages Recent additions