0% found this document useful (0 votes)
11 views19 pages

Assignment 4

The document contains a series of C programming assignments focused on generating various patterns using loops. Each assignment includes source code for different patterns such as stars, numbers, and Fibonacci sequences, along with explanations of the functions used to create them. The document serves as a guide for practicing programming skills in C by implementing these patterns.

Uploaded by

papercloudds
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)
11 views19 pages

Assignment 4

The document contains a series of C programming assignments focused on generating various patterns using loops. Each assignment includes source code for different patterns such as stars, numbers, and Fibonacci sequences, along with explanations of the functions used to create them. The document serves as a guide for practicing programming skills in C by implementing these patterns.

Uploaded by

papercloudds
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/ 19

ASSIGNMENT 4

1. Write a C program to generate the following patterns:


a. * b. * c. * * * *
* * * * * * *
*** * * * * *
**** * * * * *

Source Code:
a.
#include <stdio.h>

// Function to print pattern a


void patternA(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int n = 4; // Number of rows for each pattern
printf("Pattern A:\n");
patternA(n);

return 0;
}
Output:

b.
#include <stdio.h>

// Function to print pattern b


void patternB(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}

printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern B:\n");
patternB(n);
return 0;
}

Output:

c.
#include <stdio.h>

// Function to print inverted right-aligned triangle pattern


void patternB(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < i; j++) {
printf("* ");
}
printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern B:\n");
patternB(n);

return 0;
}

Output:

2. . Write a C program to generate the following patterns:


a. *
* *
* * *
* * * *
* * * * *

Source Code:
#include <stdio.h>

// Function to print centered pyramid pattern


void patternA(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < i; j++) {
printf("* ");
}
printf("\n");
}
}

int main() {
int n = 5; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:

b. 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code:
#include <stdio.h>

// Function to print left-aligned number pattern


void patternA(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
}

int main() {
int n = 5; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:
c. 1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

Souce Code:
#include <stdio.h>

// Function to print centered pyramid with decreasing and increasing numbers


void patternA(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces for alignment
for (int j = 0; j < n - i; j++) {
printf(" ");
}
// Print decreasing numbers
for (int j = i; j >= 1; j--) {
printf("%d ", j);
}
// Print increasing numbers
for (int j = 2; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
}

int main() {
int n = 5; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:

3. Write a C program to generate the following patterns:


a. 1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

Source Code:
#include <stdio.h>

// Function to print centered pyramid with increasing and decreasing numbers


void patternA(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces for alignment
for (int j = 0; j < n - i; j++) {
printf(" ");
}
// Print increasing numbers
int num = i;
for (int j = 0; j < i; j++) {
printf("%d ", num++);
}
// Print decreasing numbers
num -= 2;
for (int j = 0; j < i - 1; j++) {
printf("%d ", num--);
}
printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:
b. 0
1 1
2 3 5
8 13 21

Source Code:
#include <stdio.h>

// Function to print Fibonacci triangle pattern


void patternA(int n) {
int a = 0, b = 1, temp;
for (int i = 1; i <= n; i++) {
int x = a, y = b;
for (int j = 1; j <= i; j++) {
printf("%d ", x);
temp = x + y;
x = y;
y = temp;
}
a = x;
b = y;
printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:

c. 1
121
12321
1234321
12321
121
1

Source Code:
#include <stdio.h>

// Function to print the given symmetric number pyramid pattern


void patternA(int n) {
// Upper half of the pattern
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
for (int j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
// Lower half of the pattern
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
for (int j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}
Output:

4. . Write a C program to generate the following patterns:


a. 1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Source Code:
#include <stdio.h>

// Function to print the alternating binary pattern


void patternA(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", (i + j) % 2);
}
printf("\n");
}
}
int main() {
int n = 5; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:

b. 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Source Code:
#include <stdio.h>

// Function to print Pascal's Triangle


void patternA(int n) {
for (int i = 0; i < n; i++) {
int num = 1;
// Print spaces for alignment
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
// Print Pascal's Triangle values
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num = num * (i - j) / (j + 1);
}
printf("\n");
}
}

int main() {
int n = 5; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:
c. * * * *
* * *
* *
*
* *
* * *
* * * *

Source Code:
#include <stdio.h>

// Function to print the hourglass star pattern


void patternA(int n) {
// Upper inverted triangle
for (int i = n; i >= 1; i--) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < i; j++) {
printf("* ");
}
printf("\n");
}
// Lower triangle
for (int i = 2; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < i; j++) {
printf("* ");
}
printf("\n");
}
}

int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:
d. 1 1
333 3 3 3
55555 5 5 5 5 5
7777777 7 7 7 7 7 7 7
55555 5 5 5 5 5
333 3 3 3
1 1

Source Code:

#include <stdio.h>

// Function to print the given numeric pattern


void patternA(int n) {
int numbers[] = {1, 3, 5, 7, 5, 3, 1}; // Pattern sequence
int max_width = n * 2 - 1; // Maximum width for center alignment

for (int i = 0; i < 7; i++) {


int spaces = (max_width - numbers[i]) / 2; // Calculate leading spaces

for (int j = 0; j < spaces; j++) {


printf(" ");
}
for (int j = 0; j < numbers[i]; j++) {
printf("%d ", numbers[i]);
}
printf("\n");
}
}
int main() {
int n = 4; // Number of rows for the pattern

printf("Pattern A:\n");
patternA(n);

return 0;
}

Output:

You might also like