Pps compulsory programs to learn?
1) Write a program to print Fibonacci series using functions?
#include <stdio.h>
void printFibonacci(int n) {
int a = 0, b = 1, c;
printf("%d %d ", a, b);
for (int i = 3; i <= n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printFibonacci(n);
return 0;
}
Output:
Enter the number of terms: 5 0 1 1 2 3
2) Write a program to find factorial of a number using recursion, without recursion?
i) With recursion
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial: %d\n", factorial(n));
return 0;
}
Output:
Enter a number: 5
Factorial: 120
ii) Without Recursion:
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial: %d\n", fact);
return 0;
}
Output:
Enter a number: 5
Factorial: 120
3) Write a c program for matrix multiplication of order m x n, n x p?
#include <stdio.h>
int main() {
int m, n, p;
printf("Enter dimensions of first matrix (m x n): ");
scanf("%d %d", &m, &n);
printf("Enter dimensions of second matrix (n x p): ");
scanf("%d %d", &n, &p);
int A[m][n], B[n][p], C[m][p];
printf("Enter elements of first matrix: \n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of second matrix: \n");
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++)
scanf("%d", &B[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < p; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++)
C[i][j] += A[i][k] * B[k][j];
}
printf("Resultant matrix: \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter dimensions of first matrix (m x n): 2 3
Enter dimensions of second matrix (n x p): 3 2
Enter elements of first matrix:
123
456
Enter elements of second matrix:
78
9 10
11 12
Resultant matrix: 58 64 139 154
4) Write a program for matrix addition and matrix subtraction?
#include <stdio.h>
int main() {
int m, n;
printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &n);
int A[m][n], B[m][n], sum[m][n];
printf("Enter elements of first matrix: \n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of second matrix: \n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &B[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sum[i][j] = A[i][j] + B[i][j];
printf("Matrix Addition Result: \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter number of rows and columns: 2 2
Enter elements of first matrix:
12
34
Enter elements of second matrix:
56
78
5) Write a program to read a string and check whether it is palindrome or not?
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int len, i, j;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for(i = 0, j = len - 1; j >= 0; i++, j--)
rev[i] = str[j];
rev[i] = '\0';
if (strcmp(str, rev) == 0)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
Output:
Enter a string: madam
Palindrome
6) Write linear search algorithm and program?
Algorithm:
Step 1: Start
Step 2: for i=0 to n-1 repeat step 3
Step 3: if a[i] is key then return i
Step 4: if i becomes n then return -1 to indicate searching unsuccessful.
Step 5: Stop
Program:
#include <stdio.h>
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {5, 3, 7, 1, 9, 2};
int size = sizeof(arr) / sizeof(arr[0]);
int target;
printf("Enter the element to search: ");
scanf("%d", &target)
int result = linearSearch(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Output:
Enter the element to search: 7
Element found at index 2
7) Write binary search algorithm and program?
Algorithm:
Step-1: Start
Step 2: Set l with 0 and r with n-1
Step 3: calculate middle position. ( mid=(l+r)/2 )
Step 4: if middle element is less than the key
element then Ignore the left half by
updating l to mid+1
Step 5: if middle element is greater than the
key element then Ignore the right half
by updating r to mid-1
Step 6: Otherwise the middle element matches with key element-
The search will end successfully here by returning middle position. i.e,
return mid; Step 7: Repeat steps 3,4,5,6 while l is less than or equal
to r.
Step 8: if above loop is terminated without returning any position, it means there is no key
element in the list. Therefore we can return -1.
Step-9: Stop
program:
#include <stdio.h>
int binarySearch(int a[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (key == a[mid]) {
return mid;
}
if (key < a[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
int main() {
int n, key, result;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int a[n];
printf("Enter %d elements in sorted order:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the value to search: ");
scanf("%d", &key);
result = binarySearch(a, n, key);
if (result != -1) {
printf("Value found at index=%d\n", result);
} else {
printf("Value not found\n");
}
return 0;
}
Output:
Enter the number of elements in the array: 10
Enter 10 elements in sorted order:
3
15
24
29
32
36
58
64
69
20
Enter the value to search: 15
Value found at index=1
8) Write bubble sort algorithm and program?
Program:
#include<stdio.h>
void BubbleSort(int[], int);
void main()
{
int a[10], i, n;
printf("Enter number of elements to be sorted: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
BubbleSort(a, n);
}
void BubbleSort(int a[], int n)
{
int i, j, temp;
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-i-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("\nSorted list is: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
Output:
Enter number of elements to be sorted: 4
Enter 4 elements: 2
1
5
4
Sorted list is: 1 2 4 5
9) Write selection sort algorithm and program?
Algorithm:
Step 1: Start
Step 2: for cur=0 to n-2 repeat steps 3,4 and 6
Step 3: set min position to i. (Assume starting element as smallest element)
Step 4: for j=cur+1 to n-1 repeat step 5(compare each element with current smallest)
Step 5: if a[j] is less than a[min] then (finding position of smallest element in
unsorted list) Update min position to j;
Step 6: if min is not equal to cur then
Swap a[cur] and a[min] (swapping smallest element with current element)
Step 7 : stop
Program:
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
selectionSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements: 4
Enter the elements: 2
1
5
4
Sorted array: 1 2 4 5
10) Write insertion sort algorithm and program?
Algorithm:
Step 1: start
Step 2: for i=1 to n-1
repeat step 3 Step 3:
for j=i to 1 repeat
step 4 Step 4: if (a[j]
is less than a[j-1]
then
Swap a[j] and a[j-1] (shift all the elements which are greater than the element to be
insert (i.e., a[j]) in sorted list.)
Step 5: Stop
Program:
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
insertionSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements: 4
Enter the elements: 2
1
3
6
Sorted array: 1 2 3 6
11) Write a program on storage classes?
#include <stdio.h>
extern int extVar;
void display() {
static int staticVar = 0;
auto int autoVar = 5;
register int registerVar = 10;
staticVar++;
autoVar++;
registerVar++;
printf("Static Variable: %d\n", staticVar);
printf("Auto Variable: %d\n", autoVar);
printf("Register Variable: %d\n", registerVar);
printf("Extern Variable: %d\n", extVar);
}
int extVar = 100;
int main() {
display();
display();
return 0;
}
Output:
Static Variable: 1
Auto Variable: 6
Register Variable: 11
Extern Variable: 100
Static Variable: 2
Auto Variable: 6
Register Variable: 11
Extern Variable: 100
12) Write a program using dynamic memory allocation?
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n, newSize, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Entered elements are:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter new size for the array: ");
scanf("%d", &newSize);
arr = (int *)realloc(arr, newSize * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
printf("Enter %d more elements:\n", newSize - n);
for (i = n; i < newSize; i++) {
scanf("%d", &arr[i]);
}
printf("Updated elements are:\n");
for (i = 0; i < newSize; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Output:
Enter the number of elements: 4
Enter 4 elements:
10
20
30
40
Entered elements are:
10 20 30 40
Enter new size for the array: 6
Enter 2 more elements:
50
60
Updated elements are:
10 20 30 40 50 60
13)Write simple program demonstrating the use of rewind, ftell, and fseek functions in C ?
#include <stdio.h>
int main() {
FILE *file;
long size;
file = fopen("example.txt", "w+");
if (file == NULL) {
printf("Unable to open file\n");
return 1;
}
fprintf(file, "Hello, World!\nThis is a test.");
rewind(file);
size = ftell(file);
printf("File pointer position after rewind: %ld\n", size);
fseek(file, 7, SEEK_SET);
char ch = fgetc(file);
printf("Character at position 7: %c\n", ch);
fclose(file);
return 0;
}
Output:
File pointer position after rewind: 0
Character at position 7: W
14) C program to merge two files into a third file?
#include <stdio.h>
int main() {
FILE *file1, *file2, *file3;
char ch;
file1 = fopen("file1.txt", "r");
if (file1 == NULL) {
printf("Unable to open file1.txt\n");
return 1;
}
file2 = fopen("file2.txt", "r");
if (file2 == NULL) {
printf("Unable to open file2.txt\n");
fclose(file1);
return 1;
}
file3 = fopen("file3.txt", "w");
if (file3 == NULL) {
printf("Unable to create file3.txt\n");
fclose(file1);
fclose(file2);
return 1;
}
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, file3);
}
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, file3);
}
printf("Files merged successfully into file3.txt\n");
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
Output:
file1.txt
Hello, this is the first file.
file2.txt
This is the second file.
File3.txt
Hello, this is the first file.This is the second file.
Output:
Files merged successfully into file3.txt