Last active
September 23, 2021 21:48
-
-
Save vaaino/aae14bc70d15514d32b16aae724608e0 to your computer and use it in GitHub Desktop.
pascals triangle in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*-------------------------------------- | |
* Program Name: pascal | |
* Author: vaino | |
* Description: pascals triangle | |
*-------------------------------------- | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
int fac(int n) | |
{ | |
if (n <= 1) | |
return 1; | |
return fac(n - 1) * n; | |
} | |
int choose(int n, int r) | |
{ | |
return fac(n)/(fac(n - r) * fac(r)); | |
} | |
int main(void) | |
{ | |
int levels; | |
scanf("%d", &levels); | |
for (int i = 0; i < levels; i++) | |
{ | |
char temp[50] = ""; | |
for (int j = 0; j < (i + 1); j++) | |
{ | |
char t[5]; | |
sprintf(t, "%d ", choose(i, j)); | |
strcat(temp, t); | |
} | |
printf("%s\x0A", temp); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*-------------------------------------- | |
* Program Name: pascal | |
* Author: vaino | |
* Description: pascals triangle | |
*-------------------------------------- | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <tice.h> | |
#include <keypadc.h> | |
#define INPUT_SIZE 10 | |
int fac(int n) | |
{ | |
if (n <= 1) | |
return 1; | |
return fac(n - 1) * n; | |
} | |
int choose(int n, int r) | |
{ | |
return fac(n)/(fac(n - r) * fac(r)); | |
} | |
int main(void) | |
{ | |
char iter_c[INPUT_SIZE]; | |
os_ClrHome(); | |
os_GetStringInput("iter? ", iter_c, INPUT_SIZE); | |
int iter = atoi(iter_c); | |
for (int i = 0; i < iter; i++) | |
{ | |
char temp[100] = ""; | |
for (int j = 0; j < (i + 1); j++) | |
{ | |
char t[100]; | |
sprintf(t, "%d ", choose(i, j)); | |
strcat(temp, t); | |
} | |
printf("%s\n", temp); | |
} | |
while (!os_GetCSC()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment