0% found this document useful (0 votes)
34 views26 pages

CLPP Lab

Uploaded by

akshai020kumar
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)
34 views26 pages

CLPP Lab

Uploaded by

akshai020kumar
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/ 26

Er.

PERUMAL MANIMEKALAI
POLYTECHNIC COLLEGE
Koneripalli, Hosur – 635 117

DEPARTMENT OF
ELECTRONICS (ROBOTICS) ENGINEERING

LAB MANUAL
NAME OF THE SUBJECT : C Language Programming Practical

YEAR / SEM : III / V

COURSE CODE : 4047564

PREPARED BY : J Stuart Kirubhakarapandian M.E.

Lecturer
List of experiments

1. Program to calculate simple and compound interest


2. Program to find the gross salary and net salary of an employee having basic
pay, hra, da, ma and income tax. Assume da is 20% of basic pay and income tax
is 10% of gross salary.
3. Program to find whether the given number is a positive number, negative
number or zero.
4. Program to find the largest and smallest among the three given numbers.
5. Program to read a numeric month and display it in words.
6. Programs to find the sum of series using while, do…while and for loop
statements.
7. Programs to find the factorial of a given number using while, do…while and for
loop statements.
8. Program to print multiplication table.
9. Program to find the sum of digits of a given number
10.Program to implement Fibonacci series.
11.Program to sort a list of numbers
12.Program to sort the strings.
13.Program to add two matrices.
14.Write a function to find the factorial of a value. Write a program to find the
nCr value using the above function.
Program to calculate simple and compound interest
Ex No 1
Date:
Aim:
To write and execute a C program to calculate simple and compound interest.
Program:
#include <stdio.h>
#include <math.h>
float calculateSimpleInterest(float principal, float rate, float time) {
float simpleInterest;
simpleInterest = (principal * rate * time) / 100.0;
return simpleInterest;
}
float calculateCompoundInterest(float principal, float rate, float time) {
float compoundInterest;
compoundInterest = principal * (pow((1 + rate / 100.0), time)) - principal;
return compoundInterest;
}
int main() {
float principal, rate, time, simpleInterest, compoundInterest;

// Input principal amount, rate of interest, and time period


printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest (per annum): ");
scanf("%f", &rate);
printf("Enter time period (in years): ");
scanf("%f", &time);

simpleInterest = calculateSimpleInterest(principal, rate, time);


compoundInterest = calculateCompoundInterest(principal, rate, time);

printf("\nSimple Interest: %.2f\n", simpleInterest);


printf("Compound Interest: %.2f\n", compoundInterest);

return 0;
}
Output:

Result:
Thus the program is executed and the output is verified successfully.
Program to find the gross salary and net salary of an employee having basic pay,
hra, da, ma and income tax. Assume da is 20% of basic pay and income tax is 10%
of gross salary.
Ex No 2
Date:
Aim:

To write and execute a C program to find the gross salary and net salary of an employee having basic
pay, hra, da, ma and income tax. Assume da is 20% of basic pay and income tax is 10% of gross salary.

Program:
#include <stdio.h>
float calculateGrossSalary(float basicPay, float hra, float da, float ma) {
float grossSalary;
da = basicPay * 0.2; // DA is 20% of basic pay
grossSalary = basicPay + hra + da + ma;
return grossSalary;
}
float calculateNetSalary(float grossSalary) {
float incomeTax, netSalary;
incomeTax = grossSalary * 0.1; // Income tax is 10% of gross salary
netSalary = grossSalary - incomeTax;
return netSalary;
}
int main() {
float basicPay, hra, ma, da, grossSalary, netSalary;
printf("Enter Basic Pay: ");
scanf("%f", &basicPay);
printf("Enter HRA: ");
scanf("%f", &hra);
printf("Enter Medical Allowance: ");
scanf("%f", &ma);
da = basicPay * 0.2; // DA is 20% of basic pay
grossSalary = calculateGrossSalary(basicPay, hra, da, ma);
netSalary = calculateNetSalary(grossSalary);
printf("\nGross Salary: %.2f\n", grossSalary);
printf("Net Salary: %.2f\n", netSalary);

return 0;
}
Output:

Result:
Thus the program is executed and the output is verified successfully.
Program to find whether the given number is a positive number, negative number
or zero.
Ex No 3
Date:
Aim:
To write and execute a C Program to find whether the given number is a positive number, negative
number or zero.
Program:
#include <stdio.h>
int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number < 0) {
printf("%d is a negative number.\n", number);
} else {
printf("The number is zero.\n");
}

return 0;
}

Output:
Enter a number: 5
5 is a positive number.

Enter a number: -3
-3 is a negative number.

Enter a number: 0
The number is zero.

Result:
Thus the program is executed and the output is verified successfully.
Program to find the largest and smallest among the three given numbers.
Ex No 4
Date:
Aim:
To write and execute a C program to find the largest and smallest among the three given numbers.
Program:
#include <stdio.h>

