| | |
Need Help With Printing Pascal's Triangle In C
![]() |
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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. 

C Syntax (Toggle Plain Text)
#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; } }
•
•
Join Date: Jul 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by indianscorpion2
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.
C Syntax (Toggle Plain Text)
#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]); } } }
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.
I'm here to prove you wrong.
•
•
Join Date: Jul 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: frequency problem,can u plz do the documentation for mi
- Next Thread: Frequency of characters entered.
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





