Practical File Template
Practical File Template
OF
[CSE124]
Page 1 of 33
INDEX
S.NO PROGRAM PG.NO. DATE SIGN REM
1. Write a program that performs as a calculator 4-5
( addition, multiplication, division,
subtraction).
2. Write a program to find area of 5-6
triangle(a=h*b*.5) a = area h = height b = base
3. Write a program to calculate simple interest (i 6-7
= (p*r*n)/100 )i = Simple interest p = Principal
amount r = Rate of interest n = Number of
years
4. Write a C program to interchange two 7
numbers.
5. Write a C program to enter a distance to 7-8
kilometre and convert it into meter, feet, inches
and centimetres.
6. Write a program to compute Fahrenheit from 8-9
centigrade (f=1.8*c +32)
7. Write a C program to find that the accepted 9
number is Negative, or Positive or Zero.
8. Write a program to read marks of a student 9 - 10
from the keyboard whether the student is pass
or fails ( using if-else)
9. Write a program to read three numbers from 10 -11
the keyboard and find out the maximum out of
these three. (nested if-else)
10. Write a C program to check whether the 11 - 12
entered character is a capital, small letter, digit
or any special character.
11. Write a C program to find out the Maximum 12 -13
and Minimum numbers from the given 10
numbers
12. Write a program to reverse a number. 13 - 14
13. Write a program to find out the sum of the first 14
and last digits of a given number.
14. Write a program to calculate the average and 14 - 16
total of 5 students for 3 subjects (use nested for
loops)
15. Write a C program to read and store the roll no 16 - 17
and marks of 20 students using an array.\
16. Write a program to find the maximum element 17 - 18
from the 1-Dimensional array.
17. Write a C program to calculate the average, 18 - 19
geometric and harmonic mean of n elements in
an array.
18. Write a program to sort a given array in 19 - 20
ascending order (Use Insertion sort, Bubble
sort, Selection sort, Merge sort, Quicksort,
Heapsort).
19. Write a program to find a character from a 21
given string.
20. Write a program to replace a character in 21 - 22
the given string.
Page 2 of 33
21. Write a program to reverse the string. 22 - 23
22. Write a program to convert the string into 23 - 24
upper case
23. Write a functioning Exchange to interchange 24 - 25
the values of two variables, say x and y.
illustrate the use of this function in a calling
function.
24. Write a program to find the factorial of a 25
number using recursion.
Page 3 of 33
Program 1
Page 5 of 33
Program 2
Que.2 Write a program to find area of triangle(a=h*b*.5) a = area h = height b =
base.
Ans.
#include<stdio.h>
int main() {
float base, height, area;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("The area of the triangle is: %.2f\n", area);
return 0;
}
Output-
Page 6 of 33
Program 3
Que.3 Write a program to calculate simple interest (i = (p*r*n)/100 )i = Simple
interest p = Principal amount r = Rate of interest n = Number of years.
Ans.
#include<stdio.h>
int main() {
float p, r, n, i;
printf("Enter Principal Amount: ");
scanf("%f", &p);
printf("Enter Rate of Interest: ");
scanf("%f", &r);
printf("Enter Number of Years: ");
scanf("%f", &n);
i = (p * r * n) / 100;
printf("Simple Interest: %.2f\n", i);
return 0;
}
Output-
Page 7 of 33
Program 4
Que. 4 Write a C program to interchange two numbers.
Ans.
#include<stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output-
Page 8 of 33
Program 5
Que. 5 Write a C program to enter a distance to kilometre and convert it into
meter, feet, inches and centimetres.
Ans.
#include<stdio.h>
int main() {
float km, meters, feet, inches, cm;
printf("Enter distance in kilometers: ");
scanf("%f", &km);
meters = km * 1000;
cm = meters * 100;
inches = meters * 39.37;
feet = meters * 3.281;
printf("Meters: %.2f\nFeet: %.2f\nInches: %.2f\nCentimeters: %.2f\n",
meters, feet, inches, cm);
return 0;
}
Output-
Page 9 of 33
Program 6
Que.6 Write a program to compute Fahrenheit from centigrade (f=1.8*c +32).
Ans.
#include<stdio.h>
int main() {
float c, f;
printf("Enter temperature in Centigrade: ");
scanf("%f", &c);
f = 1.8 * c + 32;
printf("Temperature in Fahrenheit: %.2f\n", f);
return 0;
}
Output-
Page 10 of 33
Program 7
Que.7 Write a C program to find that the accepted number is Negative, or
Positive or Zero.
Ans.
#include<stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
Output-
Page 11 of 33
Program 8
Que.8 Write a program to read marks of a student from the keyboard whether
the student is pass or fails ( using if-else).
Ans.
#include<stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 40) {
printf("Pass\n");
} else {
printf("Fail\n");
}
return 0;
}
Output-
Page 12 of 33
Program 9
Que.9 Write a program to read three numbers from the keyboard and find out
the maximum out of these three. (nested if-else).
Ans.
#include<stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c) {
printf("Maximum: %d\n", a);
} else {
printf("Maximum: %d\n", c);
}
} else {
if (b > c) {
printf("Maximum: %d\n", b);
} else {
printf("Maximum: %d\n", c);
}
}
return 0;
}
Output-
Page 13 of 33
Program 10
Que.10 Write a C program to check whether the entered character is a capital,
small letter, digit or any special character.
Ans.
#include<stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') {
printf("The entered character is a Capital Letter.\n");
} else if (ch >= 'a' && ch <= 'z') {
printf("The entered character is a Small Letter.\n");
} else if (ch >= '0' && ch <= '9') {
printf("The entered character is a Digit.\n");
} else {
printf("The entered character is a Special Character.\n");
}
return 0;
}
Output-
Page 14 of 33
Program 11
Que.11 Write a C program to find out the Maximum and Minimum numbers
from the given 10 numbers.
Ans.
#include<stdio.h>
int main() {
int numbers[10], i, max, min;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
max = min = numbers[0];
for (i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
printf("Maximum number: %d\n", max);
printf("Minimum number: %d\n", min);
return 0;
}
Output-
Page 15 of 33
Page 16 of 33
Program 12
Que.12 Write a program to reverse a number.
Ans.
#include<stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n", reverse);
return 0;
}
Output-
Page 17 of 33
Program 13
Que.13 Write a program to find out the sum of the first and last digits of a given
number.
Ans.
#include<stdio.h>
int main() {
int num, firstDigit, lastDigit;
printf("Enter a number: ");
scanf("%d", &num);
lastDigit = num % 10;
while (num >= 10) {
num /= 10;
}
firstDigit = num;
printf("Sum of first and last digits: %d\n", firstDigit + lastDigit);
return 0;
}
Output-
Page 18 of 33
Program 14
Que.14 Write a program to calculate the average and total of 5 students for 3
subjects (use nested for loops).
Ans.
#include<stdio.h>
int main() {
int marks[5][3], total[5], i, j;
float average[5];
for (i = 0; i < 5; i++) {
printf("Enter marks of 3 subjects for student %d:\n", i + 1);
for (j = 0; j < 3; j++) {
scanf("%d", &marks[i][j]);
}
}
for (i = 0; i < 5; i++) {
total[i] = 0;
for (j = 0; j < 3; j++) {
total[i] += marks[i][j];
}
average[i] = total[i] / 3.0;
}
for (i = 0; i < 5; i++) {
printf("Student %d - Total: %d, Average: %.2f\n", i + 1, total[i],
average[i]);
}
return 0;
}
Output-
Page 19 of 33
Page 20 of 33
Program 15
Que.15 Write a C program to read and store the roll no and marks of 20
students using an array.
Ans.
#include<stdio.h>
int main() {
int roll_no[20], marks[20], i;
for (i = 0; i < 20; i++) {
printf("Enter roll number and marks for student %d: ", i + 1);
scanf("%d %d", &roll_no[i], &marks[i]);
}
printf("\nRoll Number\tMarks\n");
for (i = 0; i < 20; i++) {
printf("%d\t\t%d\n", roll_no[i], marks[i]);
}
return 0;
}
Output-
Page 21 of 33
Program 16
Que.16 Write a program to find the maximum element from the 1-Dimensional
array.
Ans.
#include<stdio.h>
int main() {
int n, i, max;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The maximum element in the array is: %d\n", max);
return 0;
}
Output-
Page 22 of 33
Program 17
Que.17 Write a C program to calculate the average, geometric and harmonic
mean of n elements in an array
Ans.
#include<stdio.h>
#include<math.h>
int main() {
int n, i;
float sum = 0, product = 1, harmonic_sum = 0, average, geometric_mean,
harmonic_mean;
printf("Enter the number of elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%f", &arr[i]);
sum += arr[i];
product *= arr[i];
harmonic_sum += 1 / arr[i];
}
average = sum / n;
geometric_mean = pow(product, 1.0 / n);
harmonic_mean = n / harmonic_sum;
printf("Average: %.2f\n", average);
printf("Geometric Mean: %.2f\n", geometric_mean);
printf("Harmonic Mean: %.2f\n", harmonic_mean);
return 0;
}
Output-
Page 23 of 33
Page 24 of 33
Program 18
Que.18 Write a program to sort a given array in ascending order (Use Insertion
sort, Bubble sort, Selection sort, Merge sort, Quicksort, Heapsort).
Ans.
#include<stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Page 25 of 33
Ouptut-
Page 26 of 33
Program 19
Que.19 Write a program to find a character from a given string.
Ans.
#include<stdio.h>
#include<string.h>
int main() {
char str[100], ch;
int i, found = 0;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter the character to search: ");
scanf(" %c", &ch); // Space before %c to handle newline
for (i = 0; i < strlen(str); i++) {
if (str[i] == ch) {
found = 1;
printf("Character '%c' found at position %d\n", ch, i + 1);
break;
}
}
if (!found) {
printf("Character '%c' not found in the string.\n", ch);
}
return 0;
}
Output-
Page 27 of 33
Program 20
Que.20 Write a program to replace a character in the given string.
Ans.
#include<stdio.h>
#include<string.h>
int main() {
char str[100], old_char, new_char;
int i;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter the character to replace: ");
scanf(" %c", &old_char);
printf("Enter the new character: ");
scanf(" %c", &new_char);
for (i = 0; i < strlen(str); i++) {
if (str[i] == old_char) {
str[i] = new_char;
}
}
printf("Modified string: %s\n", str);
return 0;
}
Output-
Page 28 of 33
Program 21
Que.21 Write a program to reverse the string.
Ans.
#include<stdio.h>
#include<string.h>
int main() {
char str[100], rev[100];
int i, len;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0';
printf("Reversed string: %s\n", rev);
return 0;
}
Output-
Page 29 of 33
Program 22
Que.22 Write a program to convert the string into upper case.
Ans.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
scanf("%s", str);
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("String in uppercase: %s\n", str);
return 0;
}
Output-
Page 30 of 33
Program 23
Que.23 Write a functioning Exchange to interchange the values of two variables,
say x and y. illustrate the use of this function in a calling function.
Ans.
#include<stdio.h>
void interchange(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
interchange(&a, &b);
printf("After interchanging:\n");
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output-
Page 31 of 33
Program 24
Que.24 Write a program to find the factorial of a number using recursion.
Ans.
#include<stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output-
Page 32 of 33
Program 25
Que.25 Write a C program using a global variable and static variable.
Ans.
#include<stdio.h>
int global_count = 0;
void increment() {
static int static_count = 0;
global_count++;
static_count++;
printf("Global count: %d, Static count: %d\n", global_count, static_count);
}
int main() {
increment();
increment();
increment();
return 0;
Output-
Page 33 of 33