Fact

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include "mpi.

h"
#include<stdio.h>
int main(int argc,char *argv[])
{
int rank,size,n,a,b,root,f,l,res,cnt,r;
root=0;r=1;res=1;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank==root)
{
printf("\n\nEnter the factorial value: ");
scanf("%d",&n);
}
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==1)
{
f=(n*rank)/size;
l=(n*(rank+1))/size;
for(cnt=f+1;cnt<=l;cnt++)
{
res=res*cnt;
}
}
if(rank==2)
{
f=(n*rank)/size;
l=(n*(rank+1))/size;
for(cnt=f+1;cnt<=l;cnt++)
{
res=res*cnt;
}
}
if(rank==3)
{
f=(n*rank)/size;
l=(n*(rank+1))/size;
for(cnt=f+1;cnt<=l;cnt++)
{
res=res*cnt;
}
}
MPI_Reduce(&res,&r,1,MPI_INT,MPI_PROD,root,MPI_COMM_WORLD);
if(rank==0)

{
printf("\nfactorial of %d is : %d\n\n\n",n,r);
}
MPI_Finalize();
return 0;
}

You might also like