Write a basic half pyramid pattern program - c

Have tried few basic pattern
trying to get pattern
1
2 4
3 6 12
4 8 16 32
SO far trying to find the proper sequence, my idea is that need another variable lets say num, and need to create a sequence for num to print num eventually
#include <stdio.h>
int main()
{
int rows = 0 , i, j , num,num2;
do{
printf("please enter the number of rows: ");
scanf("%d",&rows);
}while(rows <=2 );
printf("printing a half pyramid of %d rows", rows);
printf("\n");
for( i = 1; i <=rows; ++i) {
for (j = 1; j <= i; ++j ) {
printf("%d ", );
}
printf("\n");
}
return 0;
}
Not being able to figure out a sequence

The code you were given literally contains all the parts necessary. All that remains for you is to fill out this line inside the nested loop:
printf("%d ", ‹what goes here?›);
To find the answer you need to find how the value relates to the current row and column (give by i and j, respectively).
You don’t need an additional variable num (to be clear, you can create one, but it’s not necessary to solve this problem).

We, beginners, should help each other.:)
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned int tmp = n * n;
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, value * j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 1
1
Enter the height of a pyramid (0 - exit): 2
1
2 4
Enter the height of a pyramid (0 - exit): 3
1
2 4
3 6 9
Enter the height of a pyramid (0 - exit): 4
1
2 4
3 6 9
4 8 12 16
Enter the height of a pyramid (0 - exit): 5
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Enter the height of a pyramid (0 - exit): 6
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
Enter the height of a pyramid (0 - exit): 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
Enter the height of a pyramid (0 - exit): 8
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
Enter the height of a pyramid (0 - exit): 9
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
Enter the height of a pyramid (0 - exit): 0
The loops in the program can also look like
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value += i;
}
putchar( '\n' );
}
or without introducing the intermediate variable value like
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, j * ( i + 1 ) );
}
putchar( '\n' );
}
You yourself can add a check to the program that n * n is not greater than UINT_MAX.
Edit: As you changed the displayed values in the pattern then the program can look for example the following way
#include <stdio.h>
#include <math.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned long long int tmp = n * ( long long unsigned )pow( 2, ( n - 1 ) );
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value *= 2;
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 10
1
2 4
3 6 12
4 8 16 32
5 10 20 40 80
6 12 24 48 96 192
7 14 28 56 112 224 448
8 16 32 64 128 256 512 1024
9 18 36 72 144 288 576 1152 2304
10 20 40 80 160 320 640 1280 2560 5120
Enter the height of a pyramid (0 - exit): 0

Tricky Pattern. Here, is a logic for that pattern with implementation.
'n' is the number of rows.
#include <stdio.h>
int main(void) {
int n = 4;
for(int i=1; i<=n; i++) {
int k=i;
printf("%d%s",k," ");
for(int j=1; j<i; j++) {
k = k*2;
printf("%d%s",k," ");
}
printf("\n");
}
return 0;
}

Related

Is it possible to create a user input number pyramid whereby the pyramid only display from 0 - 9?

I tried editing the loop for counter so that the last line of printf is non negative but I'm not too sure which part of the loop to edit.
#include <stdio.h>
void printPatternHere(int height);
int main() {
int height;
printf("Enter the number of rows: ");
scanf("%d", &height);
printf("printPattern: \n");
printPatternHere(height);
return 0;
}
void printPatternHere(int height) {
int n, c, row, t = 1;
int counter = 0;
for (row = 1; row <= height; row++) {
for (c = 1; c <= height - row; c++)
t = row; // start each line with row number
for (c = 1; c <= row; c++) {
counter++;
if (counter > 9) {
t = t % 10;
}
printf("%d ", t);
t++;
}
for (c = 1 ; c < row; c++) {
t--; //reset row number
}
printf("\n");
}
}
If userinput is 14, its output should be something like this:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 0 1
7 8 9 0 1 2 3
8 9 0 1 2 3 4 5
9 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 0 1
2 3 4 5 6 7 8 9 0 1 2 3
3 4 5 6 7 8 9 0 1 2 3 4 5
4 5 6 7 8 9 0 1 2 3 4 5 6 7
Your function is cluttered too much, for loops are more than enough. Code below will be enough. Better to debug to grasp the idea.
// in "main"
printPattern3(height); // wrong function name!
printPatternHere(height); // corrected.
void printPatternHere(int height) {
int c, row;
for (row = 1; row <= height; row++) {
for (c = 0; c < row; c++) {
printf("%d ", (c + row) % 10);
}
printf("\n");
}
}
Your code is too complicated. The output of the pyramid can be done simpler.
For example this loop
for (c = 1; c <= height - row; c++)
t = row; // start each line with row number
does not make a sense.
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%u ", ( j + i + 1 ) % Base );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 14
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 0 1
7 8 9 0 1 2 3
8 9 0 1 2 3 4 5
9 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 0 1
2 3 4 5 6 7 8 9 0 1 2 3
3 4 5 6 7 8 9 0 1 2 3 4 5
4 5 6 7 8 9 0 1 2 3 4 5 6 7
Enter a non-negative number (0 - exit): 0
If you want to write a separate function that will output the pattern then the program can look the following way.
#include <stdio.h>
FILE * display_pyramid( unsigned int n, FILE *fp )
{
const unsigned int Base = 10;
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
fprintf( fp, "%u ", ( j + i + 1 ) % Base );
}
fputc( '\n', fp );
}
return fp;
}
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
fputc( '\n', display_pyramid( n, stdout ) );
}
return 0;
}

