Sample – Program to print the first 10 lines of pascal’s triangle.

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
long triangle(int x,int y);
int main()
{
clrscr();
const lines=10;
for (int i=0;i<lines;i++)
for (int j=1;j<lines-i;j++)
cout << setw(2) << ” “;
for (int j=0;j<=i;j++)
cout << setw(4) << triangle(i,j);
cout << endl;
getch();
}
long triangle(int x,int y)
{
if(x<0||y<0||y>x)
return 0;
long c=1;
for (int i=1;i<=y;i++,x–)
c=c*x/i;
return c;
}

This program does not take in any screen inputs from the user.
It just prints out the first ten lines of the pascal’s triangle using the ‘cout’ command.

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – count number of characters and lines in a file
  2. Sample – Program to enter two integers and print the quotient and remainder.
  3. Sample – Program to enter an integer and print out its successor.
  4. Sample – Program to enter an integer and print its total value based on the formula ‘x – 1/3!x3 + 1/5!x5 – 1/7!x7 + 1/9!x9′.
  5. Sample – Program to convert 2-digit octal number into binary number and print it.

Leave a Comment

Previous post:

Next post: