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
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 |
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 |
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 |
How do I determine whether a character is numeric, alphabetic, and so on?
What is the size of array float a(10)?
How to swap 3 numbers without using 4th variable?
Write one statement equalent to the following two statements x=sqr(a); return(x); Choose from one of the alternatives a.return(sqr(a)); b.printf("sqr(a)"); c.return(a*a*a); d.printf("%d",sqr(a));
write a program in c to find out the sum of digits of a number.but here is a condition that compiler sums the value from left to right....not right to left..
What is the purpose of the preprocessor directive error?
Can a pointer be null?
plz let me know how to become a telecom protocol tester. thank you.
write a program without using main function?
What does void main return?
What does do in c?
write an algorithm and c program to add two 2x2 matrics