Eratosthenes prime numbers

I have created a program to search for prime numbers. It works without problems until the entered number is smaller than 52, when it is bigger output prints out some blank (0) numbers and I don't know why. Also other numbers have blank output.
My code is:
#include <stdio.h> //Prime numbers
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
int c[100], n, a[50], d, e, b = 1;
void sort() {
for (int i = 1; i < n; i++) {
if (c[i] > 1) {
a[b] = c[i];
printf("%d %d %d\n", a[1], b, i);
b++;
e = 2;
d = 0;
while (d <= n) {
d = c[i] * e;
c[d - 1] = 0;
e++;
}
}
}
}
int main() {
printf("Enter number as an limit:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
c[i] = i + 1;
}
sort();
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 1; i < b; i++) {
printf("%d ", a[i]);
}
return 0;
}
Here is output for 25:
Enter number as an limit:
25
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
But for 83 is:
Enter number as an limit:
83
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
2 10 28
2 11 30
2 12 36
2 13 40
2 14 42
2 15 46
2 16 52
0 17 58
0 18 60
0 19 66
0 20 70
0 21 72
0 22 78
0 23 82
Prime numbers between 1 and 83 are:
0 3 5 7 11 0 17 19 23 29 31 37 0 43 47 53 0 61 67 71 73 79 83
Blank spots always spots after 17th prime number. And always the blank numbers are the same. Can you help me please what is the problem?
The loop setting entries in c for multiples of c[i] runs too far: you should compute the next d before comparing against n:
for (d = c[i] * 2; d <= n; d += c[i]) {
c[d - 1] = 0;
}
As a matter of fact you could start at d = c[i] * c[i] because all lower multiples have already been seen during the previous iterations of the outer loop.
Also note that it is confusing to store i + 1 into c[i]: the code would be simpler with an array of booleans holding 1 for prime numbers and 0 for composite.
Here is a modified version:
#include <stdio.h>
int main() {
unsigned char c[101];
int a[50];
int n, b = 0;
printf("Enter number as a limit:\n");
if (scanf("%d", &n) != 1 || n < 0 || n > 100) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < n; i++) {
c[i] = 1;
}
for (int i = 2; i < n; i++) {
if (c[i] != 0) {
a[b] = i;
//printf("%d %d %d\n", a[0], b, i);
b++;
for (int d = i * i; d <= n; d += i) {
c[d] = 0;
}
}
}
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 0; i < b; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
chqrlie$ ./sieve4780
Enter number as a limit:
25
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
chqrlie$ ./sieve4780
Enter number as a limit:
83
Prime numbers between 1 and 83 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Your problem seems to be caused by the fact that you have declared an array with size 50, but in fact it goes further than that: imagine you want to use Eratosthenes' procedure to find the first 10,000 prime numbers. Does this mean that you need to declare an array of size 10,000 first (or even bigger), risking to blow up your memory?
No: best thing to do is to work with collections where you don't need to set the maximum size at declaration time, like a linked list, a vector, ..., like that you can make your list grow as much as you like during runtime.

Code for Binary search in C not working properly