int main() {
int num1, num2, num3;
int largest, smallest;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

largest = num1;
smallest = num1;

if (num2 > largest) {


largest = num2;
}
if (num3 > largest) {
largest = num3;
}

if (num2 < smallest) {


smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}

printf("Largest number: %d\n", largest);


printf("Smallest number: %d\n", smallest);

return 0;
}
Output:
Enter three numbers: 5 12 3
Largest number: 12
Smallest number: 3

Enter three numbers: -8 0 -2


Largest number: 0
Smallest number: -8

Enter three numbers: 1 1 1


Largest number: 1
Smallest number: 1

Result:
Thus the program is executed and the output is verified successfully.
Program to read a numeric month and display it in words.
Ex No 5
Date:
Aim:
To write and execute a C Program to read a numeric month and display it in words.
Program:
#include <stdio.h>
int main() {
int month;
printf("Enter a month number (1-12): ");
scanf("%d", &month);
switch(month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number! Please enter a number between 1 and 12.\n");
}

return 0;
}

Output:
Enter a month number (1-12): 3
March

Enter a month number (1-12): 9


September

Enter a month number (1-12): 13


Invalid month number! Please enter a number between 1 and 12.

Result:
Thus the program is executed and the output is verified successfully.
Programs to find the sum of series using while, do…while and for loop statements
Ex No 6
Date:
Aim:
To write and execute a C Programs to find the sum of series using while, do…while and for loop statements
Program for while loop:

#include <stdio.h>
int main() {
int n, i = 1, sum = 0;

printf("Enter the number of terms: ");


scanf("%d", &n);

while (i <= n) {
sum += i;
i++;
}
printf("Sum of first %d natural numbers = %d\n", n, sum);

return 0;
}
Program for do…while loop:

#include <stdio.h>
int main() {
int n, i = 1, sum = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
do {
sum += i;
i++;
} while (i <= n);
printf("Sum of first %d natural numbers = %d\n", n, sum);
return 0;
}
Program for for loop:
#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter the number of terms: ");


scanf("%d", &n);

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


sum += i;
}

printf("Sum of first %d natural numbers = %d\n", n, sum);

return 0;
}

Output:
Enter the number of terms: 5
Sum of first 5 natural numbers = 15

Enter the number of terms: 10


Sum of first 10 natural numbers = 55

Result:
Thus the program is executed and the output is verified successfully.
Programs to find the factorial of a given number using while, do…while and for
loop statements.
Ex No 7
Date:
Aim:
To write and execute a C Programs to find the factorial of a given number using while, do…while and
for loop statements.
Program for while loop:
#include <stdio.h>
int main() {
int n, factorial = 1;
int i = 1;

printf("Enter a number: ");


scanf("%d", &n);

while (i <= n) {
factorial *= i;
i++;
}

printf("Factorial of %d = %d\n", n, factorial);

return 0;
}
Program for do…..while loop:
#include <stdio.h>
int main() {
int n, factorial = 1;
int i = 1;

printf("Enter a number: ");


scanf("%d", &n);

do {
factorial *= i;
i++;
} while (i <= n);

printf("Factorial of %d = %d\n", n, factorial);

return 0;
}
Program for for loop:
#include <stdio.h>

int main() {
int n, factorial = 1;

printf("Enter a number: ");


scanf("%d", &n);
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %d\n", n, factorial);

return 0;
}

Output:

Enter a number: 5
Factorial of 5 = 120

Enter a number: 10
Factorial of 10 = 3628800

Result:
Thus the program is executed and the output is verified successfully.
Program to print multiplication table.
Ex No 8
Date:
Aim:
To write and execute a C Program to print multiplication table.
Program:
#include <stdio.h>

