c program to find and count the prime number upto given number ?

>> Friday, October 28, 2011


This program find the Prime number upto the number you desire and will also count the number of prime numbers. This program uses goto Statement which is very infrequently used but sometime desired at some places and so as here. So in this program will we also learn to use goto statement. Below is the program with its output, with prime number upto 100.
Have a look on it :


#include<stdio.h>
#include<conio.h>

int main()
{
     int n=2, count=0;                  //count variable store the number of prime number
     int i, m;
     printf("enter the number upto which you want to find prime numbers : ");      
     scanf("%d",&m);
     printf("prime numbers are : ");
     while(n<m)
     {
             for(i=2; i<n; i++)
                      {
                      if(n%i==0)
                      goto t;                             // goto is referring to line or code 't'
                      }
             printf("\t%d ", n);
             count++;
             t : n++;                                    //when goto is executed it will directly jump to this statement
     }
     printf("\n\nNumber of prime numbers are %d",count);
     getch();
}
       
   

Here is the output :

c program to find the prime number upto a given number and count total number of prime numbers


0 comments:

Post a Comment

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save