Logo 
Search:

C Programming Articles

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

Program to find factorial of a input number

Posted By: Yasmin Silva     Category: C Programming     Views: 10505

Write a Program to find factorial of a input number.

Code for Program to find factorial of a input number in C Programming

#include<stdio.h>
#include "headfork.h"// included in attachment
#include "headshr.h"
#include "headsem.h"

main()
{
    int shmid,shmid1,shmid2;
    int *ans,*lock1,mul;
    int id,n,npc,j,i;
        
    ans=shared(sizeof(int)*2,&shmid);

    lock1=shared(sizeof(int)*2,&shmid1);

//    ans=shared(sizeof(int)*2,&shmid2);

    lock_init(lock1);

    printf("Enter ant No: ");
    scanf("%d",&n);

    printf("Enter the no.of proc: ");
    scanf("%d",&npc);

    *ans=1;

    id=p_fork(npc);
    mul=1;
    
    for(i=id+1;i<=n;i+=npc)
    {
        mul=mul*i;
    }
    
    locksem(lock1);
    * ans = *ans * mul;
    unlock(lock1);

    p_join(npc,id);

    printf("The Factorial of %d is: %d",n,*ans);

}
  
Share: 


Didn't find what you were looking for? Find more on Program to find factorial of a input number Or get search suggestion and latest updates.

Yasmin    Silva
Yasmin Silva author of Program to find factorial of a input number is from Salvador, Brazil.
 
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!