Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, November 20, 2013

Program to print the first 10 lines of Pascal's Triangle


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
long triangle(int x,int y);
int main()
{
clrscr();
const lines=10;
for (int i=0;i<lines;i++)
for (int j=1;j<lines-i;j++)
cout << setw(2) << " ";
for (int j=0;j<=i;j++)
cout << setw(4) << triangle(i,j);
cout << endl;
getch();
}
long triangle(int x,int y)
{
if(x<0||y<0||y>x)
return 0;
long c=1;
for (int i=1;i<=y;i++,x--)
c=c*x/i;
return c;
}

To know about Pascal's Triangle Click Here

Thursday, July 25, 2013

Program to find n!/(r!(n-r)!)

#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n,r,f,f1,f2,ft;
cout<<"Enter the value of n and r : ";
cin>>n>>r;
f=fact(n);
f1=fact(r);
f2=fact(n-r);
ft=f/(f1*f2);
cout<<"result : "<<ft;
getch();
}
int fact(int n)
{
int x;
if(n==1)
x=1;
else
x=n*fact(n-1);
return(x);
}

To know about function click here

Wednesday, July 24, 2013

Program to find the factorial of a number using function

#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n,f;
cout<<"Enter a number : ";
cin>>n;
f=fact(n);
cout<<"Factorial of the given number is : "<<f;
getch();
}
int fact(int n)
{
int x;
if(n==1)
x=1;
else
x=n*fact(n-1);
return(x);
}