Pps compulsory programs to learn
Pps compulsory programs to learn
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
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);
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
int main() {
int m, n;
printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &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
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;
Algorithm:
Step-1: Start
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 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
Algorithm:
Step 1: Start
Program:
#include <stdio.h>
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:
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);
#include <stdio.h>
int main() {
FILE *file;
long size;
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;
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