Program -1
(a) Write a program to calculate simple & compound interest
#include <stdio.h>
#include <math.h>
// Function to calculate simple interest
float calculateSimpleInterest(float principal, float rate, int
time) { return (principal*rate*time)/100;
// Function to calculate compound interest
float calculateCompoundInterest(float principal, float rate,
int time)
{ return principal*(pow((1+ rate/100), time)) - principal;
}
int main() {
float principal,
rate;
int time;
// Input values
printf("Enter principal amount:
"); scanf("%f", &principal);
printf("Enter annual interest rate:
"); scanf("%f", &rate);
printf("Enter time in years:
"); scanf("%d", &time);
// Calculate and display simple interest
float simpleInterest = calculateSimpleInterest(principal,
rate, time); printf("\nSimple Interest: %.2f\n",
simpleInterest);
// Calculate and display compound interest
float compoundInterest = calculateCompoundInterest(principal,
rate, time);
printf("Compound Interest: %.2f\n", compoundInterest);
return 0;
}
Output:
Enter principal amount:
25000
Enter annual interest rate:
5
Enter time in years: 2
Simple Interest: 2500.00
Compound Interest:
2562.50
(b) Write a C Program to interchange two numbers
#include <stdio.h>
int main() {
int num1, num2,
temp; // Input values
printf("Enter first number:
"); scanf("%d", &num1);
printf("Enter second number:
"); scanf("%d", &num2);
// Swapping using a temporary
variable temp = num1;
num1 =
num2;
num2 = temp;
// Displaying the swapped values
printf("\nAfter swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n",
num2);
return 0;
}
Output:
Enter first number: 5
Enter second number:
8 After swapping:
First number: 8
Second number: 5
Program-2
Find the biggest of three numbers using C.
#include <stdio.h>
int main() {
int num1, num2,
num3; // Input values
printf("Enter the first number:
"); scanf("%d", &num1);
printf("Enter the second number:
"); scanf("%d", &num2);
printf("Enter the third number:
"); scanf("%d", &num3);
// Check for the largest number
if (num1 >= num2 && num1 >= num3) {
printf("The largest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >=
num3) {
printf("The largest number is: %d\n",
num2);
} else {
printf("The largest number is: %d\n", num3);
}
return 0;
}
Output:
Enter the first number: 5
Enter the second number:
8
Enter the third number: 9
The largest number is: 9
Enter the first number: 5
Enter the second number:
8
Enter the third number: 9
The largest number is: 9
Program 3:
Write a C Program to find the sum of individual digits of a positive integer
#include <stdio.h>
int main()
{
int num, originalNum, remainder, sum
= 0; // Input a positive integer
printf("Enter a positive integer:
"); scanf("%d", &num);
// Store the original number for later
comparison originalNum = num;
// Calculate the sum of individual
digits while (num > 0){
remainder = num %
10;
sum += remainder;
num /= 10;
// Display the result
printf("The sum of individual digits of %d is: %d\n", originalNum,
sum);
return 0;
Output:
Enter a positive integer: 5489
The sum of individual digits of 5489 is: 26
Program 4:
A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding
two terms in the sequence.
#include <stdio.h>
int main()
{
int n, firstTerm = 0, secondTerm = 1, nextTerm;
// Input the number of terms
printf("Enter the number of terms in the Fibonacci
Sequence: "); scanf("%d", &n);
// Display the first two terms
printf("Fibonacci Sequence: %d, %d", firstTerm,
secondTerm); // Generate and display the rest of the
terms
for (int i = 3; i <= n; ++i)
{
nextTerm = firstTerm +
secondTerm; printf(", %d",
nextTerm);
firstTerm = secondTerm;
secondTerm =
nextTerm;
}
printf("\n");
return 0;
}
Output:
Enter the number of terms in the Fibonacci Sequence: 20
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181
Program-5
Write a C program to check whether a number is Armstrong or not.
#include <stdio.h>
#include <math.h>
// Function to calculate the number of digits in a given number
int countDigits(int num)
{
int count = 0;
while (num !=
0)
{
num /= 10;
++count;
}
return count;
}
//Function to check if a number is an Armstrong number
int isArmstrong(int num)
{
int originalNum, remainder, result = 0, n;
originalNum = num;
// Count the number of digits n = countDigits(num);
// Calculate the sum of digits each raised to the power of n
while (originalNum != 0)
{
remainder = originalNum %
10; result +=
pow(remainder, n);
originalNum /= 10;
}
// Check if the number is an Armstrong number
return (result ==
num);
}
int main()
int num;
// Input a number
printf("Enter a number:
"); scanf("%d", &num);
// Check and display the result
if (isArmstrong(num))
{
printf("%d is an Armstrong number.\n", num);
} else
{
printf("%d is not an Armstrong number.\n",
num);
return 0;
}
Output:
Enter a number: 153
153 is not an Armstrong number.
Enter a number: 121
121 is not an Armstrong number.
Program -6
Write a C Program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int num)
{
if (num <= 1 )
return false;
for (int i = 2; i * i <= num; ++ i
)
{
if (num % i ==0)
{ return false;
return true;
int main() {
int n;
// Input the value of n
printf("Enter the value of n:
"); scanf("%d", &n);
// Display prime numbers between 1 and n
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 2; i <= n; ++ i
)
{
if (isPrime(i)) {
printf("%d\n", i);
return 0;
Output:
Enter the value of n: 20
Prime numbers between 1 and 20
are:
2
3
5
7
11
13
17
19
Program-7:
Write a c program that implements searching of given item in given list
#include <stdio.h>
//Function to perform linear search in the list
int linearSearch(int arr[], int size, int
key)
{
for (int i = 0; i < size; +
+i)
{
if (arr[i] ==
key)
{
return i;
// Return the index of the found item
return -1; // Return -1 if the item is not
found } int main() {
int size, key;
// Input the size of the list
printf("Enter the size of the list: "); scanf("%d",
&size); int arr[size];
// Input the elements of the list
printf("Enter %d elements:\n",
size); for (int i = 0; i < size; ++i)
scanf("%d", &arr[i]);
// Input the item to search
printf("Enter the item to search: ");
scanf("%d", &key);
// Perform linear search
int index = linearSearch(arr, size,
key); // Display the result
if (index != -1) {
printf("%d found at index %d.\n", key,
index);
} else {
printf("%d not found in the list.\n", key);
}
return 0;
}
Output:
Enter the size of the list:
5 Enter 5 elements:
10 25 7 14 30
Enter the item to search: 30
30 found at index 4.
Program-8
Write a C program that uses functions to perform the following: Addition
of two matrices. Multiplication of two matrices.
#include <stdio.h>
#define MAX 10
void getMatrix(int matrix[MAX][MAX], int rows, int
cols) {
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i,
j);
scanf("%d", &matrix[i][j]);
}
}
}
void printMatrix(int matrix[MAX][MAX], int rows, int
cols) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{
printf("%d ", matrix[i]
[j]);
}
printf("\n");
}
}
void addMatrices(int matrix1[MAX][MAX], int matrix2[MAX]
[MAX], int result[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i]
[j];
}
}
}
void multiplyMatrices(int matrix1[MAX][MAX], int matrix2[MAX]
[MAX], int result[MAX][MAX], int rows1, int cols1, int rows2, int
cols2) {
if (cols1 != rows2) {
printf("Matrix multiplication not
possible!\n");
return;
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++)
{
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k]
[j];
}
}
} }
int main() {
int matrix1[MAX][MAX], matrix2[MAX][MAX], result[MAX]
[MAX]; int rows1, cols1, rows2, cols2;
printf("Enter rows and columns for first
matrix: "); scanf("%d %d", &rows1, &cols1);
printf("Enter rows and columns for second
matrix: "); scanf("%d %d", &rows2, &cols2);
printf("Enter elements of the first matrix:\
n"); getMatrix(matrix1, rows1, cols1);
printf("Enter elements of the second
matrix:\n"); getMatrix(matrix2, rows2,
cols2);
printf("First Matrix:\n");
printMatrix(matrix1, rows1, cols1);
printf("Second Matrix:\n");
printMatrix(matrix2, rows2, cols2);
if (rows1 == rows2 && cols1 == cols2) {
addMatrices(matrix1, matrix2, result, rows1,
cols1); printf("Matrix after addition:\n");
printMatrix(result, rows1,
cols1);
} else {
printf("Matrix addition not possible due to incompatible
dimensions!\n");
}
multiplyMatrices(matrix1, matrix2, result, rows1, cols1, rows2,
cols2); printf("Matrix after multiplication:\n");
if (cols1 == rows2) {
printMatrix(result, rows1,
cols2);
}
return 0;
}
Output:
Enter rows and columns for first matrix: 2 2
Enter rows and columns for second
matrix: 2 2 Enter elements of the first
matrix:
Enter elements of the
matrix: Element [0][0]: 1
Element [0][1]:
Element [1][0]:
Element [1][1]:
4
Enter elements of the second
matrix: Enter elements of the
matrix:
Element [0][0]: 5
Element [0][1]:
6
Element [1][0]:
7
Element [1][1]:
8
First Matrix:
Matrix:
12
34
Second
Matrix:
Matrix:
56
78
Matrix after
addition:
68
10 12
Matrix after
multiplication:
Matrix:
19 22
43 50
Program-9
Write a program for concatenation of two strings.
#include <stdio.h>
#include
<string.h> int
main() {
char firstString[100], secondString[100],
concatenatedString[200]; // Input the first string
printf("Enter the first string: ");
fgets(firstString, sizeof(firstString), stdin);
firstString[strcspn(firstString, "\n")] = '\0'; // Remove newline character
from input // Input the second string
printf("Enter the second string: ");
fgets(secondString, sizeof(secondString), stdin);
secondString[strcspn(secondString, "\n")] = '\0'; // Remove newline
character from
input
// Concatenate the strings
strcpy(concatenatedString, firstString);
strcat(concatenatedString,
secondString); // Display the
concatenated string
printf("Concatenated String: %s\n", concatenatedString);
return 0;
}
Output:
Enter the first string: hello
Enter the second string:
world
Concatenated String:
helloworld
Program-10
Write a program for length of a string with and without string handling
functions
#include
<stdio.h>
#include
<string.h>
// Function to calculate string length without using string handling
int calculateStringLengthWithoutFunctions(const
char* str)
{
int length = 0;
while (str[length] != '\
0')
{
length++;
} return
length;
int main() {
char
inputString[100];
// Input the string
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
inputString[strcspn(inputString, "\n")] = '\0'; // Remove newline
character from
input
// Calculate string length using string handling function
strlen int lengthWithFunction = strlen(inputString);
// Calculate string length without using string handling functions
int lengthWithoutFunction =
calculateStringLengthWithoutFunctions(inputString); // Display the
results
printf("\nLength of the string using string handling
functions: %d\n",lengthWithFunction);
printf("Length of the string without using string handling function
%d\n", lengthWithoutFunction);
return 0;
}
Output:
Enter a string: Programming
Length of the string using string handling functions: 11
Length of the string without using string handling function 11
Program-11
Write a program to demonstrate Call by Value and Call by Reference
mechanism.
#include <stdio.h>
// Function to demonstrate Call by Value
void callByValue(int x) {
printf("Inside callByValue, before change: x = %d\n", x); x
= 100; // Change the value of x
printf("Inside callByValue, after change: x = %d\n", x);
// Function to demonstrate Call by Reference
void callByReference(int *x) {
printf("Inside callByReference, before change: *x = %d\n", *x);
*x = 100; // Change the value of x
printf("Inside callByReference, after change: *x = %d\n", *x);
int main() {
int a = 10, b = 20;
printf("Before callByValue, a = %d\n", a);
callByValue(a); // Call by Value
printf("After callByValue, a = %d\n\n", a);
printf("Before callByReference, b = %d\n", b);
callByReference(&b); // Call by Reference
printf("After callByReference, b = %d\n", b);
return 0;
}
Output:
Before callByValue, a = 10
Inside callByValue, before change: x
= 10 Inside callByValue, after change:
x = 100 After callByValue, a = 10
Before callByReference, b = 20
Inside callByReference, before change: *x
= 20 Inside callByReference, after change:
*x = 100 After callByReference, b = 100
Program-12
Write a program to find GCD of two numbers using Recursion
#include <stdio.h>
// Function to find GCD using
recursion int gcd(int a, int b) {
// Base case: if the second number is 0, return the first
number if (b == 0) {
return a;
}
// Recursive case: call gcd with b and the remainder of a
divided by b return gcd(b, a % b);
int main() {
int num1, num2;
// Input two numbers from the
user
printf("Enter two integers: ");
scanf("%d %d", &num1,
&num2);
// Find and print the GCD
printf("The GCD of %d and %d is %d\n", num1, num2, gcd(num1,
num2));
return 0;
}
Output:
Enter two integers: 48
18
The GCD of 48 and 18 is
6
Program-13
Write a C program to perform various operations using pointers
#include <stdio.h>
void swap(int *x, int *y) {
// Swap the values pointed to by x and y using
pointers int temp = *x;
*x = *y;
*y = temp;
void findLargest(int arr[], int n) {
// Find the largest element in the array using a pointer
int *largest = &arr[0]; // Initialize pointer to first
element for (int i = 1; i < n; i++) {
if (*largest < arr[i]) {
largest = &arr[i]; // Update pointer to largest
element
}
}
printf("Largest element in the array: %d\n",
*largest);
}
void reverseArray(int arr[], int n) {
// Reverse the elements of the array using
pointers int *start = arr;
int *end = arr + n -
1;
while (start < end) {
swap(start, end);
start++;
end--;
}
}
int main() {
int arr[] = {5, 2, 8, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Function calls to demonstrate pointer operations
swap(&arr[0], &arr[2]); // Swap first and third
elements findLargest(arr, n);
reverseArray(arr, n);
printf("Modified array:
");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Original array: 5 2 8 1 3
Largest element in the
array: 8 Modified array: 3 1
528
Program-14
Write a c program to read data of 10 employees with a structure of
1.employeeid, 2.aadhar_no, 3.title, 4.joined date, 5.salary, 6.date of birth,
7.gender, 8.department
#include <stdio.h>
#define NUM_EMPLOYEES 10
// Define a structure to store employee
details struct Employee {
int employeeId;
long long
aadhar_no; char
title[50];
char joinedDate[11]; // Format: YYYY-MM-
DD float salary;
char dateOfBirth[11]; // Format: YYYY-MM-
DD char gender[10];
char
department[50];
};
void readEmployeeData(struct Employee
*emp) {
printf("Enter Employee ID: ");
scanf("%d", &emp->employeeId);
printf("Enter Aadhar No: ");
scanf("%lld", &emp->aadhar_no);
printf("Enter Title: ");
scanf(" %[^\n]%*c", emp->title);
printf("Enter Joined Date (YYYY-MM-DD):
"); scanf("%s", emp->joinedDate);
printf("Enter Salary: ");
scanf("%f", &emp->salary);
printf("Enter Date of Birth (YYYY-MM-DD):
"); scanf("%s", emp->dateOfBirth);
printf("Enter Gender: ");
scanf("%s", emp->gender);
printf("Enter Department: ");
scanf(" %[^\n]%*c", emp-
>department);
}
void printEmployeeData(struct Employee emp) {
printf("\nEmployee ID: %d\n",
emp.employeeId); printf("Aadhar No: %lld\
n", emp.aadhar_no); printf("Title: %s\n",
emp.title);
printf("Joined Date: %s\n",
emp.joinedDate); printf("Salary: %.2f\n",
emp.salary);
printf("Date of Birth: %s\n",
emp.dateOfBirth); printf("Gender: %s\n",
emp.gender);
printf("Department: %s\n",
emp.department);
}
int main() {
struct Employee employees[NUM_EMPLOYEES];
// Read data for all employees
for (int i = 0; i < NUM_EMPLOYEES; i++) {
printf("\nEnter details for Employee %d:\n", i
+ 1); readEmployeeData(&employees[i]);
}
// Print data for all employees
printf("\nEmployee Details:\
n");
for (int i = 0; i < NUM_EMPLOYEES; i+
+) {
printEmployeeData(employees[i]);
}
return 0;
}
Output:
Enter details for Employee
1: Enter Employee ID: 101
Enter Aadhar No:
123456789012 Enter Title:
Manager
Enter Joined Date (YYYY-MM-DD): 2024-
07-01 Enter Salary: 50000
Enter Date of Birth (YYYY-MM-DD): 1990-
05-15 Enter Gender: male
Enter Department: IT
Enter details for Employee 2:
Enter Employee ID: 102
Enter Aadhar No:
234567890123 Enter Title:
Analyst
Enter Joined Date (YYYY-MM-DD): 2024-
07-01 Enter Salary: 40000
Enter Date of Birth (YYYY-MM-DD): 1988-
07-20 Enter Gender: female
Enter Department: Finance
Enter details for Employee 3:
Enter Employee ID: 103
Enter Aadhar No:
45678912301
Enter Title: hrm
Enter Joined Date (YYYY-MM-DD): 2022-
01-01 Enter Salary: 50000
Enter Date of Birth (YYYY-MM-DD): 2000-
07-15 Enter Gender: Male
Enter Department: operations
Enter details for Employee
4:
Enter Employee ID: 104
Enter Aadhar No:
7894561239
Enter Title: HRM
Enter Joined Date (YYYY-MM-DD): 2024-
01-01 Enter Salary: 60000
Enter Date of Birth (YYYY-MM-DD): 1990-
05-01 Enter Gender: Male
Enter Department: Operations
Program-15
Write a c program to demonstrate dynamic arrays using dynamic memory
management functions
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int size = 0;
int capacity =
2;
int input;
char choice;
// Allocate initial memory for the array
array = (int *)malloc(capacity *
sizeof(int)); if (array == NULL) {
fprintf(stderr, "Memory allocation failed\
n");
return 1;
}
printf("Dynamic Array Demonstration\n");
// Input loop
do {
printf("Enter an integer:
"); scanf("%d", &input);
// Check if we need to resize the
array if (size == capacity) {
capacity *= 2; // Double the capacity
int *temp = (int *)realloc(array, capacity *
sizeof(int)); if (temp == NULL) {
fprintf(stderr, "Memory reallocation failed\n");
free(array); // Free the originally allocated
memory
return 1;
}
array =
temp;
}
// Store the input value in the
array array[size++] = input;
// Ask if the user wants to continue
printf("Do you want to enter another number?
(y/n): "); scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
// Print the contents of the
array
printf("\nArray contents:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
// Free the allocated
memory free(array);
return 0;
}
Output:
Dynamic Array
Demonstration Enter an
integer: 5
Do you want to enter another number?
(y/n): y Enter an integer: 10
Do you want to enter another number?
(y/n): y Enter an integer: 20
Do you want to enter another number?
(y/n): y Enter an integer: 30
Do you want to enter another number?
(y/n): y Enter an integer: 40
Do you want to enter another number?
(y/n): y Enter an integer: 50
Do you want to enter another number? (y/n): n
Array contents:
5 10 20 30 40 50