CLPP Lab
CLPP Lab
PERUMAL MANIMEKALAI
POLYTECHNIC COLLEGE
Koneripalli, Hosur – 635 117
DEPARTMENT OF
ELECTRONICS (ROBOTICS) ENGINEERING
LAB MANUAL
NAME OF THE SUBJECT : C Language Programming Practical
Lecturer
List of experiments
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;
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;
return 0;
}
Output:
Enter three numbers: 5 12 3
Largest number: 12
Smallest number: 3
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
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;
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;
return 0;
}
Output:
Enter the number of terms: 5
Sum of first 5 natural numbers = 15
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;
while (i <= n) {
factorial *= i;
i++;
}
return 0;
}
Program for do…..while loop:
#include <stdio.h>
int main() {
int n, factorial = 1;
int i = 1;
do {
factorial *= i;
i++;
} while (i <= n);
return 0;
}
Program for for loop:
#include <stdio.h>
int main() {
int n, factorial = 1;
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;
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:
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("Fibonacci Series:\n");
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
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;
bubbleSort(arr, n);
return 0;
}
Output:
Enter number of elements: 5
Enter 5 elements:
23
14
56
3
8
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>
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
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>
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);
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
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>
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>
int main() {
int n, r;
printf("Enter the value of n and r to find nCr: ");
scanf("%d %d", &n, &r);
return 0;
}
Output:
Result:
Thus the program is executed and the output is verified successfully.