Program 5: Print the prime numbers upto a given range

/* Program 5: Print the prime numbers upto a given range */

 

#include<stdio.h>

int main()

{

        int i,j,flag,n;

        do

        {

                 gotoxy(10,10);

                printf(“Enter the upper range number (<=1000) : “);

                scanf(“%d”,&n);

                if (n<1 || n>1000)

                {

                        gotoxy(10,25);

                        printf(“Out of data range …. please try again”);

                 }

        }while(n<1 || n>1000);

        gotoxy(10,5);

        printf(“The prime numbers upto %d are as follows….\n\n”,n);

        for (i=2;i<=n;i++)

        {

            flag=0;

                for (j=2;j<=i/2;j++)

                {

                        if (i%j==0)

                        {

                                flag=1;

                                break;

                        }

                }

                if (flag==0)

                        printf(”  %d\t”,i);

        }

        return 0;

}