This prgm is about finding prime nos in a certain range...and i made it..the problem which i faced was
when the user is entered an alphabet wrong instead of an integer..i have to ignore that entry and i have to continue the execution at the same place...
Eg;suppose if "ending value" goes wrong the execution has to continue again from ending value...
For alpha check i used the statement
"if(scanf("%ld", &n))" but this is executing only once..next time if this statement is encountered it is not asking the value from user, instead it itself assigning the previous value and resulting in infinite loop......
How to overcome this problem ???
#include<stdio.h>
#include<ctype.h>
long int x,y;
int h=0;
int main()
{
long int a[150000],i,j;
int c,g,k=1,t=0;
// clrscr();
while(t<1)
{
printf("\n enter the starting limit:");
t=check();
}
t=0;
while(t<1)
{
printf("\n enter the ending limit:");
t=check();
}
for(i=x;i<=y;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
/*if(c==3)
j=j+i;*/
}
if(c==2)
{
a[k]=i;
printf("\n prime no %d is:%ld",k,a[k]);
k++;
}
}
t=0;
printf("\n Enter the n th prime no within the range:");
scanf("%d",&g);
if(g<=k)
printf("the %d th prime no within the range:%ld",g,a[g]);
else
printf("\n the entered no is out of range!");
printf("\n");
// getch();
return 0;
}
int check()
{ long int n;
if(scanf("%ld", &n))
{
if(n==1)
{
printf("\n invalid value");
return 0;
}
else if(h==0)
{
x=n;
h++;
}
else
{
y=n;
}
return 1;
}
else
{
printf("\n invalid value");
return 0;
}
}

New Topic/Question
Reply



MultiQuote




|