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

Describe wild pointers in c?

0 Answers  


6)What would be the output? main() { int u=1,v=3; pf("%d%d",u,v); funct1(&u,&v); pf("%d%d\n",u,v); } void funct1(int *pu, int *pv) { *pu=0; *pv=0; return; } a)1 3 1 3 b)1 3 1 1 c)1 3 0 0 d)1 1 1 1 e) 3 1 3 1

4 Answers  


Tell me what is null pointer in c?

0 Answers  


Can you write the function prototype, definition and mention the other requirements.

0 Answers   Adobe,


What is typedef example?

0 Answers  






Is register a keyword in c?

0 Answers  


WAP TO ACCEPT STRING AND COUNT A COMES N TIMES B COMES N TIMES C COMES N TIMES D COMES N TIMES AND SO ON......... AT LAST UNTIL Z COMES N TIMES...............

3 Answers  


write an interactive C program that will encode or decode a line of text.To encode a line of text,proceed as follows. 1.convert each character,including blank spaces,to its ASCII equivalent. 2.Generate a positive random integer.add this integer to the ASCII equivalent of each character.The same random integer will be used for the entire line of text. 3.Suppose that N1 represents the lowest permissible value in the ASCII code,and N2 represents the highest permissible value.If the number obtained in step 2 above(i.e.,the original ASCII equivalent plus the random integer)exceeds N2,then subtract the largest possible multiple of N2 from this number,and add the remainder to N1.Hence the encoded number will always fall between N1 and N2,and will therefore always represent some ASCII character. 4.Dislay the characters that correspond to the encoded ASCII values.  The procedure is reversed when decoding a line of text.Be certain,however,that the same random number is used in decodingas was used in encoding.

0 Answers  


What is output of the following program ? main() { i = 1; printf("%d %d %d\n",i,i++,i++); }

9 Answers   CTS, Wipro,


main() { int i; for(i=0;i<5;i++) printf("%d",1l<<i); } why doesn't 'l' affect the code??????

1 Answers  


Write a programm such that if user enter 11.25 it roundup to 11 but if user enter 11.51 upto 11.99 it will round up to 12 i.e.;convert the floting point value into integer format as explain above..

2 Answers  


#define MAX 3 main() { printf("MAX = %d \n",MAX ); #undef MAX #ifdef MAX printf("Vector Instituteā€); #endif

4 Answers   IBM, Vector,


Categories