0% found this document useful (0 votes)
7 views12 pages

C Programs for Patterns & Sequences

The document provides step-by-step solutions for various C programming problems focused on patterns, sequences, and series. It includes algorithms and code for generating different patterns (e.g., triangles, squares, diamonds), arithmetic and geometric progressions, Fibonacci and Tribonacci sequences, and approximations of π and e using series. Each problem is clearly defined with input validation and example outputs.

Uploaded by

daddyrizz454
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)
7 views12 pages

C Programs for Patterns & Sequences

The document provides step-by-step solutions for various C programming problems focused on patterns, sequences, and series. It includes algorithms and code for generating different patterns (e.g., triangles, squares, diamonds), arithmetic and geometric progressions, Fibonacci and Tribonacci sequences, and approximations of π and e using series. Each problem is clearly defined with input validation and example outputs.

Uploaded by

daddyrizz454
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

Solutions: C Programs (Patterns, Sequences & Series)

Prepared as step-by-step solutions by your professor-like assistant. Each program is in C


and includes a short algorithm followed by the code.

Problem 6: Simple patterns (n rows)


Notes: For these examples take n = 5. Each subproblem is a separate program.

6.1 Ascending $ triangle (rows 1..n) - Algorithm:

1) Read n (n>0). 2) For i from 1 to n: print $ repeated i times then newline.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter n: ");

if (scanf("%d", &n) != 1 || n <= 0) {

printf("Invalid input. n must be > 0.\n");

return 1;

for (i = 1; i <= n; i++) {

for (j = 0; j < i; j++) putchar('$');

putchar('\n');

return 0;

6.2 Descending $ triangle (rows n..1) - Algorithm:

1) Read n. 2) For i from n down to 1: print $ repeated i times then newline.

