Hi there,
I wrote a program that should find all the Prime Numbers in an interval specified by the user.
Here is the program that works:
insert
Code:
#include<stdio.h>
int main()
{
int isPrime;
int high;
int low;
int i;
printf("Enter the lower number: \n");
scanf(" %d", &low);
printf("Enter the higher number: \n");
scanf(" %d", &high);
for(low ; low < high ; ++low){
isPrime=1;
if (low <= 1) {
++low;
continue;
}
for(i=2 ; i <= low /2 ; ++i ){
if(low % i==0)
isPrime=0;
}
if(isPrime==1)
printf(" %d ", low);
}
return 0;
}
Here's the thing. I'm trying to add a Break following the second For in order to save redundecy but everytime I do, it stops working properly and I can't seem to figure why... :/
Here the same prog just with the Break:
insert
Code:
#include<stdio.h>
int main()
{
int isPrime;
int high;
int low;
int i;
printf("Enter the lower number: \n");
scanf(" %d", &low);
printf("Enter the higher number: \n");
scanf(" %d", &high);
for(low ; low < high ; ++low){
isPrime=1;
if (low <= 1) {
++low;
continue;
}
for(i=2 ; i <= low /2 ; ++i ){
if(low % i==0)
isPrime=0;
break;
}
if(isPrime==1)
printf(" %d ", low);
}
return 0;
}