i have tried to do the pascals law in the following method .....bt i m not getting a triangle shape.could someone help me out in this tight Spot?? here is the code what i have typed...

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
long TRIANGLE(int x,int y);
int main()
{
clrscr();
int lines=0,n;
cout<<"enter how many lines";
cin>>n;
for(lines=0;lines<=n;lines++)
{
for(int i=0;i<lines;i++)
for(int j=0;j<lines-n;j++)
cout<<setw(2)<<" ";
for(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;
}

Recommended Answers

All 2 Replies

First, remember to post code surrounded by code tags to retain indentation to make the code easier to read.

Assuming you want the peak of the triangle centered over the base then I would would calculate the numbers and put them into a container printing the container once all calcualtions were done and not print the numbers to the screen on the fly. I'd consider using nested loops to control the display of the triangle using leading spaces followed by visible values followed by trailing spaces (though the trailing spaces are more conceptual than real).
How many spaces will be needed will depend on how many lines are displayed, how many characters will be needed in the display of the last line, and how many digits there are in the largest number contained within the triangle.

Hey raniajay, you'd be happy that C++ has provided standard libraries without the .h extension since long. You can simply write

#include <iostream>
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.