-4

The program (c code) gets a minimum and maximum value from the user to then determine whether all of the numbers in between are prime or not. If the number is not prime, the program must also tell the user one set of factors of that number. I'm having trouble determining how to work the for loops and then determine what the factors are. My programming knowledge is not very good, so the simple terms would be helpful. Any help is greatly appreciated.

    int min, max, i, d, is_prime, not_prime; 
    void flag(int *is_prime, int *not_prime);
    int main() {
       int min, max, i, d, is_prime, not_prime;
      printf("The program will calculate all prime numbers in the range n"); 
      printf("Please enter a value for the start value\n");
     scanf("%d", &min);
     printf("Please enter a value for the end value\n");
     scanf("%d", &max); 
   for(i=min; i<=max; i++) {
     for(d=2; d<i<10; d++) {
        if (d % i ==0) { (flag==1); } }
pm100
  • 48,078
  • 23
  • 82
  • 145
Curtis Negele
  • 15
  • 2
  • 5
  • 1
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions. Please be aware that we do not provide _from-stratch_ coding service here. :-) – Sourav Ghosh Jan 30 '15 at 21:22
  • 1
    homework? Really? you want us to code for you? – 23ars Jan 30 '15 at 21:23
  • This question appears to be off-topic because it is implying _"Please write my code for me."_ – Sourav Ghosh Jan 30 '15 at 21:23
  • you will get answers here if you show that you have something done but are stuck. If you dont have an idea how to start ask your teacher – pm100 Jan 30 '15 at 21:24
  • i am very new to coding: int min, max, i, d, is_prime, not_prime; void flag(int *is_prime, int *not_prime); int main() { int min, max, i, d, is_prime, not_prime; printf("The program will calculate all prime numbers in the range specified\n"); printf("Please enter a value for the start value\n"); scanf("%d", &min); printf("Please enter a value for the end value\n"); scanf("%d", &max); for(i=min; i<=max; i++) { for(d=2; d – Curtis Negele Jan 30 '15 at 21:26
  • edit that code into the question - we cant read it posted as a comment – pm100 Jan 30 '15 at 21:49
  • You don't want `flag` as both a function and a variable. You need to report on the factor you just found. Your code only works for numbers up to 100 or so. There are other, more efficient ways of finding the primality of the numbers in the range. Use 'Sieve of Eratosthenes' as a search term (on SO or on Google or other search engine of choice). – Jonathan Leffler Jan 30 '15 at 23:26

1 Answers1

2

break the problem into smaller pieces.

  • Write code that asks for two numbers and simply prints all the numbers between them

  • write a function to determine if a number is prime (returns boolean)

  • write a function that returns a factor of a number

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pm100
  • 48,078
  • 23
  • 82
  • 145
  • so would it be best to not have a for loop within a for loop to generate the numbers in between and then determine if those numbers are prime? 'for(i=min; i<=max; i++) { printf("%d \n",i); for(d=2; d – Curtis Negele Jan 30 '15 at 21:42
  • Each number in the range needs to be sent to a call by reference function also. And sorry I haven't figured out how to make the code format nicely into the comments yet. – Curtis Negele Jan 30 '15 at 21:44
  • @CurtisNegele: there isn't a way to format code in comments nicely. You can embed it inside ``backquotes (`)`` (so `for (int i = min; i <= max; i++)`) which is as good as you get. Line breaks and multiple spaces will both be ignored in comments. It's best to edit the code into the question, formatted carefully for readability. Layout the code as you want it to look, then select it and use the **`{}`** button above the edit box to indent it as code. – Jonathan Leffler Jan 30 '15 at 23:18