Note.....

Dear readers I Have collected these Programs from various sources of internet...I don't know what the header file is Please mail me if any error found hi2rashid@yahoo.co.in

Sponsored....

Tuesday, April 13, 2010

C++ program for newton backward interpolation formula

//program for newton backward interpolation formula
#include
#include
#include
#define maxn 100
#define order 4
void main()
{
float ax[maxn+1],ay[maxn+1],diff[maxn+1][order+1],nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
clrscr();
cout<<"enter the value of n\n";
cin>>n;
cout<<"enter the values in form x,y\n";
for(i=1;i<=n;i++)
{cin>>ax[i]>>ay[i];}
cout<<"enter the value of x for which value of y is wanted\n";
cin>>x;
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{diff[i][1]=ay[i]-ay[i-1];}
for(j=2;j<=order;j++)
{
for(i=n;i>j;i--)
{diff[i][j]=diff[i][j-1]-diff[i-1][j-1];}
}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=order;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"when x = "<<<" y = "<
getch();
}

output:
enter the value of n
5
enter the values in form x,y
80 5026
85 5674
90 6362
95 7088
100 7854
enter the value of x for which value of y is wanted
105
when x = 105 y = 8666

C++ Program to solve linear equation (3 variable) by gauss elimination method

//program to solve linear equation (3 variable) by gauss elimination method
#include
#include
#include
#include
#include
#define r 3
#define c 4
void main()
{
clrscr();
float a[r][c];
int i,j;
textcolor(GREEN);
cprintf("PROGRAM TO SOLVE LINEAR EQUATIONS BY GAUSS ELIMINATION
METHOD");
cout<<"\n";
textcolor(BLUE);
cprintf("enter the values in given format");
cout<<"\n";
cprintf("a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]");
cout<<"\n";
for( i=0;i
{
for( j=0;j
{
cout<<"enter a["<<<"]["<<<"] : ";
cin>>a[i][j];
cout<<"\n";
}
}
delay(10);
step:
clrscr();
cout<<"the values entered are :\n";
for( i=0;i
{
for(j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp1=a[0][0];
for( j=0;j
{

a[0][j]=a[0][j]/temp1;
}
cout<<"the values after step 1 :\n";
cprintf("on dividing R1 by ");cout<<<"\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp2=a[1][0];
for( j=0;j
{a[1][j]=a[1][j]-temp2*a[0][j]; }
cout<<"the values after step 2 :\n";
cprintf("on R2 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp3=a[2][0];
for( j=0;j
{a[2][j]=a[2][j]-temp3*a[0][j];}
cout<<"the values after step 3 :\n";
textcolor(GREEN);
cprintf("on R3 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp4=a[2][1];
for(j=0;j
a[2][j]=a[2][j]-temp4/a[1][1]*a[1][j];
cout<<"the values after step 4 :\n";
cprintf("on R3 - ");cout<<<"*R2\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}
cout<<"Please wait while system calculates the result..............\n";
float z=a[r-1][c-1]/a[r-1][c-2];
float y=(a[r-2][c-1]-a[r-2][c-2]*z )/a[r-2][c-3];
float x=(a[r-3][c-1]-a[r-3][c-2]*z-a[r-3][c-3]*y)/a[r-3][c-4];
delay(10000);
clrscr();
textcolor(GREEN);
cprintf("SOLUTION OF GIVEN SYSTEM OF EQUATIONS");
cout<<"\n";
cout<<"x = "<
cout<<"\ny = "<
cout<<"\nz = "<
float ch;
cout<<"\n";
cprintf("Do you want to view steps again");cout<<"\n";
cprintf("press '1' else Press '2'");cout<<" ";cin>>ch;
if(ch==1)
goto step;
else
exit(0);
getch();
}

output:

enter the values in given format
a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]
enter a[0][0] : 2

enter a[0][1] : 1

enter a[0][2] : 1

enter a[0][3] : 10

enter a[1][0] : 3

enter a[1][1] : 2

enter a[1][2] : 3

enter a[1][3] : 18

enter a[2][0] : 1

enter a[2][1] : 4

enter a[2][2] : 9

enter a[2][3] : 16

2 1 1 10
3 2 3 18
1 4 9 16
the values after step 1 :
on dividing R1 by 1
1 0.5 0.5 5
3 2 3 18
1 4 9 16
the values after step 2 :
on R2 - 3*R1
1 0.5 0.5 5
0 0.5 1.5 3
1 4 9 16
the values after step 3 :
on R3 - 1*R1
1 0.5 0.5 5
0 0.5 1.5 3
0 3.5 8.5 11
the values after step 4 :
on R3 - 7*R2
1 0.5 0.5 5
0 0.5 1.5 3
0 0 -2 -10
Please wait while system calculates the result..............

SOLUTION OF GIVEN SYSTEM OF EQUATIONS
x = 7
y = -9
z = 5
Do you want to view steps again
press '1' else Press '2'

C++ PROGRAM FOR INTEGRATION USING NUMERICAL METHODS

//PROGRAM FOR INTEGRATION USING NUMERICAL METHODS

#include
#include
#define f(x) sqrt(cos(x))
#define inv 10
#include
void main()
{
clrscr();
int choice;
float l,j=0,u,h,arr[inv+1],sum1=0,sum2=0,sum3=0,res=0;
textcolor(GREEN);
cprintf("INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd
RULE");
cout<<"\n";
textcolor(BLUE);
cprintf("1.TRAPEZOIDAL RULE");
cout<<"\n";
cprintf("2.SIMPSON'S 1/3rd RULE");
cout<<"\nenter your choice :";
cin>>choice;

cout<<"enter the lower and upper limit respectively\n";
cin>>l>>u;
j=l;
h=(u-l)/inv;
for(int i=0;i<=inv;i++)
{
arr[i]=f(j);//cout<<"\n"<
j=j+h;
}

sum1=arr[0]+arr[inv];

switch(choice)
{
case 1:
cout<<"\n================================================================================\n";
for(i=1;i
{sum2+=arr[i];}
res=h/2*(sum1+2*sum2);
cout<<"\nthe integration is :"<
break;
case 2:
cout<<"\n================================================================================\n";
for(i=1;i
{ sum2+=arr[i];}
for(i=2;i
{sum3+=arr[i];}
res=h/3*(sum1+4*sum2+2*sum3);
cout<<"\nthe integration is :"<
break;
default:cprintf("WRONG CHOICE ");
}
cout<<"\n";
cprintf("PROGRAM DESIGNED BY:ROHIT KUMAR");
cout<<"\n";
textcolor(RED);
cprintf("BRANCH : ECE / 3rd semester / roll 249/06 ");

getch();
}

output:

INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd RULE
1.TRAPEZOIDAL RULE
2.SIMPSON'S 1/3rd RULE
enter your choice :1
enter the lower and upper limit respectively
0
1.57

================================================================================


the integration is :1.187191
PROGRAM DESIGNED BY:ROHIT KUMAR
BRANCH : ECE / 3rd semester / roll 249/06

INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd RULE
1.TRAPEZOIDAL RULE
2.SIMPSON'S 1/3rd RULE
enter your choice :2
enter the lower and upper limit respectively
0
1.57

================================================================================


the integration is :1.19437

C++ Program for newton forward interpolation formula

//Program for newton forward interpolation formula

#include
#include
#include
#include
#define MAXN 100
#define ORDER 4
void main()
{
float ax[MAXN],ay[MAXN],diff[MAXN+1][ORDER+1];
float nr=1.0,dr=1.0,x,p,h,yp;
int i,n,j,k;
textcolor(GREEN);
clrscr();
cout<<"enter the value of n : ";
cin>>n;
cout<<"\nenter the values in form x,y\n";
for(i=0;i<=n;i++)
cin>>ax[i]>>ay[i];
cout<<"\nenter the value of x for which y is wanted : ";
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
diff[i][1]=ay[i+1]-ay[i];
for(j=2;j<=ORDER;j++)
for(i=0;i<=n;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
while(!(ax[i]>x))
i++;
i--;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=ORDER;k++)
{
nr*=p-k+1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"\n when x = "<<<", y = "<<<<
delay(200);
cout<<"\a\a";
textcolor(YELLOW);
cout<<"\n\n\a";

cout<<"\n\n";


getch();
}

output:
enter the value of n : 4
enter the values in form x,y
80 5026
85 5674
90 6362
95 7088
100 7854
enter the value of x for which y is wanted : 83
when x = 83, y = 5.41e+03

C++ Program for probability- 12

//program no. 12 (probability)

#include
#include
#include
#include
void main()
{
textcolor(BROWN);
clrscr();
int w1,w2,b1,b2;
float p1,p2,p3;
cout<<"enter the number of white & black balls for urn 1 "; cin>>w1>>b1;
cout<<"enter the number of white & black balls for urn 2 "; cin>>w2>>b2;
cout<<"\ncase 1 : both ball drawn from urn1 are black "; p1=(nCr(b1,2)/nCr(b1+w1,2))*(nCr(w2,1)/nCr(w2+b2+2,1)); cout<<"\n\ncase 2: when both balls drawn from urn 1 are white"; p2=(nCr(w1,2)/nCr(w1+b1,2))*(nCr(w2+2,1)/nCr(w2+b2+2,1)); cout<<"\n\ncase 3 : when one ball is white & other is black"; p3=((nCr(w1,1)*nCr(b1,1))/nCr(w1+b1,2))*(nCr(w2+1,1)/nCr(w2+b2+2,1)); cout<<"\nprobability in case 1 = "< 2 = "< cout<<" 3 = "< cout< textcolor(RED); cprintf(" p3 =" %f" 1 =" 0.011538" 2 =" 0.288462" 3 =" 0.153846" p3 =" 0.453846">
main ( )
{
int a = 5;
printf("value of a = %d\n",a);
printf("address of a = %u\n",&a);
}

C++ Program for probability-11

//program no. 11 (probability)
#include
#include
#include
#include
#define max 5
void main()
{
textcolor(LIGHTGREEN);
clrscr();
float urn[max],w[max],b[max],p[max],sum=0;
int num,nw;
cout<<"enter the number of urns : ";
cin>>num;
cout<<"enter the number of white balls to be drawn :";
cin>>nw;
for(int i=0;i
{
cout<<"for urn "<<<" enter its probability : ";
cin>>urn[i];
cout<<"\n enter the number of white & black balls resp. : ";
cin>>w[i]>>b[i];
p[i]=nCr(w[i],nw)/nCr(w[i]+b[i],nw);
sum+=p[i]*urn[i];
}
cout<<"\nthe required probability is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR ");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of urns : 3
enter the number of white balls to be drawn :2
for urn 1 enter its probability : 0.2

enter the number of white & black balls resp. : 7 3
for urn 2 enter its probability : 0.6

enter the number of white & black balls resp. : 4 6
for urn 3 enter its probability : 0.2

enter the number of white & black balls resp. : 2 8

the required probability is 0.177778

C++ Program for probability- 10

//program no. 10 (probability)

#include
#include
#include
#define max 5
void main()
{
float num,a[max],s[max],p[max],sum=0;
textcolor(LIGHTGREEN); int m;
clrscr();
cout<<"enter the number of plants = ";
cin>>num;
cout<<"enter the plant which scooter is likely to be found ";
cin>>m;
for(int i=1;i<=num;i++)
{
cout<<"enter the % of sccoters manufactured at plant "<<<" = ";
cin>>a[i];
cout<<"enter the % of scooters of standard quality manufactured at
plant "<<<" = ";
cin>>s[i];
sum+=a[i]*s[i];
}
p[m]=a[m]*s[m]/sum;
cout<<"\nthe chance that it comes from plant "<<<" = "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of plants = 2
enter the plant which scooter is likely to be found 2
enter the % of sccoters manufactured at plant 1 = 0.7
enter the % of scooters of standard quality manufactured at plant 1 =
0.8
enter the % of sccoters manufactured at plant 2 = 0.3
enter the % of scooters of standard quality manufactured at plant 2 =
0.9

the chance that it comes from plant 2 = 0.325301

C++ Program for probability-9

//program no.9 (probability)

#include
#include
#include
void main()
{
textcolor(LIGHTBLUE);
clrscr();
int num;
float s,c,t,sa,ca,ta,sum=0,p;
cout<<"enter the number of scooter drivers = ";
cin>>s;
cout<<"the chances of scooter accident = ";
cin>>sa;
cout<<"enter the number of car drivers = ";
cin>>c;
cout<<"the chances of car accident = ";
cin>>ca;
cout<<"enter the number of truck drivers = ";
cin>>t;
cout<<"the chances of truck accident = ";
cin>>ta;
sum=s*sa+c*ca+t*ta;
p=s*sa/sum;
cout<<"\n\nthe probability that scooterist has met an accident = "<
delay(800);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of scooter drivers = 2000
the chances of scooter accident = 0.01
enter the number of car drivers = 4000
the chances of car accident = 0.03
enter the number of truck drivers = 6000
the chances of truck accident = 0.15


the probability that scooterist has met an accident = 0.019231

C++ Program for probability- 8

//program no.8 (probability)
#include
#include
#include
#include
#include
void main()
{
textcolor(LIGHTBLUE);
clrscr();
int d,t,p1,p2,p3,p4,n;
cout<<"enter the number of total bulbs = ";
cin>>t;
cout<<"enter the number of defected bulbs = ";
cin>>d;
cout<<"the number of bulbs drawn = ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\n\nCASE "<<<" : WHEN "<<<" DEFECTED BULB AND "<<<" GOOD
BULBS ";
cout<<"\nthe probability = "<
}
delay(5000);
textcolor(GREEN);
clrscr();
cout<<"\nthe probability distribution table ";
cout<<"\n\n";
cout<<"number\t";
cout<<"probability\n";
for(i=1;i<=n;i++)
{cout<
cout<<
cout<<"\n";
}
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");

getch();
}

output:
enter the number of total bulbs = 20
enter the number of defected bulbs = 5
the number of bulbs drawn = 4


CASE 1 : WHEN 1 DEFECTED BULB AND 3 GOOD BULBS
the probability = 0.462332

CASE 2 : WHEN 2 DEFECTED BULB AND 2 GOOD BULBS
the probability = 0.693498

CASE 3 : WHEN 3 DEFECTED BULB AND 1 GOOD BULBS
the probability = 0.462332

CASE 4 : WHEN 4 DEFECTED BULB AND 0 GOOD BULBS
the probability = 0.115583

the probability distribution table

number probability
1 0.462332
2 0.693498
3 0.462332
4 0.115583

C++ Program no. probability-7

program no. 7 (probability)
#include
#include
#include
#define max 5
void main()
{
textcolor(LIGHTBLUE);
clrscr();
int num;
float b[max],d[max],pdb[max],sum=0;
cout<<"enter the number of machines ";
cin>>num;
for(int i=0;i
{
cout<<"enter the percentage of bolts made by machine "<<<" = ";
cin>>b[i];
cout<<"enter the percentage of defective bolts made by machine
"<<<" = ";
cin>>d[i];
pdb[i]=b[i]*d[i];
sum+=pdb[i];
}
for(i=0;i
{
float p1;
p1=b[i]*d[i]/sum;
cout<<"\nthe probability that defective bolt was made by machine
"<<<" = "<
}
delay(800);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of machines 3
enter the percentage of bolts made by machine 1 = 0.25
enter the percentage of defective bolts made by machine 1 = 0.05
enter the percentage of bolts made by machine 2 = 0.35
enter the percentage of defective bolts made by machine 2 = 0.03
enter the percentage of bolts made by machine 3 = .40
enter the percentage of defective bolts made by machine 3 = 0.02

the probability that defective bolt was made by machine 1 = 0.403226
the probability that defective bolt was made by machine 2 = 0.33871
the probability that defective bolt was made by machine 3 = 0.258065

C++ Program for probability- 6

//program no.6 (probability)

#include
#include
#include
void main()
{
textcolor(GREEN);
clrscr();char n1[10],n2[10];
cout<<"enter the name of person throwing dice first & then second\n";
cin>>n1>>n2;
float p1,p2;
p1=1/(6*(1-25/36.0f));
p2=5/(36*(1-25/36.0f));
cout<<"\nthe probability of winning of "<<<" is "<
cout<<"\nthe probability of winning of "<<<" is "<
cout<<"\nthe respective chances of winning is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the name of person throwing dice first & then second
a
b

the probability of winning of a is 0.545455
the probability of winning of b is 0.454545
the respective chances of winning is 1.2

C++ Program no. probability-5

//Program no. 5 (probability)

#include
#include
#include
#include
void main()
{
textcolor(GREEN);
clrscr();
int a,b,p1,b1,p2,b2; double s1,s2;
cout<<"enter the num. of share of A in the lottery : ";
cin>>a;
cout<<"enter the num. of prizes and blanks :";
cin>>p1>>b1;
cout<<"enter the num. of share of B in the lottery : ";
cin>>b;
cout<<"enter the num. of prizes and blanks :";
cin>>p2>>b2;
s1=((nCr(p1,a))+((nCr(p1,1))*(nCr(b1,1))))/nCr((p1+b1),a);
cout<<"\n\n\athe probability of success of A is : "<
s2=(nCr(p2,3)+nCr(p2,2)*nCr(b2,1)+nCr(p2,1)*nCr(b2,2))/nCr((p2+b2),b);
cout<<"\n\n\athe probability of success of B is : "<
cout<<"\n\n\athe ratio of success of A:B is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");

getch();
}

output:
enter the num. of share of A in the lottery : 2
enter the num. of prizes and blanks :3 5
enter the num. of share of B in the lottery : 3
enter the num. of prizes and blanks :4 6

the probability of success of A is : 0.642857

the probability of success of B is : 0.833333

the ratio of success of A:B is 0.771429

C++ Program for probability- 4

//Program no. 4 (probability)

#include
#include
#include
void main()
{
textcolor(GREEN);
clrscr();
float a1,a2,b1,b2,c1,c2,p1,p2;
cout<<"enter the success ratio of A in the form (x,y) : "; cin>>a1>>a2;
cout<<"enter the success ratio of B in the form (x,y) : "; cin>>b1>>b2;
cout<<"enter the success ratio of C in the form (x,y) : "; cin>>c1>>c2;
cout<<"\n\awhen any two hit the shot "; p1=((a1/a2)*(b1/b2)*((c2-c1)/(c2)))+(((a2-a1)/a2)*(b1/b2)*(c1/(c2)))+((a1/a2)*((b2-b1)/b2)*(c1/c2)); cout<<"\n\nthe probability in this case is "< p2="((a1/a2)*(b1/b2)*(c1/c2));">
delay(100);
textcolor(RED);
cout<<"\n\n\a"; cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR"); cout<<"\n\n"; cprintf("/ roll 249/06 "); getch(); } output:
enter the success ratio of A in the form (x,y) : 4 5
enter the success ratio of B in the form (x,y) : 3 4
enter the success ratio of C in the form (x,y) : 2 3

when any two hit the shot
the probability in this case is 0.433333

when all three review the book
the probability in this case is 0.4

the required probability is 0.833333

C++ Program no. probability-3

Program no. 3 (probability)

#include
#include
#include
void main()
{
textcolor(GREEN);
clrscr();
float a1,a2,b1,b2,c1,c2;float p1,p2;
cout<<"enter the odds for 1st critic in the form (x y) : ";
cin>>a1>>a2;
cout<<"enter the odds for 2nd critic in the form (x y) : ";
cin>>b1>>b2;
cout<<"enter the odds for 3rd critic in the form (x y) : ";
cin>>c1>>c2;
cout<<"\ncase 1: when all three review the book ";
p1=((a1/(a1+a2))*(b1/(b1+b2))*(c1/(c1+c2)));
cout<<"\n\aa\nthe probability in this case is "<
cout<<"\n\n\acase 2: when any two review the book ";
p2=((a1/(a1+a2))*(b1/(b1+b2))*(c2/(c1+c2)))+((a2/(a1+a2))*(b1/(b1+b2))*(c1/(c1+c2)))+((a1/(a1+a2))*(b2/(b1+b2))*(c1/(c1+c2)));
cout<<"\nthe probability in this case is "<
cout<<"\n\a\nbut required probability is sum of two probabilities that
is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the odds for 1st critic in the form (x y) : 5 2
enter the odds for 2nd critic in the form (x y) : 4 3
enter the odds for 3rd critic in the form (x y) : 3 4

case 1: when all three review the book
the probability in this case is 0.174927

case 2: when any two review the book
the probability in this case is 0.434402

but required probability is sum of two probabilities that is 0.609329

C++ Program for probability- 2

//program no. 2 (probability)

#include
#include
#include
#include
void main()
{
int tc,ace,dc;
textcolor(BROWN);
clrscr();
cout<<"\aenter total no. of cards : ";
cin>>tc;
cout<<"\aenter the no. of aces : ";
cin>>ace;
cout<<"\aenter the no. of aces drawn : ";
cin>>dc;
float prob=nCr(ace,dc)/nCr(tc,dc);
cout<<"the probability of drawing "<<<" aces in a pack of "<<<"
is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter total no. of cards : 52
enter the no. of aces : 4
enter the no. of aces drawn : 2
the probability of drawing 2 aces in a pack of 52 is 0.004525

Program for (probability 1)

//Program for (probability 1)

#include
#include
#include
float nCr(int n,int r);
float fact(int n) ;
void main()
{
textcolor(YELLOW);
clrscr();
int r,b,w,t,r1,b1,w1,t1;float prob,p1,p2,p3,sam_s;
cout<<"enter the number of red balls : ";
cin>>r;
cout<<"enter the number of blue balls : ";
cin>>b;
cout<<"enter the number of white balls : ";
cin>>w;
t=r+b+w;
cout<<"enter the number of red ball drawn : ";
cin>>r1;
cout<<"enter the number of blue ball drawn : ";
cin>>b1;
cout<<"enter the number of white ball drawn : ";
cin>>w1;
t1=r1+b1+w1;
sam_s=nCr(t,t1);
p1=nCr(r,r1);p2=nCr(b,b1);p3=nCr(w,w1);
prob=(p1*p2*p3)/sam_s;
cout<<"\nthe probability of drawing "<<<" red "<<<" blue "<<<"
white balls is : "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:PRASHANT KUMAR SINGH");
cout<<"\n\n";
cprintf("BRANCH : ECE / 3rd semester / roll 194/06 ");

getch();
}

float fact(int n)
{float fn=1;
if(n==0)
return(1);
else
{
for(int i=1;i<=n;i++)
fn*=i;
return(fn);
}
}

float nCr(int n,int r)
{
float C=fact(n)/(fact(r)*fact(n-r));
return(C);
}

output:

enter the number of red balls : 3
enter the number of blue balls : 7
enter the number of white balls : 6
enter the number of red ball drawn : 0
enter the number of blue ball drawn : 1
enter the number of white ball drawn : 1

the probability of drawing 0 red 1 blue 1 white balls is : 0.35