hi my name is srikanth.i have been trying to develop a source code to print the pascal's triangle for the past three days.i have'nt had any success.i would be greatly obliged if anyone could give me a simple source code to print the pascal's triangle in C.

Recommended Answers

All 14 Replies

If you've been working on it for 3 days, surely you have developed a starting point. Why don't you post your code, indicate specifically what's not working, and someone will help you.

hi i am a student of computer science.i am a beginner.i am looking for a simple source code in C to print the pascal's triangle.the user should have the option to enter the number of rows he requires to view.i would be obliged if anyone could help out here.

You didn't like the answer the first time so you post it again?

i am a student of computer science.i require an algorithm to print the pascal's triangle.

Merged threads: indianscorpion2, please don't start new threads with the same topic in a relatively short period of time (a couple days). If you haven't had any success, how about starting with a failed attempt?

Here's the mathematical way to do it. But if this is homework, your teacher is probably looking for the brute force way of doing it. I won't show you that one because half the fun is figuring things out for yourself. The other half is writing the code to do it. :)

#include <iostream>

double factorial(double n)
{
  return (n > 1) ? n * factorial(n - 1) : 1;
}

double ncr(int n, int r)
{
  return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
  std::cout.setf(std::ios::fixed, std::ios::floatfield);
  std::cout.precision(0);

  for (int i = 0; i < 15; i++) {
    for (int j = 0; j <= i; j++)
      std::cout << std::right << ncr(i, j) << ' ';
    std::cout << std::endl;
  }
}

hi i am a student of computer science.i am a beginner.i am looking for a simple source code in C to print the pascal's triangle.the user should have the option to enter the number of rows he requires to view.i would be obliged if anyone could help out here.

#include<stdio.h>
void main()
{
int ary[i][j],n;

printf("\nEnter the number of rows:");
scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  for(j=0;j<=i;j++)
  {
   if(j==0!!j==1)
   ary[i][j]=1;
   else
   ary[i][j]=ary[i-1][j-1]+ary[i-1][j];
  }
 }

 for(i=0;i<n;i++)
 {
  printf("\n"); 
  for(j=0;j<=i;j++)
  {
   printf("%d",ary[i][j]);
  }
 }
}

<< moderator edit: added [code][/code] tags >>

uzone24, one reason I haven't moderated your post is because the original question was posed several months ago. But we don't give answers to homework without effort on the student's part. The other reason is because your code both sucks and is horribly broken, so if someone bothered to turn it in, they would surely fail.

uzone24, one reason I haven't moderated your post is because the original question was posed several months ago. But we don't give answers to homework without effort on the student's part. The other reason is because your code both sucks and is horribly broken, so if someone bothered to turn it in, they would surely fail.

Narue can u elaborate on reasons y my code sucks as well wud fail ??

y wud it suck???????????

I'm sure it's perfectly fine to
- use i and j without having declared them first,
- use the mystical !! binary operator,
- fill values of 1 into the triangle where values of 1 do not belong,
- print the contents of your datastructure without putting spaces in between numbers,
- wonder why people think your code has problems without ever having tested it.

>Narue can u elaborate on reasons y my code sucks as well wud fail ??
You need an explanation? Clearly you don't take pride in your own work, or you would have at least tried to compile that code before posting it. But, I'll give you the benefit of the doubt. Let's make your code compilable and then presentable (note that I made no changes to your logic, though I assumed by !! you meant ||):

#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
  int i, j;
  int ary[10][10], n;

  printf ( "Enter the number of rows:" );
  fflush ( stdout );

  if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 )
    return EXIT_FAILURE;

  for ( i = 0; i < n; i++ ) {
    for ( j = 0; j <= i; j++ ) {
      if ( j == 0 || j == 1 )
        ary[i][j] = 1;
      else
        ary[i][j] = ary[i - 1][j - 1] + ary[i - 1][j];
    }
  }

  for ( i = 0; i < n; i++ ) {
    for ( j = 0; j <= i; j++ )
      printf ( "%-6d", ary[i][j] );
    printf ( "\n" );
  }

  return EXIT_SUCCESS;
}

And the output on my system is (drumroll please):

Enter the number of rows: [B]10[/B]
1
1    1
1    1    1244889
1    1    12448904730113
1    1    124489159750038203521
1    1    12448927219894141785248203648
1    1    1244893846478621398418223821728203648
1    1    124489497096792986320443780590305858208203648
1    1    124489510954573395728837364379474366410387894689588966
1    1    12448961219946850527456113216677148010204113155878483784349588968

That's certainly a different way of displaying Pascal's triangle. Compare that with a correct program:

#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
  int i, j;
  int n;

  printf ( "Enter the number of rows: " );
  fflush ( stdout );

  if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 )
    return EXIT_FAILURE;

  for ( i = 0;  i < n; i++ ) {
    int result = 1;

    for ( j = 0;  j < n; j++ ) {
      if ( j > 0 )
        result = result * ( i + j ) / j;

      printf ( "%-6d", result );
    }

    printf ( "\n" );
  }

  return EXIT_SUCCESS;
}

And you get a more recognizable table:

Enter the number of rows: [B]10[/B]
1     1     1     1     1     1     1     1     1     1
1     2     3     4     5     6     7     8     9     10
1     3     6     10    15    21    28    36    45    55
1     4     10    20    35    56    84    120   165   220
1     5     15    35    70    126   210   330   495   715
1     6     21    56    126   252   462   792   1287  2002
1     7     28    84    210   462   924   1716  3003  5005
1     8     36    120   330   792   1716  3432  6435  11440
1     9     45    165   495   1287  3003  6435  12870 24310
1     10    55    220   715   2002  5005  11440 24310 48620

>y wud it suck???????????
No, it's not that it "wud" suck, it's that it does suck. And as for why, because it's not compilable, and even if it were, it would still be wrong and broken. Is that so hard to understand?

As a side note, those dumbass abbreviations don't make you look any smarter. This isn't a chatroom, where slow typers need to abbreviate everything to get their message posted in time. Take your time, use correct spelling and grammer, and people will respect you more for it.

I'm sorry to disappoint everyone. But what exactly are you calculating, its certainly not Pascal's Triangle.

If you're in any doubt of what the first few rows of the triangle should look like see: http://en.wikipedia.org/wiki/Pascal_triangle

And some clean code I've found that shows how to calculate any given row in C, is at: <removed spam link>
I know the above posts were made ages ago, but I've been search for code and thought I may as well respond to the above and stop any other 'students' from failing :)

I'm sorry to disappoint everyone. But what exactly are you calculating, its certainly not Pascal's Triangle.

Yes it is, if you follow this link (the one you posted), you will see that it is exactly the same as the one in Narue's post, but tilted 45 degrees.

But I guess you won't be able to reply since on of the mod's is probably going to lock this thread :idea:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.