Can anyone help me w/ this....help me make a program of pascal triangle

Dani AI

Generated

This thread is asking for a Pascal triangle program in C. mentioned converting a C++ example; and correctly asked for posted effort; pointed to an earlier example. Below is a concise explanation and a minimal, plain-C implementation that fits typical school assignments and is easy to convert from C++.

Two common ways to generate Pascal rows: build each row from the previous row (either with two arrays or an in-place single-array update), or compute each binomial coefficient directly using the multiplicative formula C(n,k) = C(n,k-1) * (n-k+1) / k. The in-place single-array method is simple, uses O(n) memory and O(n^2) time, and is shown here.

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

int main(void) {
    int n;
    if (scanf("%d", &n) != 1 || n <= 0) return 0;
    unsigned long long *a = calloc(n + 1, sizeof(unsigned long long));
    if (!a) return 1;
    a[0] = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = i; j > 0; --j) a[j] += a[j - 1];
        for (int j = 0; j <= i; ++j) printf("%llu ", a[j]);
        printf("\n");
    }
    free(a);
    return 0;
}

Notes and cautions: integer values grow quickly and will overflow 32/64-bit types for moderately large row counts; use a big-integer library (for example, GMP) when exact large coefficients are required. For background on the math, see Pascal's triangle. As stressed, posting any attempted code or target formatting (how many rows, spacing, command-line vs interactive) helps provide more specific fixes.

Recommended Answers

All 5 Replies

Search through this forum. You'll get number of such threads.

Can anyone help me w/ this....help me make a program of pascal triangle

This is properbly an school asignment, and if you read the rules and regulations:

We only give homework to those who show effort

So at least show some effort and explain the problem properly (must it be in command prompt, how many rows for you triangle), tell what you already have tried andpost your current code.

ok juz wait...im making a code for c...but still ive searched this site i saw only c++...and im trying 2 convert it into c...

ok juz wait...im making a code for c...but still ive searched this site i saw only c++...and im trying 2 convert it into c...

The point is that you need to post some code. If you find some C++ program then try to convert it to C. If you have problems then post it.

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.