Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to find the total no of prime numbers between 1 to n by using thread

Posted By: Easy Tutor     Category: C Programming     Views: 4615

Write a program to find the total no of prime numbers between 1 to n by using thread.

Code for Program to find the total no of prime numbers between 1 to n by using thread in C Programming

# include <stdio.h>
# include <pthread.h>


void * thread_sum(void *);
int TotalCount=0;
int iNumber;
pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;

int main()
{
    int iCount,jCount;
    int threadPara;
    int PrimeFlag=1;
    pthread_t tid;
    printf("Enter Number Up to Which You want to Count Prime Numbers :");
    scanf("%d",&iNumber);
    threadPara=iNumber/2 + 1;
    pthread_create(&tid,NULL,thread_sum,(void *)&threadPara);
    for(iCount=2;iCount<=iNumber/2;iCount=iCount++)
    {
        for(jCount=2;jCount<iCount;jCount++)
        {
            if(iCount%jCount==0)
            {
                PrimeFlag=0;
                break;
            }
        }
        
        if(PrimeFlag==1)
        {    
            printf("[ %d ]  Main\n",iCount);
            pthread_mutex_lock(&mVar);
            TotalCount=TotalCount++;
            pthread_mutex_unlock(&mVar);
        }
        else
        {
            PrimeFlag=1;
        }
    }
    
    pthread_join(tid,NULL);
    
    printf("Final Count is : %d \n",TotalCount);
    return 0;
}

void *thread_sum(void *no)
{
    int *ifrom,iCount,jCount;
    int PrimeFlag=1;
    ifrom=(int*)no;
    
    for(iCount=*ifrom;iCount<=iNumber;iCount=iCount++)
    {
        for(jCount=2;jCount<iCount;jCount++)
        {
            if(iCount%jCount==0)
            {
                PrimeFlag=0;
                break;
            }
        }
        
        if(PrimeFlag==1)
        {    
            printf("[ %d ] Thread\n",iCount);
            pthread_mutex_lock(&mVar);
            TotalCount++;
            pthread_mutex_unlock(&mVar);
        }        
        PrimeFlag=1;        
    }
    pthread_exit(NULL);    
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to find the total no of prime numbers between 1 to n by using thread is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!