int main() {
int number, range;

printf("Enter the number for multiplication table: ");


scanf("%d", &number);

printf("Enter the range (up to which multiple): ");


scanf("%d", &range);

printf("Multiplication Table of %d up to %d:\n", number, range);


for (int i = 1; i <= range; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
Output:
Enter the number for multiplication table: 5 Enter the number for multiplication table: 3
Enter the range (up to which multiple): 10 Enter the range (up to which multiple): 12
Multiplication Table of 5 up to 10: Multiplication Table of 3 up to 12:
5x1=5 3x1=3
5 x 2 = 10 3x2=6
5 x 3 = 15 3x3=9
5 x 4 = 20 3 x 4 = 12
5 x 5 = 25 3 x 5 = 15
5 x 6 = 30 3 x 6 = 18
5 x 7 = 35 3 x 7 = 21
5 x 8 = 40 3 x 8 = 24
5 x 9 = 45 3 x 9 = 27
5 x 10 = 50 3 x 10 = 30
3 x 11 = 33
3 x 12 = 36

Result:
Thus the program is executed and the output is verified successfully.
Program to find the sum of digits of a given number
Ex No 9
Date:
Aim:
To write and execute a C program to find the sum of digits of a given number
Program:
#include <stdio.h>
int main() {
int number, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &number);
while (number != 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}

Output:

Enter a number: 12345


Sum of digits = 15

Enter a number: 9876


Sum of digits = 30

Result:
Thus the program is executed and the output is verified successfully.
Program to implement Fibonacci series.
Ex No 10
Date:
Aim:
To write and execute a C Program to implement Fibonacci series.
Program:
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;

printf("Enter the number of terms for Fibonacci series: ");


scanf("%d", &n);

printf("Fibonacci Series:\n");

printf("%d, %d", first, second);

for (int i = 3; i <= n; i++) {


next = first + second;
printf(", %d", next);
first = second;
second = next;
}

printf("\n");

return 0;
}

Output:
Enter the number of terms for Fibonacci series: 10
Fibonacci Series:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Enter the number of terms for Fibonacci series: 5


Fibonacci Series:
0, 1, 1, 2, 3

Result:
Thus the program is executed and the output is verified successfully.
Program to sort a list of numbers
Ex No 11
Date:
Aim:
To write and execute a C Program to sort a list of numbers.
Program:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
// Last i elements are already in place
for (j = 0; j < n-i-1; j++) {
// Swap if the current element is greater than the next
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main() {
int arr[100], n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

bubbleSort(arr, n);

printf("\nSorted array in ascending order:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Output:
Enter number of elements: 5
Enter 5 elements:
23
14
56
3
8

Sorted array in ascending order:


3 8 14 23 56

Enter number of elements: 8


Enter 8 elements:
67
12
45
9
32
6
81
15

Sorted array in ascending order:


6 9 12 15 32 45 67 81

Result:
Thus the program is executed and the output is verified successfully.
Program to sort the strings.
Ex No 12
Date:
Aim:
To write and execute a C Program to sort the strings.
Program:
#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100


#define MAX_STRINGS 10

void bubbleSort(char arr[][MAX_LENGTH], int n) {


int i, j;
char temp[MAX_LENGTH];

for (i = 0; i < n-1; i++) {


for (j = 0; j < n-i-1; j++) {
if (strcmp(arr[j], arr[j+1]) > 0) {
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
}
}
}
}

int main() {
char strings[MAX_STRINGS][MAX_LENGTH];
int n, i;
printf("Enter number of strings: ");
scanf("%d", &n);
printf("Enter %d strings:\n", n);
for (i = 0; i < n; i++) {
scanf("%s", strings[i]);
}
bubbleSort(strings, n);
printf("\nSorted strings in lexicographical order:\n");
for (i = 0; i < n; i++) {
printf("%s\n", strings[i]);
}

return 0;
}
Output:
Enter number of strings: 5
Enter 5 strings:
apple
orange
banana
grape
pear

Sorted strings in lexicographical order:


apple
banana
grape
orange
pear

Enter number of strings: 3


Enter 3 strings:
hello
world
programming

Sorted strings in lexicographical order:


hello
programming
world

Result:
Thus the program is executed and the output is verified successfully.
Program to add two matrices.
Ex No 13
Date:
Aim:
To write and execute a C Program to add two matrices.
Program:
#include <stdio.h>

#define MAX_ROWS 100


#define MAX_COLS 100

void addMatrices(int mat1[MAX_ROWS][MAX_COLS], int mat2[MAX_ROWS][MAX_COLS], int


result[MAX_ROWS][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

void displayMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS][MAX_COLS], result[MAX_ROWS][MAX_COLS];
int rows, cols;
printf("Enter number of rows and columns of matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
}
}
addMatrices(mat1, mat2, result, rows, cols);
printf("\nFirst Matrix:\n");
displayMatrix(mat1, rows, cols);

printf("\nSecond Matrix:\n");
displayMatrix(mat2, rows, cols);

printf("\nSum of the Matrices:\n");


displayMatrix(result, rows, cols);

return 0;
}

Output:
Enter number of rows and columns of matrices: 2 3
Enter elements of first matrix:
123
456
Enter elements of second matrix:
789
10 11 12

First Matrix:
1 2 3
4 5 6

Second Matrix:
7 8 9
10 11 12

Sum of the Matrices:


8 10 12
14 16 18

Result:
Thus the program is executed and the output is verified successfully.
Write a function to find the factorial of a value. Write a program to
find the nCr value using the above function.
Ex No 14
Date:
Aim:
To write a function to find the factorial of a value. Write a program to find the nCr value using the above
function.
Program: 1
#include <stdio.h>

unsigned long long factorial(int n) {


unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}

int main() {
int num;
printf("Enter a number to find its factorial: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
unsigned long long fact = factorial(num);
printf("Factorial of %d is: %llu\n", num, fact);
}

return 0;
}

Program: 2
#include <stdio.h>

unsigned long long factorial(int n) {


unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}

unsigned long long nCr(int n, int r) {


if (r > n || n < 0 || r < 0) {
return 0; // Invalid inputs, return 0
}
unsigned long long numerator = factorial(n);
unsigned long long denominator = factorial(r) * factorial(n - r);
return numerator / denominator;
}

int main() {
int n, r;
printf("Enter the value of n and r to find nCr: ");
scanf("%d %d", &n, &r);

if (n < 0 || r < 0 || r > n) {


printf("Invalid input. Please enter valid values (n >= 0, r >= 0, r <= n).\n");
} else {
unsigned long long result = nCr(n, r);
printf("%dC%d = %llu\n", n, r, result);
}

return 0;
}

Output:

Result:
Thus the program is executed and the output is verified successfully.

You might also like