I can't fix the logical error because I don't know what is wrong in this code. Every input, it shows "element not found". I would really appreciate it if someone can help me in this. Also in this code, I have assumed we'll be taking the size of the array as an odd number, what to do if we decide to take an even number as size?
#include<stdio.h>
int main(){
int size;
printf("Enter the number of elemets(odd number) : ");
scanf("%d",&size);
int arr[size];
printf("Enter the elements in ascending order : ");
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
int element;
int flag=0;
printf("Enter element to be found : ");
scanf("%d",&element);
int low=0;
int high=size-1;
while(low<high){
int mid=(low+high)/2;
if(element<arr[mid]){
high=mid-1;
}
else if(element>arr[mid]){
low=mid+1;
}
else if(element==arr[mid]){
printf("Element %d found at pos %d ",element,mid);
flag=1;
break;
}
}
if(flag==0){
printf("Element not found");
}
return 0;
}
The problem is your while test. You have:
while(low<high) {
...
}
This will fail when low == high if the desired value is at that position. It is easily fixed by changing the test to:
while(low <= high) {
...
}
This is all that's needed to fix it. You don't need to add any special cases to "fix it up". Just make sure your array is in ascending order and it should work.
EDIT: Refer to the better answer by #TomKarzes
My old answer is:
You missed a boundary case of high==low
#include<stdio.h>
int main(){
int size;
printf("Enter the number of elements(odd number) : ");
scanf("%d",&size);
int arr[size];
printf("Enter the elements in ascending order : ");
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
int element;
int flag=0;
printf("Enter element to be found : ");
scanf("%d",&element);
int low=0;
int high=size-1;
while(low<high){
int mid=(low+high)/2;
if(element<arr[mid]){
high=mid-1;
}
else if(element>arr[mid]){
low=mid+1;
}
else if(element==arr[mid]){
printf("Element %d found at pos %d ",element,mid);
flag=1;
break;
}
}
if(low==high && arr[low]==element) //Added 1 extra condition check that you missed
{
printf("Element %d found at pos %d ",element,low);
flag=1;
}
if(flag==0){
printf("Element not found");
}
return 0;
}
For starters for the number of elements of the array you shell use the type size_t. An object of the type int can be small to accommodate the number of elements in an array.
This condition of the loop
int high=size-1;
while(low<high){
//...
is incorrect. For example let's assume that the array has only one element. In this case high will be equal to 0 and hence equal to left due to its initialization
int high=size-1;
So the the loop will not iterate and you will get that the entered number is not found in the array though the first and single element fo the array actually will be equal to the number.
You need change the condition like
while ( !( high < low ) )
//...
This if statement within the else statement
else if(element==arr[mid]){
is redundant. You could just write
else // if(element==arr[mid]){
It would be better if the code that performs the binary search will be placed in a separate function.
Here is a demonstrative program that shows how such a function can be written.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int binary_search( const int a[], size_t n, int value )
{
size_t left = 0, right = n;
int found = 0;
while ( !found && left != right )
{
size_t middle = left + ( right - left ) / 2;
if ( value < a[middle] )
{
right = middle;
}
else if ( a[middle] < value )
{
left = middle + 1;
}
else
{
found = 1;
}
}
return found;
}
int cmp( const void *a, const void *b )
{
int left = *( const int * )a;
int right = *( const int * )b;
return ( right < left ) - ( left < right );
}
int main(void)
{
const size_t N = 15;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
size_t n = rand() % N + 1;
int a[n];
for ( size_t j = 0; j < n; j++ ) a[j] = rand() % N;
qsort( a, n, sizeof( int ), cmp );
for ( size_t j = 0; j < n; j++ )
{
printf( "%d ", a[j] );
}
putchar( '\n' );
int value = rand() % N;
printf( "The value %d is %sfound in the array\n",
value, binary_search( a, n, value ) == 1 ? "" : "not " );
}
return 0;
}
Its output might look for example the following way
0 2 2 3 4 5 7 7 8 9 10 12 13 13
The value 5 is found in the array
4 8 12
The value 10 is not found in the array
1 2 6 8 8 8 9 9 9 12 12 13
The value 10 is not found in the array
2 3 5 5 7 7 7 9 10 14
The value 11 is not found in the array
0 1 1 5 6 10 11 13 13 13
The value 7 is not found in the array
0 3 3 3 4 8 8 10 11 12 14 14 14 14
The value 3 is found in the array
0 5 5 10 11 11 12 13 13 14 14
The value 12 is found in the array
3 4 5 7 10 13 14 14 14
The value 14 is found in the array
0 3 3 7
The value 2 is not found in the array
1 6 9
The value 10 is not found in the array
2 2 3 3 4 4 4 5 5 6 8 8 9 13 13
The value 11 is not found in the array
11 11 13
The value 11 is found in the array
0 0 0 1 2 5 5 5 7 7 8 9 12 12 14
The value 6 is not found in the array
8 8 13
The value 1 is not found in the array
2 2 4 4 5 9 9 10 12 12 13 13 14 14
The value 14 is found in the array

Advanced Number Pattern

Given a Number N, Print the following pattern.
Input Format
The input contains a number N
Constraints
1 < N < 100
Output Format
The required pattern
for input 5 is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
and for input 3 is
1
2 5
3 4 6
this is the code i have tried .. but the results are not the same
#include<stdio.h>
void pattern(int n)
{
for(int i=1; i<=n; i++)
{
int k = i;
for(int j=1; j<=i; j++)
{
printf("%d ",k);
k = n - j + k;
}
printf("\n");
}
}
int main()
{
int n = 5;
pattern(n);
return 0;
}
this is the result of the above code
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
How should I modify the above code to get the Expected Output?
This is an interesting problem, and not an easy one. I'm not going to write a program to solve it (in part because I'm too lazy), but I can describe how I would solve it.
You already have an outer loop for(int i=1; i<=n; i++) which counts down the rows, and an inner loop for(int j=1; j<=i; j++) which counts across the columns. Those are both fine.
Inside the inner loop I would test if(j % 2 == 1). If j % 2 is 1 we're in an odd-numbered column, and we want to count down the column. But if j % 2 is 0, we're in an even column, and we have to do it the other way.
First I would have a variable which is the number that's supposed to be at the top of the column (1, 9, 10, 14, or 15 in the n=5 case). I'd have to compute that number two different ways, one for the "odd" columns and one for the "even".
And then I'd use that number as a base to count down the odd columns, and up the evens. Specifically: I'd add i to it in the odd columns, but subtract i in the even columns. But actually that's not quite right, because i is not 1 at the top of columns other than 1, so what I'd actually have to add or subtract would be some function of i and j. But I think you can work this out.
Here are my three cents.:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
unsigned int value = j % 2 == 0
? i + 1 + j * n - j * ( j + 1 ) / 2
: ( j + 1 ) * n - j * ( j + 1 ) / 2 - i + j;
printf( "%2u ", value );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look the following way
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 9
1
2 17
3 16 18
4 15 19 30
5 14 20 29 31
6 13 21 28 32 39
7 12 22 27 33 38 40
8 11 23 26 34 37 41 44
9 10 24 25 35 36 42 43 45
Enter a non-negative number no greater than 100 (0 - exit): 8
1
2 15
3 14 16
4 13 17 26
5 12 18 25 27
6 11 19 24 28 33
7 10 20 23 29 32 34
8 9 21 22 30 31 35 36
Enter a non-negative number no greater than 100 (0 - exit): 7
1
2 13
3 12 14
4 11 15 22
5 10 16 21 23
6 9 17 20 24 27
7 8 18 19 25 26 28
Enter a non-negative number no greater than 100 (0 - exit): 6
1
2 11
3 10 12
4 9 13 18
5 8 14 17 19
6 7 15 16 20 21
Enter a non-negative number no greater than 100 (0 - exit): 5
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Enter a non-negative number no greater than 100 (0 - exit): 4
1
2 7
3 6 8
4 5 9 10
Enter a non-negative number no greater than 100 (0 - exit): 3
1
2 5
3 4 6
Enter a non-negative number no greater than 100 (0 - exit): 2
1
2 3
Enter a non-negative number no greater than 100 (0 - exit): 1
1
Enter a non-negative number no greater than 100 (0 - exit): 0
Or using the recursive approach of calculating the output value the program can look the following way.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%2u ", value );
value += j % 2 == 0 ? 2 * ( n - i ) - 1 : 2 * ( i - j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
For example its output for the entered number equal to 10 looks like
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 0

I have some spacing error in my pascals triangle using C programming

The spacing is off in my code, can anyone help. I have attempted it (shown below)
#include <stdio.h>
int factorial(int n){
int fact = 1;
if(n == 0){
return 1;
} else {
for(int i = 1; i <= n; i++){
fact = fact * i;
}
return fact;
}
}
int choose(int n, int r)
{
int ans;
ans = (factorial(n))/((factorial(r))*(factorial(n-r)));
return ans;
}
void triangle(int numOfRows){
for(int n=0; n<numOfRows; n++)
{
for(int i=1; i<=numOfRows-n; i++){
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%5d ",choose(n,r)); // Changed to %3d
}
printf("\n");
}
}
int main(){
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
while(rows > 0 && rows <=13){
triangle(rows);
printf("Enter the number of rows: ");
scanf("%d", &rows);
}
return 0;
}
The expected output should be:
Thanks i'd appreciate it (this is also my first time using this site, so sorry for bad format stuff).
The program needs to work up to 13 rows (which is shown in my while loop in my main functions).
You need to make a couple of changes to have the triangle aligned to the left as in the expected output.
First, you are adding 3 extra spaces in the first loop with the printf(" "), that is fixed using < instead of <= in the loop condition.
Second, there are 4 extra chars added due to the "%5d " in the second printf call, you need to avoid that for the first iteration (when r == 0) using just "%d ".
Here's how the triangle() function will look like after the changes:
void triangle(int numOfRows) {
for(int n = 0; n < numOfRows; n++) {
for(int i = 1; i < numOfRows-n; i++) {
printf(" ");
}
for(int r = 0; r <= n; r++) {
printf(r == 0 ? "%d " : "%5d ", choose(n, r));
}
printf("\n");
}
}
And some example output (works up to 13 without a problem, at least on my 64-bit Linux with both gcc and clang):
Enter the number of rows: 3
1
1 1
1 2 1
Enter the number of rows: 4
1
1 1
1 2 1
1 3 3 1
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Enter the number of rows: 13
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1

Resources