#include <stdio.h>
int main() {

int n, i, j;

printf("Enter n: ");

if (scanf("%d", &n) != 1 || n <= 0) {

printf("Invalid input. n must be > 0.\n");

return 1;

for (i = n; i >= 1; i--) {

for (j = 0; j < i; j++) putchar('$');

putchar('\n');

return 0;

6.3 Numeric decreasing rows (example: for n=5):

Output:
12345
1234
123
12
1
Algorithm: For i from n down to 1 print numbers 1..i on each line.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter n: ");

if (scanf("%d", &n) != 1 || n <= 0) {

printf("Invalid input. n must be > 0.\n");

return 1;

}
for (i = n; i >= 1; i--) {

for (j = 1; j <= i; j++) printf("%d", j);

putchar('\n');

return 0;

6.4 Numeric increasing rows (optional):

Output for n=5:


1
12
123
1234
12345
Algorithm: For i from 1 to n print numbers 1..i.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter n: ");

if (scanf("%d", &n) != 1 || n <= 0) {

printf("Invalid input. n must be > 0.\n");

return 1;

for (i = 1; i <= n; i++) {

for (j = 1; j <= i; j++) printf("%d", j);

putchar('\n');

return 0;

}
Problem 7: More patterns (hollow square, triangle, diagonals, diamond,
palindromic numbers)
We present a few commonly requested patterns. Each is its own small program.

7.1 Hollow square (size n):

Algorithm: For i=1..n and j=1..n, print "*" when on border (i==1||i==n||j==1||j==n), else
print space.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter size n (>=1): ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

for (i = 1; i <= n; i++) {

for (j = 1; j <= n; j++) {

if (i == 1 || i == n || j == 1 || j == n) printf("* ");

else printf(" ");

putchar('\n');

return 0;

7.2 Hollow right-angled triangle (n rows):

Algorithm: For row i, for col j=1..i print "*" if j==1 or j==i or i==n else print space.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter n (rows): ");


if (scanf("%d", &n) != 1 || n <= 0) return 1;

for (i = 1; i <= n; i++) {

for (j = 1; j <= i; j++) {

if (j == 1 || j == i || i == n) printf("* ");

else printf(" ");

putchar('\n');

return 0;

7.3 Square with diagonals (use # for visible marks):

Algorithm: In an n x n grid mark border and both diagonals: if


i==1||i==n||j==1||j==n||i==j||j==n-i+1 then print "# " else print two spaces.

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter n (>=1): ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

for (i = 1; i <= n; i++) {

for (j = 1; j <= n; j++) {

if (i==1 || i==n || j==1 || j==n || i==j || j==n-i+1)


printf("# ");

else printf(" ");

putchar('\n');

return 0;

}
7.4 Diamond of stars (total height = 2*n-1):

Algorithm: Print upper pyramid (i=1..n) with (n-i) spaces and (2*i-1) stars, then mirror it
for i=n-1..1.

#include <stdio.h>

int main() {

int n, i, j, k;

printf("Enter n (number of rows for upper half): ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

// upper half

for (i = 1; i <= n; i++) {

for (j = 0; j < n - i; j++) printf(" ");

for (k = 0; k < 2 * i - 1; k++) printf("*");

putchar('\n');

// lower half

for (i = n - 1; i >= 1; i--) {

for (j = 0; j < n - i; j++) printf(" ");

for (k = 0; k < 2 * i - 1; k++) printf("*");

putchar('\n');

return 0;

7.5 Palindromic number pyramid (e.g., row for i=3: 12321):

Algorithm: For each row i (1..n) print leading spaces, then numbers 1..i, then i-1..1.

#include <stdio.h>

int main() {
int n, i, j;

printf("Enter n: ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

for (i = 1; i <= n; i++) {

for (j = 0; j < n - i; j++) printf(" ");

for (j = 1; j <= i; j++) printf("%d", j);

for (j = i - 1; j >= 1; j--) printf("%d", j);

putchar('\n');

return 0;

Problem 8: Arithmetic and Geometric Progression


8.1 Arithmetic Progression (AP): Given first term a, difference d and n, print first n terms.

#include <stdio.h>

int main() {

long long a, d;

int n, i;

printf("Enter a (first term), d (difference) and n (number of


terms): ");

if (scanf("%lld %lld %d", &a, &d, &n) != 3 || n <= 0) return 1;

for (i = 0; i < n; i++) {

printf("%lld ", a + i * d);

putchar('\n');

return 0;

8.2 Geometric Progression (GP): Given first term a, multiplier r and n, print first n terms.

#include <stdio.h>
int main() {

long long a, r;

int n, i;

printf("Enter a (first term), r (multiplier) and n (number of


terms): ");

if (scanf("%lld %lld %d", &a, &r, &n) != 3 || n <= 0) return 1;

long long term = a;

for (i = 0; i < n; i++) {

printf("%lld ", term);

term = term * r;

putchar('\n');

return 0;

Problem 9: Fibonacci sequence (first n terms)


We use the common convention: F0 = 0, F1 = 1, then Fn = F(n-1) + F(n-2).

#include <stdio.h>

int main() {

int n, i;

printf("Enter n (number of terms): ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

long long a = 0, b = 1, c;

if (n >= 1) printf("%lld ", a);

if (n >= 2) printf("%lld ", b);

for (i = 3; i <= n; i++) {

c = a + b;

printf("%lld ", c);


a = b; b = c;

putchar('\n');

return 0;

Problem 10: Tribonacci sequence (first n terms)


One convention: T0 = 0, T1 = 0, T2 = 1, then Tn = T(n-1)+T(n-2)+T(n-3). The program below
follows that.

#include <stdio.h>

int main() {

int n, i;

printf("Enter n (number of terms): ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

long long t0 = 0, t1 = 0, t2 = 1, t;

if (n >= 1) printf("%lld ", t0);

if (n >= 2) printf("%lld ", t1);

if (n >= 3) printf("%lld ", t2);

for (i = 4; i <= n; i++) {

t = t0 + t1 + t2;

printf("%lld ", t);

t0 = t1; t1 = t2; t2 = t;

putchar('\n');

return 0;

Problem 11: Check if two numbers are consecutive Fibonacci numbers


Algorithm: Generate Fibonacci numbers up to max(n1,n2) and check consecutive pairs.
Treat the pair (1,1) as consecutive (because 1 appears twice).
#include <stdio.h>

#include <stdlib.h>

int main() {

long long n1, n2;

printf("Enter two positive integers: ");

if (scanf("%lld %lld", &n1, &n2) != 2 || n1 <= 0 || n2 <= 0) return


1;

long long a = 0, b = 1;

if ((n1 == 1 && n2 == 1)) { printf("Yes - they are consecutive


Fibonacci numbers (1,1)\\n"); return 0; }

while (b <= n2 || b <= n1) {

long long c = a + b;

if (a == n1 && b == n2) { printf("Yes - %lld and %lld are


consecutive Fibonacci numbers.\\n", n1, n2); return 0; }

a = b; b = c;

if (b > 0 && b > n1 && b > n2) break; // safety stop

// also check reverse order in case user provided numbers in


reverse

a = 0; b = 1;

while (b <= n1 || b <= n2) {

long long c = a + b;

if (a == n2 && b == n1) { printf("Yes - they are consecutive in


reverse order.\\n"); return 0; }

a = b; b = c;

if (b > 0 && b > n1 && b > n2) break;

printf("No - they are not consecutive Fibonacci numbers.\\n");

return 0;
}

Problem 12: Approximate π using the Leibniz series


Note: One classical series is π/4 = 1 - 1/3 + 1/5 - 1/7 + ... (also arctan(1) expansion). Use n
terms of this series.

#include <stdio.h>

int main() {

int n, k;

double sum = 0.0;

printf("Enter number of terms n: ");

if (scanf("%d", &n) != 1 || n <= 0) return 1;

for (k = 0; k < n; k++) {

double term = (k % 2 == 0 ? 1.0 : -1.0) / (2.0 * k + 1.0);

sum += term;

double pi_approx = 4.0 * sum;

printf("Approximate value of pi using %d terms: %.15f\n", n,


pi_approx);

return 0;

Problem 13: Approximate e^x using Taylor series


Use e^x = sum_{k=0}^{n-1} x^k / k!. We compute iteratively to avoid recomputing factorials
each time.

#include <stdio.h>

int main() {

int n, k;

double x;

printf("Enter x and number of terms n: ");


if (scanf("%lf %d", &x, &n) != 2 || n <= 0) return 1;

double sum = 1.0; // term for k=0

double term = 1.0;

for (k = 1; k < n; k++) {

term = term * x / k; // update term to x^k / k!

sum += term;

printf("Approximation of e^%.3f using %d terms: %.15f\n", x, n,


sum);

return 0;

Notes and tips:


- For large n some results (GP, factorials) may overflow integer types; use double or long
double where needed.
- The Tribonacci and Fibonacci programs use iterative loops for clarity and efficiency.
- For better precision in pi and e approximations use larger n and double precision.

End of document.

You might also like