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

What are lookup tables in c?

0 Answers  


When was c language developed?

0 Answers  


What is the difference between null pointer and void pointer

10 Answers   CTS, Manforce, MAQ Software,


Program to swap the any two elements in an array containing N number of elements?

1 Answers   Bosch, Glenwood, Ugam Solutions,


What is the difference between union and anonymous union?

0 Answers   Hexaware,






What is the difference between malloc() and calloc() function in c language?

0 Answers  


What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }

0 Answers   Google,


Write a function that accepts a sentence as a parameter, and returns the same with each of its words reversed. The returned sentence should have 1 blank space between each pair of words. Demonstrate the usage of this function from a main program. Example: Parameter: “jack and jill went up a hill” Return Value: “kcaj dna llij tnew pu a llih”

5 Answers   Mind Tree,


What is a program flowchart?

0 Answers  


What are the output(s) for the following ? #include char *f() {char *s=malloc(8); strcpy(s,"goodbye")} main() { char *f(); printf("%c",*f()='A'); }

0 Answers   Wilco,


Which control loop is recommended if you have to execute set of statements for fixed number of times?

0 Answers  


Write a program that takes a 5 digit number and calculates 2 power that number and prints it

7 Answers  


Categories