| | |
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 |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





