Write a c program to print Pascal triangle.






1. Pascal triangle in c without using array

2. C code to print Pascal triangle

3. Simple c program for Pascal triangle

4. C program to generate Pascal triangle

5. Pascal triangle program in c language

6. C program to print Pascal triangle using for loop


#include<stdio.h>

long fact(int);
int main(){
    int line,i,j;

    printf("Enter the no. of lines: ");
    scanf("%d",&line);

    for(i=0;i<line;i++){
         for(j=0;j<line-i-1;j++)
             printf(" ");

         for(j=0;j<=i;j++)
             printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
         printf("\n");
    }
    return 0;
}

long fact(int num){
    long f=1;
    int i=1;
    while(i<=num){
         f=f*i;
         i++;
  }
  return f;
 }

Sample output:

Enter the no. of lines: 8
       1
      1 1
     1 2 1
    1 3 3 1
   1 4 6 4 1
  1 5 10 10 5 1
 1 6 15 20 15 6 1
1 7 21 35 35 21 7 1





10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
15. Write a c program to solve quadratic equation.
18. Write a c program which passes structure to function.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.

43 comments:

  1. good site
    thanks to Administrator

    ReplyDelete
  2. #include
    int main()
    {
    int i,j,k,n,m;
    printf("Enter the range ");
    scanf("%d",&n);
    m = n-1;
    for(i=1;i<=n;i++)
    {
    k = i;
    j = m;
    while(j)
    {
    printf(" ");
    j--;
    }
    while(k)
    {
    printf("*");
    printf(" ");
    k--;
    }
    printf("\n");
    m--;
    }
    }

    ReplyDelete
  3. dude your code is right but this a code for pyramid

    ReplyDelete
  4. please write a program to display all the perfect and prime numbers within a range.
    (I tried them, but after entering the range, the screen holds showing nothing.. on further clicking the program execution exits)

    ReplyDelete
  5. how to say a number is perfect or not?

    ReplyDelete
  6. rose_preethi10/16/11, 12:06 AM

    The 6 is a perfect number , because we can divide a 6 by 1,2,3 and if we add 1+2+3 we will get an answer as 6... When the sum of divisior of a particular number is equal to the number then we say that is a perfect number..
    Another Example is 28 which is divided by 1,2,4,7,14 and when we add the number we will get as 28, 1+ 2 + 4 + 7 + 14 = 28

    ReplyDelete
  7. Hi Pavani,
    Which compiler are you using?

    ReplyDelete
  8. preema&amp; reema12/12/11, 1:57 PM

    good site,thank u.......

    ReplyDelete
  9. incomplete,,,,,,,,,,,,, jst luk 1st for loop of i

    ReplyDelete
  10. simple way with out using function....

    #include
    #include

    void main()
    {
    int i,j,n,c,k,space;
    clrscr();
    printf("Enter the limit ");
    scanf("%d",&n);
    printf("\n\n");
    space=n;
    for(i=0;i<=n;i++)
    {
    c=1;
    for(k=space;k>=0;k--)
    printf(" ");

    space--;
    for(j=0;j<=i;j++)
    {
    printf("%d ",c);
    c=(c*(i-j)/(j+1));
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  11. why hav u used long datatype for fact() function... why d things don't work up with int as a return type???
    Please reply me at psvalent05@yahoo.com

    ReplyDelete
  12. function get more confusion....

    ReplyDelete
  13. function get more confusion....

    ReplyDelete
  14. lots of confusion.....

    ReplyDelete
  15. can u please give me solution for the following pattern
    1
    2 2
    3 3 3
    4 4 4 4

    ReplyDelete
    Replies
    1. #include
      void main()
      {
      int x,y;
      for(x=1;x<=4;x++)
      {
      for(y=1;y<=x;y++)
      printf("%2d",x);
      printf("\n");
      }

      }

      Delete
    2. #include
      int main ()
      {
      int i,j,a=1;
      for(i=1;i<=4;i++){
      for(j=1;j<=i;j++){
      printf ("%d ",a);
      }
      a++;
      printf ("\n");
      }
      getchar();
      return 0;
      }

      Delete
  16. send code for fallowing o/p::
    1
    2 3
    4 5 6
    7 8 9 10

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,n,c;
      printf("Enter the limit ");
      scanf("%d",&n);
      int s=n;
      for(r=0;r<n;r++)
      {
      for(c=1;c<=s;c++)
      {
      printf("%d ",c);
      }
      s--;
      printf("\n");
      }
      return;
      }
      by Muthusamy.s from GCT

      Delete
    2. #include

      int main()
      {
      int n, i, c, a = 1;

      printf("Enter the number of rows of Floyd's triangle to print\n");
      scanf("%d", &n);

      for (i = 1; i <= n; i++)
      {
      for (c = 1; c <= i; c++)
      {
      printf("%d ",a);
      a++;
      }
      printf("\n");
      }

      return 0;
      }

      Delete
  17. can u please give me solution for the following pattern...
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

    ReplyDelete
    Replies
    1. thank u

      Delete
    2. #include
      int main()
      {
      int r,n,c;
      printf("Enter the limit ");
      scanf("%d",&n);
      int s=n;
      for(r=0;r<n;r++)
      {
      for(c=1;c<=s;c++)
      {
      printf("%d ",c);
      }
      s--;
      printf("\n");
      }
      return;
      }
      by Muthusamy.s from GCT

      Delete
  18. can u pls give me solution for fibonacci series in use for loop...

    ReplyDelete
  19. can u pls provide ans for dis
    5987
    987
    87
    7

    ReplyDelete
  20. provide the answer this question

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 6 15 20 15 6 1

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i,j,n,c;
      printf("Enter the limit ");
      scanf("%d",&n);
      printf("The pascal triangle is print in floyd's triangle\n\n");
      for(i=0;i<n;i++)
      {
      c=1;
      for(j=0;j<=i;j++)
      {
      printf("%d ",c);
      c=(c*(i-j)/(j+1));
      }
      printf("\n");
      }
      return;
      }

      by Muthusamy.S GCT

      Delete
    2. Thanx a lot darling...!!!!!!!!

      Delete
  21. stack operations are used for storing elements in a stack. The input method for stack is last in first out. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials for Beginners

    ReplyDelete
  22. #include
    #include
    #include
    void main()
    {
    int a[20],n,n1,num,sum=0,r,i,j;
    clrscr();
    printf("enter the power of 11");
    scanf("%d",&n);
    for(i=0;i0)
    {
    r=num%10;
    //sum=sum+r;

    printf(" %d ",r);
    num=num/10;
    }
    printf("\n");


    }


    getch();
    }

    ReplyDelete
  23. can someone tell me whats wrong with this code for generating pascal triangle .iwish to print the pascal triangle line by line without having to induct spaces among elements


    int fact1(int j)
    {
    int k=1,i;
    for(i=j;i>=0;i-- ){
    k=i*k;
    }
    return k;
    }

    int main()
    {
    int res,line,k;
    for(line=0;line<7;line+=1){
    for(k=0;k<line;k++)
    {
    res=fact1(line)/fact1(line-k)*fact1(k);
    printf("%d\t",res);
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  24. ****** *******
    ***** *****
    *** ***
    ** **
    * *

    provide answer for this































    ReplyDelete
  25. nice but plz give the many pascal question that we butter understand about the pascal.....

    ReplyDelete
  26. I have this one works 100% i coded this my self although it's little foolish
    #include
    main()
    {
    int i, j, b[50],a[50],k,n,count;
    printf("enter the limit \n") ;
    scanf("%d", & n) ;

    for(i=0;i0;k--)
    printf(" ");
    printf("1 \n\n");
    }
    if(j==1)
    {
    for(k=2*(n-i) ;k>0;k--)
    printf(" ");
    printf("1 ");
    }
    }
    count=0;
    k=0;
    for(j=1;j<i;j++)
    {
    a[1]=1;
    a[i]=1;
    b[k]=a[j]+a[j+1];
    printf(" %d ", b[k]) ;
    k++;
    count++;
    }
    for(j=2,k=0;k<=i-1;j++, k++)
    a[j]=b[k] ;
    for(j=1;j<=i;j++)
    {
    if(j==i)
    {
    printf(" 1");
    printf("\n\n") ;
    }
    }
    }
    return 0 ;
    }

    ReplyDelete
  27. please add header file #include < stdio. h>

    ReplyDelete
  28. #include
    #include

    int main()
    {
    int a[10][10];
    int no;
    printf("Enter the number of rows for pascal triangle : ");
    scanf("%d",&no); //taking input
    for(int i=1; i<=no; i++)
    {
    for(int j=1; j<=i; j++)
    {
    if(j==1||j==i)
    {
    a[i][j]=1;
    }
    else // taking arrays value
    {
    a[i][j]=a[i-1][i-j]+a[i-1][i-j+1];
    }
    }
    }
    for(int i=1; i<=no; i++)
    {
    for(int k=1; k<=no-i; k++)
    {
    printf(" ");
    }
    for(int j=1; j<=i; j++)
    {
    printf("%d ",a[i][j]); //printing pascal triangle
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  29. simple easy approach

    #include
    void main()
    {
    int i,j,x,n,s;
    printf("Enter The Number : ");
    scanf("%d",&n);
    for(i=0;i<=n;i++)
    {
    x=1;
    for(s=1;s<=n-i;s++)
    printf("\t");
    for(j=1;j<=i+1;j++)
    {
    printf("%d\t\t",x);
    x=x*(i-j+1)/j;
    }
    printf("\n");
    }
    }

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete
  31. #include
    main()
    {
    int i,j,n,k,temp,c;

    printf("Enter the number of lines\n");
    scanf("%d",&n);
    printf("\n\n");
    temp=n;
    for(i=0;i<=n;i++,printf("\n"))
    {
    c=1;
    for(k=temp;k>=0;k--)
    printf(" ");
    temp--;
    for(j=0;j<=i;j++)
    {
    printf("%d ",c);
    c=(c*(i-j)/(j+1));
    }
    }

    }

    ReplyDelete