Strucer Programming
Strucer Programming
// Division Approach
#include <stdio.h>
int isPrime(int N) {
if (N % i == 0) {
return 0;
return 1;
int main() {
int N = 10;
if (isPrime(N)) {
printf("Yes\n");
}
else {
printf("No\n");
return 0;
// a quadratic equation
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// equation ax*2 + bx + x
// If a is 0, then equation is
if (a == 0) {
printf("Invalid");
return;
}
int d = b * b - 4 * a * c;
if (d > 0) {
else if (d == 0) {
else // d < 0
sqrt_val / (2 * a));
// Driver code
int main()
// Function call
findRoots(a, b, c);
return 0;
#include <stdio.h>
// natural numbers
int recSum(int n)
// Base condition
if (n <= 1)
return n;
// Recursive call
// Driver code
int main()
int n = 10;
printf("Sum = %d ", recSum(n));
return 0;
// using recursion
#include <stdio.h>
// Base Case:
if (n == 1) {
return 1;
// of Ns
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
#include <stdio.h>
if (n < 3) {
return;
prev2 = prev1;
prev1 = curr;
// Function that handles the first two terms and calls the
// recursive function
void printFib(int n) {
if (n < 1) {
printf("Invalid number of terms\n");
else if (n == 1) {
else if (n == 2) {
else {
fib(n, 0, 1);
return;
int main() {
int n = 9;
printFib(n);
return 0;
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
int main()
return 0;
}
7. C Program to Find Largest Element in an Array
#include <stdio.h>
max = arr[i];
return max;
int main() {
return 0;
#include <math.h>
#include <stdio.h>
int main()
return 0;
#include <stdio.h>
#include <stdlib.h>
}
int main() {
return 0;
10. Write a program in C to sort an array in descending order using Bubble Sort
#include <stdio.h>
arr[j + 1] = temp;
}
int main() {
sort(arr, n);
return 0;
#include <stdio.h>
#include <stdlib.h>
// Custom comparator
int main() {
return 0;
#include <stdio.h>
arr[j + 1] = temp;
int main() {
bubbleSort(arr,n);
for (int i = 0; i < n; i++)
return 0;
// of a matrix
#include <stdio.h>
#define N 4
// of mat[][]
int i = 0, j = 0;
temp[i][j++] = mat[row][col];
if (j == n - 1)
j = 0;
i++;
dimension of mat[][]. */
// Initialize result
int D = 0;
// single element
if (n == 1)
return mat[0][0];
// To store cofactors
int temp[N][N];
int sign = 1;
// first row
D += sign * mat[0][f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
return D;
printf("n");
// Driver code
int main()
{3, 0, 0, 5},
{2, 1, 4, -3},
{1, 0, 5, 0}};
// Function call
determinantOfMatrix(mat, N));
return 0;
// C program to implement
#include <stdio.h>
#define N 4
// This function adds A[][] and B[][],
int i, j;
int i, j;
printf("\n");
// Driver code
int main()
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// To store result
int C[N][N];
int i, j;
printf("Matrix A is \n");
printmatrix(A);
printf("Matrix B is \n");
printmatrix(B);
add(A, B, C);
printmatrix(C);
return 0;
}
15. C Program to Add Two Rectangular Matrices
// C program to implement
#include <stdio.h>
#define M 4
#define N 3
int i, j;
for (i = 0; i < M; i++)
int i, j;
printf("\n");
// Driver code
int main()
int A[M][N] = {
{ 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 }
};
int B[M][N] = {
{ 2, 1, 1 }, { 1, 2, 2 }, { 2, 3, 3 }, { 3, 4, 4 }
};
printf("Matrix A is \n");
printmatrix(A);
printf("Matrix B is \n");
printmatrix(B);
// To store result
int C[M][N];
int i, j;
add(A, B, C);
printmatrix(C);
return 0;
}
16. C Program to Multiply Two Matrices
#include <stdio.h>
#include <stdlib.h>
int result[R1][C2];
result[i][j] = 0;
printf("%d\t", result[i][j]);
printf("\n");
// Driver code
int main()
// values in MACROs)
int m1[R1][C1] = { { 1, 1 }, { 2, 2 } };
int m2[R2][C2] = { { 1, 1, 1 }, { 2, 2, 2 } };
// if coloumn of m1 not equal to rows of m2
if (C1 != R2) {
"Matrix-2\n");
"#define section\n");
exit(EXIT_FAILURE);
// Function call
multiplyMatrix(m1, m2);
return 0;
#include <stdio.h>
int i = 0;
// Move to the end of str1
i++;
int j = 0;
s1[i] = s2[j];
i++;
j++;
s1[i] = '\0';
int main() {
concat(s1, s2);
printf("%s", s1);
return 0;
}
18. C Program to Concatenate Two Strings using Pointers
#include <stdio.h>
while (*s1)
s1++;
// pointer arithmetic
while (*s2) {
*s1 = *s2;
s1++;
s2++;
*s1 = '\0';
int main() {
concat(s1, s2);
printf("%s", s1);
return 0;
#include <stdio.h>
#include <string.h>
int main() {
char string1[20];
int i, length;
int flag = 0;
scanf("%s", string1);
length = strlen(string1);
flag = 1;
break;
} else {
return 0;
#include <stdio.h>
// Two pointers
int l = 0, r = n - 1;
while (l < r) {
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
int main() {
int arr[] = {1, 2, 3, 4, 5};
rev(arr, n);
return 0;
#include <stdio.h>
if (l >= r) {
return;
arr[l] = arr[r];
arr[r] = temp;
// remaining part
rev(arr, l + 1, r - 1);
}
int main() {
rev(arr, 0, n - 1);
return 0;
#include <stdio.h>
#include <string.h>
int main() {
printf("%lu", strlen(s));
return 0;
#include <stdio.h>
#include <stdlib.h>
// Function to find the largest element
int i;
// Driver Code
int main()
int i, N = 4;
int* arr;
// allocation
if (arr == NULL) {
exit(0);
*(arr + 0) = 14;
*(arr + 1) = 12;
*(arr + 2) = 19;
*(arr + 3) = 20;
// Function Call
findLargest(arr, N);
return 0;
#include <stdio.h>
int i, j, t;
t = *(ptr + i);
*(ptr + j) = t;
// Driver code
int main()
int n = 5;
return 0;
// using pointers
#include <stdio.h>
// using pointers
ptr = string;
++ptr;
--ptr;
if (*ptr == *rev) {
--ptr;
rev++;
}
else
break;
printf("String is Palindrome");
else
// Driver code
int main()
isPalindrome(str);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};
int main() {
// 5 Student's records
students[0].roll_number = 1;
students[0].name = "Geeks1";
students[0].age = 12;
students[0].total_marks = 78.50;
students[1].roll_number = 5;
students[1].name = "Geeks5";
students[1].age = 10;
students[1].total_marks = 56.84;
students[2].roll_number = 2;
students[2].name = "Geeks2";
students[2].age = 11;
students[2].total_marks = 87.94;
students[3].roll_number = 4;
students[3].name = "Geeks4";
students[3].age = 12;
students[3].total_marks = 89.78;
students[4].roll_number = 3;
students[4].name = "Geeks3";
students[4].age = 13;
students[4].total_marks = 78.55;
printf("========================================\n");
printf("========================================\n");
printf("========================================\n");
return 0;
}
27. C program to store Student records as Structures and Sort them by Name
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char* name;
int id;
char age;
};
((struct Student*)q)->name);
// Driver program
int main()
int i = 0, n = 5;
struct Student arr[n];
arr[0].id = 1;
arr[0].name = "bd";
arr[0].age = 12;
arr[1].id = 2;
arr[1].name = "ba";
arr[1].age = 10;
arr[2].id = 3;
arr[2].name = "bc";
arr[2].age = 8;
arr[3].id = 4;
arr[3].name = "aaz";
arr[3].age = 9;
arr[4].id = 5;
arr[4].name = "az";
arr[4].age = 10;
return 0;
28. C program to store Student records as Structures and Sort them by Age or ID
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char* name;
int id;
char age;
};
// Driver program
int main()
int i = 0, n = 5;
arr[0].id = 1;
arr[0].name = "bd";
arr[0].age = 12;
arr[1].id = 2;
arr[1].name = "ba";
arr[1].age = 10;
arr[2].id = 3;
arr[2].name = "bc";
arr[2].age = 8;
arr[3].id = 4;
arr[3].name = "aaz";
arr[3].age = 9;
arr[4].id = 5;
arr[4].name = "az";
arr[4].age = 10;
return 0;