write a program that prints a pascal triangle based on the
user input(like how many stages) in an efficient time and
optimized code?

Answers were Sorted based on User's Feedback



write a program that prints a pascal triangle based on the user input(like how many stages) in an e..

Answer / vamsi krishna

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("enter the highest power");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<(n-i);j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%d ",c(i,j));
printf("\n");
}
getch();
}
c(int i,int j)
{
int k,N=1,D=1;
if(i==0)
return(1);
if(j==0)
return(1);
for(k=0;k<j;k++)
{
N=N*(i-k);
D=D*(j-k);
}
return(N/D);
}

Is This Answer Correct ?    7 Yes 2 No

write a program that prints a pascal triangle based on the user input(like how many stages) in an e..

Answer / vamsi krishna

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("enter the highest power");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<(n-i);j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%d ",c(i,j));
printf("\n");
}
getch();
}
c(int i,int j)
{
int k,N=1,D=1;
if(i==0 || j==0)
return(1);
for(k=0;k<j;k++)
{
N=N*(i-k);
D=D*(j-k);
}
return(N/D);
}

Is This Answer Correct ?    4 Yes 2 No

write a program that prints a pascal triangle based on the user input(like how many stages) in an e..

Answer / abhinav

call this recursive routine:

void pascal(int n)
{
if(n==1) {
printf("%d \n", n);
return;
}
pascal(n-1);
int i;
for(i=1;i<=n; i++)
printf("%d ",i);
for(i=n-1;i>=1; i--)
printf("%d ",i);
printf("\n");
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

logic for generating all the combinations of the any number of given letters. ex::::::::: if a,b,c,d are given the o/p should be abcd,dcba,dbac,bcad,................ 4*3*2*1 combinations............

2 Answers   Infosys,


What is signed and unsigned?

0 Answers  


Explain Doubly Linked Lists?

3 Answers  


What are different types of pointers?

0 Answers  


WHAT IS MEANT BY LIFE?

2 Answers  


Why is void main used?

0 Answers  


write a program to compare 2 numbers without using logical operators?

5 Answers   IBM,


Is c procedural or object oriented?

0 Answers  


What is f'n in math?

0 Answers  


What is a nested loop?

0 Answers  


can we define a function in structure?

2 Answers  


void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }

0 Answers  


Categories