0% found this document useful (0 votes)
19 views9 pages

C Programs Solutions Updated

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)
19 views9 pages

C Programs Solutions Updated

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/ 9

C Programming Questions and Solutions

1) Display Pyramid Pattern

#include <stdio.h>

void displayPattern(int n) {

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");

int main() {

int rows;

printf("Enter number of rows: ");

scanf("%d", &rows);

displayPattern(rows);

return 0;

2) Check Character Type

#include <stdio.h>

int main() {

char c;

printf("Enter a character: ");


scanf(" %c", &c);

if (c >= 'A' && c <= 'Z')

printf("Uppercase letter\n");

else if (c >= 'a' && c <= 'z')

printf("Lowercase letter\n");

else

printf("Special character\n");

return 0;

3) Matrix Addition

#include <stdio.h>

void readMatrix(int mat[4][4], int rows, int cols) {

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

scanf("%d", &mat[i][j]);

void printMatrix(int mat[4][4], int rows, int cols) {

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

for (int j = 0; j < cols; j++)

printf("%d ", mat[i][j]);

printf("\n");

int main() {

int A[4][4], B[4][4], C[4][4];

int M, N, P, Q;
printf("Enter dimensions of matrix A (MxN): ");

scanf("%d %d", &M, &N);

printf("Enter dimensions of matrix B (PxQ): ");

scanf("%d %d", &P, &Q);

if (M != P || N != Q) {

printf("Addition not possible\n");

return 0;

printf("Enter elements of matrix A:\n");

readMatrix(A, M, N);

printf("Enter elements of matrix B:\n");

readMatrix(B, M, N);

for (int i = 0; i < M; i++)

for (int j = 0; j < N; j++)

C[i][j] = A[i][j] + B[i][j];

printf("Matrix A:\n");

printMatrix(A, M, N);

printf("Matrix B:\n");

printMatrix(B, M, N);

printf("Resultant Matrix C:\n");

printMatrix(C, M, N);

return 0;

}
C Programming Questions and Solutions

1) Display Pyramid Pattern

#include <stdio.h>

void displayPattern(int n) {

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");

int main() {

int rows;

printf("Enter number of rows: ");

scanf("%d", &rows);

displayPattern(rows);

return 0;

2) Check Character Type

#include <stdio.h>

int main() {

char c;

printf("Enter a character: ");


scanf(" %c", &c);

if (c >= 'A' && c <= 'Z')

printf("Uppercase letter\n");

else if (c >= 'a' && c <= 'z')

printf("Lowercase letter\n");

else

printf("Special character\n");

return 0;

3) Matrix Addition

#include <stdio.h>

void readMatrix(int mat[4][4], int rows, int cols) {

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

scanf("%d", &mat[i][j]);

void printMatrix(int mat[4][4], int rows, int cols) {

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

for (int j = 0; j < cols; j++)

printf("%d ", mat[i][j]);

printf("\n");

int main() {

int A[4][4], B[4][4], C[4][4];

int M, N, P, Q;
printf("Enter dimensions of matrix A (MxN): ");

scanf("%d %d", &M, &N);

printf("Enter dimensions of matrix B (PxQ): ");

scanf("%d %d", &P, &Q);

if (M != P || N != Q) {

printf("Addition not possible\n");

return 0;

printf("Enter elements of matrix A:\n");

readMatrix(A, M, N);

printf("Enter elements of matrix B:\n");

readMatrix(B, M, N);

for (int i = 0; i < M; i++)

for (int j = 0; j < N; j++)

C[i][j] = A[i][j] + B[i][j];

printf("Matrix A:\n");

printMatrix(A, M, N);

printf("Matrix B:\n");

printMatrix(B, M, N);

printf("Resultant Matrix C:\n");

printMatrix(C, M, N);

return 0;

}
4) Compute sin(x)/cos(x) using Taylor Series

#include <stdio.h>

#include <math.h>

#define PI 3.14159265

double taylor_sin(double x, int terms) {

double result = 0.0, term = x;

int sign = 1;

for (int i = 1; i <= terms; i++) {

result += sign * term;

term *= x * x / ((2 * i) * (2 * i + 1));

sign = -sign;

return result;

double taylor_cos(double x, int terms) {

double result = 1.0, term = 1.0;

int sign = -1;

for (int i = 1; i <= terms; i++) {

term *= x * x / ((2 * i - 1) * (2 * i));

result += sign * term;

sign = -sign;

return result;

You might also like