I have a project about making pascal triangle using recursive function.
This is the example output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Hint:(x+y) n=>exponent.
*Give me an idea about this project. Thanks!
I have a project about making pascal triangle using recursive function.
This is the example output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Hint:(x+y) n=>exponent.
*Give me an idea about this project. Thanks!
You need to show us some efford before we can help you xD
Do you know how to create a pascal triangle without recursive?
If so then doing it recursive won't be that far off. Give it a go and see
what happens.
/* Program to print the Pascal's triangle recursively */
#include<stdio.h>
int pascal(int,int);
void space(int,int);
main()
{
int num,i,j;
printf("\nEnter the no. of rows required: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
space(num-i,3);
for(j=1;j<=i;j++)
{
printf("%3d",pascal(i,j));
space(1,3);
}
printf("\n");
}
}
int pascal(int row,int column)
{
if(column==0)
return 0;
else if(row==1&&column==1)
return 1;
else if(column>row)
return 0;
else
return (pascal(row-1,column-1)+pascal(row-1,column));
}
void space(int num,int mul)
{
int i;
num*=mul;
for(i=0;i<num;i++)
printf(" ");
}
This code will do the job
#include <iostream>
long calc(int n, int r) {
if ((n == 0) || (r == 0) || (n == r)) return 1;
else return (calc(n-1,r-1) + calc(n-1,r));
} // This is the recursive function
int main() {
printf("Enter a number = ");
int num;
scanf("%i", &num);
for (int i = 0; i<=num; i++) {
for (int j = num; j > i; j--) printf(" ");
for (int k = 0; k <= i; k++) printf("%6d", calc(i,k));
printf("\n");
} // End of for loop
return 0;
}
@shellexecutor: Don't bump 2 year old threads (the OP probably doesn't care anymore), and don't provide fully working code, especially for such a classic homework problem (pascal triangle) and when the OP didn't show much effort of his own.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.