0% found this document useful (0 votes)
5 views

C_Code_Snippets_With_Output

The document contains three C programs that generate different patterns: a binary number triangle, a character pattern triangle, and a spiral number pattern. Each program defines a function to create the respective pattern and includes a main function to execute it. The outputs of the programs illustrate the generated patterns in a structured format.

Uploaded by

asfcsharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C_Code_Snippets_With_Output

The document contains three C programs that generate different patterns: a binary number triangle, a character pattern triangle, and a spiral number pattern. Each program defines a function to create the respective pattern and includes a main function to execute it. The outputs of the programs illustrate the generated patterns in a structured format.

Uploaded by

asfcsharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Binary Number Triangle

#include <stdio.h>

// Function to print a binary number triangle


void binaryTriangle() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", (i + j) % 2); // Alternate 1 and 0
}
printf("\n");
}
}

int main() {
binaryTriangle();
return 0;
}

Output:

0
1 0
0 1 0
1 0 1 0
0 1 0 1 0

Character Pattern Triangle


#include <stdio.h>

// Function to print a character pattern triangle


void characterTriangle() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1);
}
printf("\n");
}
}

int main() {
characterTriangle();
return 0;
}

Output:

A
A B
A B C
A B C D
A B C D E
Spiral Number Pattern
#include <stdio.h>

// Function to print a spiral number pattern


void spiralNumberPattern() {
int n = 4;
int matrix[10][10] = {0};
int num = 1;
int left = 0, right = n - 1, top = 0, bottom = n - 1;

while (num <= n * n) {


// Left to Right
for (int i = left; i <= right; i++)
matrix[top][i] = num++;
top++;

// Top to Bottom
for (int i = top; i <= bottom; i++)
matrix[i][right] = num++;
right--;

// Right to Left
for (int i = right; i >= left; i--)
matrix[bottom][i] = num++;
bottom--;

// Bottom to Top
for (int i = bottom; i >= top; i--)
matrix[i][left] = num++;
left++;
}

// Print the matrix


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%2d ", matrix[i][j]);
printf("\n");
}
}

int main() {
spiralNumberPattern();
return 0;
}

Output:

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

You might also like