the program is showing the output perfectly but the value of a is printing more than once.
for example,
if i enter range from 10 to 20 , the output should be 11,13,17,19
but the output is 11,13,13,13,17,17,17,17,17,17,17,19,19,19,19,19,19,19,19,19
how can i fix this ?

int main(void)
{
int a,b,c,d,f;
clrscr ();
scanf ("%d",&a);
scanf ("%d",&b);
for (c=a;c<=b;c++)
	{
	for (d=2;d<b;d++)
		{
			{
			f=c%d;
			if (f==0)
			break;
			}
			if (f!=0)
			if (d>=a && d<=b)
			printf ("%d",c);
		}
	}
getche ();
}

Recommended Answers

All 11 Replies

are you trying to print all the odd numbers between two values? Your program is doing much too much work! It only needs one loop, not two. Delete that second loop and just test if the loop counter d is even or odd.

no, i am printing all the prime numbers between the input input numbers (a &b) therefore i have to use a second loop to take remainder of the numbers between a & b with all the numbers from b to 2 (b must be greater than a) to check if there is any 0 appears because if 0 appears then it won't be a prime number else it would be prime.

You need to stop the second loop after the first printf()

for (d=2;d<b;d++)
    {
        f=c%d;
        if ( f == 0)
            break;
        if (d>=a && d<=b)
        {
            printf ("%d ",c);
            break;
        }
    }
commented: thanks :) -> xufyan +0

hey thanks...the break is working can you please explain why we've applied break to print value once ?

this doesnt work. try to find primes from 2 to 30:

2
30
3 5 7 9 11 13 15 17 19 21 23 25 27 29

Process returned 0 (0x0)   execution time : 5.015 s
Press any key to continue.

this doesnt work. try to find primes from 2 to 30:

2
30
3 5 7 9 11 13 15 17 19 21 23 25 27 29

Process returned 0 (0x0)   execution time : 5.015 s
Press any key to continue.

oh..why it is not working now ? :(

it wasnt working to begin with. try to run your original code with the same range.

AD just fixed some of the blatant loop error to keep it from repeat printing. the problem is your choice of variables in which to base your loops, and the fact that you print the number as a prime on the first instance of it not being divisible by some number. just because 9 is not divisible by 2, doesnt mean that it's prime. obviously, it's divisible by 3. yet, as soon as it's found to not be divisible by 2, you print it as being prime.

your problem is also the way you name your variables. a, b, c, d, e, f... could you be any more cryptic? this is bad practice that lends itself to unintelligible code that is difficult to debug. and it will be full of bugs, because the programmer himself cant understand what is going on.

.

My program is dividing every number of loop1 with loop2, for example
if

start=10
end=20
loop1 would be 20 to 10
loop2 would be 20 to 2

the program will divide each value of loop1 with loop2 except the end value and if there is any zero the program will stop because 0 only appears if the number is non prime.
the program will print those numbers only where no 0 is appeared from end to 2 .
when i am assigning

start=10
end=20

the program is working fine but it is not working for some values, i did'nt know why is this happening so?
can you please explain the errors ?

here is my program...

int main(void)
{
int start,end,loop1,loop2,mod;
printf ("Enter first number:");
scanf ("%d",&start);
printf ("Enter second number:");
scanf ("%d",&end);
for (loop1=end;loop1>=start;loop1--)
	{
	for (loop2=2;loop2<end;loop2++)
		{
			      {
				mod=loop1%loop2;
				if (mod==0)
				break;
			      }
				if (mod!=0)

					if (loop2>=start && loop2<=end)
					{
					printf ("%d,",loop1);
					break;
					}
		}

	}

getche ();
}

last time i coded the program that was printing primes between 1 & 100 and instead of the same code as above it was working fine.
here it is.

int main(void)
{
	int a,b,c;
	{
		for (a=1;a<100;a++)
		{
			for (b=2;b<a;b++)
			{
				c=a%b;
				if (c==0)
				break;
			}
				if (c!=0)
					{
					printf ("%d,",a);
					}
		}
	}
getche ();
}

look i'm just going to give this to you, because it's obvious you're trying, and you really need to get some better coding practices.

study how i've modified your code, noting a few key points:

-- the second (inner) for loop should not be testing divisors across the entire range, but only up to a number less than the number being divided. in fact, this "maximum divisor" should not be more than half the number being tested, because it's meaningless to test divisors larger than half of the number being tested, and will just add unnecessary computations.

-- you need to test *each* of the appropriate divisors, and *none* of those divisors should be evenly divisible into the number in order for that number to be considered prime.

-- name your variables with meaningful names

-- comment your code heavily, explaining each fundamental block.

-- quit using <conio> functions like "clrscr" and "getche"

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int lower, upper, number, divisor, maxDivisor, remainder, isPrime;

   printf("\nEnter Lower Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&lower);

   printf("Enter Upper Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&upper);

   printf("\nPrimes Numbers in this Range are:\n\n");

   // cycle through each number within the range of values requested

   for (number = lower ; number <= upper; number++)
   {
      isPrime = 1;  // assume each is a prime unless found otherwise

      // for each number, test if it's divisible by any divisor between 2 and
      // a number up to the number/2.  there is no point in testing divisors
      // greater than the number/2 because it obviously won't be divisible

      maxDivisor = number/2;
      for (divisor = 2; divisor <= maxDivisor; divisor++)
      {
         remainder = number % divisor; // divide and get remainder
         if (remainder == 0)
         {                // if number is divisible with no remainder
            isPrime = 0;  // then the number is not prime and we can stop.
            break;
         }
      }
      // once we're done testing all appropriate divisors or have broken out early
      // check to see if the "isPrime" is still true.   If so, print the number as
      // it is prime.

      if (isPrime == 1)
         printf ("%d ",number);
   }

   printf("\n\nHit Enter to Quit.\n");
   getchar();

   return 0;
}
commented: ummaah... +0

great...! thank you very much...
now i've understand everything clearly except this isPrime what is this?
why have you assigned 1 for it in line 21? and how it is checking the prime condition ?

"isPrime" is just a flag that gets set or cleared to indicate whether or not the current number being tested is prime.

if isPrime = 0, then it is not prime.
if isPrime = 1, then it is prime.

here is the process by which it works:

the outer for loop for (number=lower;number<=upper;number++) tests each number within the range desired. before we start testing each number, we set isPrime = 1; which is to say that we will assume each number is prime until we later find otherwise.

so then we go into the second (inner) loop and test each of the appropriate divisors against the number. IF the number is found to be divisible by any one of the divisors, the number is "not prime" and so we clear isPrime = 0 and break; out of the inner loop -- there's no point in testing any more divisors, since the number is not prime.

once we're out of the second (inner) loop -- whether it's because we (a) found a divisor that evenly divided the number and broke out early, or (b) because we checked all the divisors and none of them evenly divided the number -- the determination of the number being prime or not will be reflected by the value of "isPrime".... if "isPrime" is 1, then the number is prime. if "isPrime" is 0, then the number is not prime.

we then index the outer loop to the next number, set "isPrime" back to 1, and repeat the process. and continue to repeat for each number being tested.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.