Reputation: 57
I came across a program to print all the prime numbers between 1 to n, where 'n' is the value supplied by the user. It used the for loop. The program is as follows-
#include <stdio.h>
int main()
{
int i, j, end, isPrime; // isPrime is used as flag variable
/* Input upper limit to print prime */
printf("Find prime numbers between 1 to : ");
scanf("%d", &end);
printf("All prime numbers between 1 to %d are:\n", end);
/* Find all Prime numbers between 1 to end */
for(i=2; i<=end; i++)
{
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
isPrime = 0;
}
if(isPrime==1)
{
printf("%4d", i);
}
}
return 0;
}
I very well understand the above code. But just to test my knowledge , I tried writing the same program using the do loop. It didn't work out very well. I searched through books and internet to find a program where they use a do loop to compute prime numbers till n. But couldn't find one. I wanted to know if its even possible or not. To show my efforts, I put below the code I made-
#include <stdio.h>
main()
{
int i, j, n, isPrime;
printf("Enter n\n");
scanf ("%d", &n);
i = 2;
do
{
do
{
j = 2;
if (i%j == 0)
isPrime = 0;
else
{
isPrime = 1;
printf ("%d", i);
}
j = j+1;
}
while (j <= i/2);
i = i+1;
}
while (i <= n);
}
Upvotes: 1
Views: 334
Reputation: 309
You had a good start, but made a few small errors which resulted in big problems
j=2
was set in the second do-while
loop. This resulted in j being set to 2 every loop, thus never exiting the do while loop.
Next was the if/else. In the for loop example, the if (isPrime==1)
was outside the for loop. You were checking inside the while loop resulting in things being named prime way to soon.
Below is an example of the working code
#include <stdio.h>
main()
{
int i, j, n, isPrime;
printf("Enter n\n");
scanf ("%d", &n);
printf("All prime numbers between 2 to %d are:\n", n);
i = 2;
do
{
isPrime = 1;
j = 2;
do
{
if (i%j == 0)
isPrime = 0;
j = j+1;
} while (j < i/2);
if (isPrime == 1 || i == 2)
printf ("%d\n", i);
i = i+1;
}
while (i <= n);
}
Upvotes: 1
Reputation: 929
Try this code instead:
#include <stdio.h>
main(){
int i, j, n, isPrime;
printf("Enter n\n");
scanf ("%d", &n);
i = 2;
do
{
isPrime = 1;
j = 2;
do
{
if (i%j == 0 && j <= i/2)
isPrime = 0;
j = j+1;
}
while (j <= i/2);
if(isPrime == 1) printf ("%d\n", i);
i = i+1;
}
while (i <= n);
}
There is difficulty in translating a for
loop to a do-while
loop.
For example, with problems you had in your code:
do
loop, you were setting j
to 2, over and over. This meant that you were stuck in an infinite loop, since at the start of the loop, the value of j
would be reset.isPrime
should be set as 1
at the start of the first do-while loop. This is so that, if i
is, at any point divisible by j
, the flag is set to 0, meaning i
is not prime. The problem with the way you set it is that if the last value of j
does NOT divide into i
, then isPrime
is set to 1, even if previous values of j
divided into it.Upvotes: 0
Reputation: 72271
A for loop
for (INIT; CONDITION; NEXT) {
BODY;
}
is equivalent to the while loop
{
INIT;
while (CONDITION) {
BODY;
NEXT;
}
}
(except that the for loop does not allow declaring the same name in the INIT
and in the outermost block of the BODY
).
You made a couple of mistakes in your conversion:
do { BODY; } while (CONDITION);
syntax always executes the BODY
at least once. The original for
loop might not.This matters for the case i==2
: In the original code, the inner for
loop will set j=2
, then immediately test whether j<=i/2
, which is 2<=1
or false, so the loop body won't execute at all. But your code starts by setting i=2; j=2;
then going right to the factor test if (i%j == 0)
. This is true, so it incorrectly concludes 2
is not a prime.
isPrime = 1;
and printf
statements from the outer loop over i
to be inside the inner loop over j
. This changes the program's behavior in a bad way.Consider what happens when i==15
: We start with j=2
, and find that i%j != 0
since 2
is not a factor of 15
. So it sets isPrime = 1;
and prints 15
. The inner loop continues, setting j=3
. This time i%j == 0
since 3
is a factor of 15
, so it sets isPrime = 0;
and prints nothing. The inner loop goes again with j=4
. Now it's back to i%j != 0
, so it sets isPrime = 1;
and prints 15
again. This continues up to j=7
, printing 15
a total of four times (when j
is 2, 4, 6, and 7).
I'll leave it to you to see if you can now correct the code using while
loops.
Upvotes: 0
Reputation: 437
here is the code that you need.
#include <stdio.h>
main() {
int i, j, n, isPrime;
printf("Enter n\n");
scanf("%d", &n);
i = 2;
do {
j = 2;
isPrime = 1;
if (i == 2)
printf("%d ", i);
do {
if (i % j == 0) {
isPrime = 0;
break;
}
j = j + 1;
} while (j <= i / 2);
if (isPrime)
printf("%d ", i);
i = i + 1;
} while (i <= n);
}
Upvotes: 0