Algorithm
Step 1:Start.
Step 2:Declare variables num,flag,i,j.
Step 3:Input and read the limit,num.
Step 4:If num=1 then
                        4.1)Print there are no prime numbers.
Step 5:Else
                        5.1)Initialize i=2.
                        5.2)while(i<=num) repeat steps a to g.
                                    a)Initialize flag=0 and j=1.
                                    b)while(j<=i) repeat steps b.1 to b.3
                                                b.1)if(i%j==0) then increment flag by 1.
                                                b.2)End if.
                                                b.3)Increment j by 1.
                                    c)End while.
                                    d)If flag=2 then print the number,i.
                                    e)End if.
                                    g)Increment i by 1.
                        5.3)End while.
Step 6:End if.
Step 7:Stop.
Program
#include<stdio.h>
#include<conio.h>
void
main()
           
{
           
int num,flag,i,j;
           
clrscr();
           
printf("Enter the limit:\n");
           
scanf("%d",&num);
           
if(num==1)
                       
{
                       
printf(“No Prime numbers”);
                       
}
           
else
                       
{
           
           
printf("Prime Numbers are \n");
           
            for(i=2;i<=num;i++)
           
 
                     
{
           
      
                
flag=0;
           
      
                
for(j=1;j<=i;j++)
           
      
                            
{
                       
  
                    
if(i%j==0)
                       
  
                    
flag++;
           
      
                            
}
           
      
                
if(flag==2)
           
      
                
printf("%d\n",i);
                                   
}
                       
}
           
getch();
           
}
Output
Enter the limit:
20                                                                              
Prime Numbers are                                                                    
2                                                                               
3
5                                                                               
7                                                                               
11                                                                              
13                                                                              
17                                                                              
19
 

what is flag
ReplyDeletePrime numbers have only two factors, one and the number itself. The 'flag' represents the number of the factors.
Delete