C Programming Lab Manual: Installation & Basics
C Programming Lab Manual: Installation & Basics
WEEK 1
1. Download Turbo C
o Get the Turbo C setup file (commonly Turbo C++ 3.2 or Turbo C++ 4.0)
from a trusted source.
2. Run the Setup File
o Double-click the downloaded file to start the installation.
3. Choose Installation Directory
o By default, it will install in C:\TC or C:\TurboC3.
o You can change the folder if needed, but keeping the default is
recommended.
4. Start Installation
o Click on Install or Next to begin copying files.
o The setup will extract and copy Turbo C files to the chosen folder.
5. Installation Complete
o Once the process finishes, a confirmation message appears.
6. Open Turbo C IDE
o Run the shortcut created on the desktop or go to the installed directory and
open [Link].
7. Configuration (if required)
o Set the drive and path if asked. Usually, it auto-detects.
1. Download Dev-C++
o Go to a trusted site like Source Forge or the official Bloodshed Dev-C++ /
Orwell Dev-C++ page.
o Download the latest setup file (usually .exe).
2. Run the Installer
o Double-click the downloaded file to start installation.
o If Windows asks for permission, click Yes.
3. Choose Language
o Select your preferred installation language (commonly English) and click
OK.
4. Accept License Agreement
o Read and click I Agree to continue.
5. Select Installation Location
o By default, it installs in C:\Program Files (x86)\Dev-Cpp.
o You can change the folder if needed.
6. Choose Components
o Keep the default selected (Full Installation) and click Next.
7. Start Installation
o Click Install to copy files and set up Dev-C++.
8. Finish Setup
o Once done, click Finish.
o A shortcut will appear on your desktop.
9. Open Dev-C++
o Double-click the shortcut or open it from the Start menu.
o Configure the initial settings if prompted (font, compiler, etc.).
When you convert algorithms/flowcharts into C source code, the process usually
involves:
source code:
#include <stdio.h>
int main()
{
int num1, num2, sum;
scanf("%d", &num1);
scanf("%d", &num2);
return 0;
}
OUTPUT:
Enter the first number: 6
Enter the second number: 9
The sum of 6 and 9 is 15
WEEK 2
Lab 1: Converting algorithms/flow charts into C Source
code.
Developing the algorithms/flowcharts for the following sample programs
i) Sum and average of 3 numbers
ii) Conversion of Fahrenheit to Celsius and vice versa
iii) Simple interest calculation
Algorithm:
1. Start
2. Input three numbers: a, b, c
3. Compute sum = a + b + c
4. Compute avg = sum / 3
5. Print sum and avg
6. Stop
SOURCE CODE:
#include <stdio.h>
int main ()
{
float a, b, c, sum, avg;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
sum = a + b + c;
avg = sum / 3;
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", avg);
return 0;
}
OUTPUT:
Enter three numbers: 65
36
27
Sum = 128.00
Average = 42.67
ii) Conversion of Fahrenheit to Celsius and Vice Versa
Algorithm:
1. Start
2. Input temperature and choice (C→F or F→C)
3. If Fahrenheit → Celsius: C = (F - 32) * 5/9
4. If Celsius → Fahrenheit: F = (C * 9/5) + 32
5. Print result
6. Stop
SOURCE CODE:
Output:
iii) Simple Interest Calculation
Formula:
Where:
P = Principal amount
R = Rate of interest
T = Time (in years)
Algorithm:
1. Start
2. Input P, R, T
3. Compute SI = (P * R * T) / 100
4. Print SI
5. Stop
SOURCE CODE:
#include <stdio.h>
int main () {
float P, R, T, SI;
printf ("Enter Principal, Rate and Time: ");
scanf ("%f %f %f", &P, &R, &T);
SI = (P * R * T) / 100;
printf ("Simple Interest = %.2f\n", SI);
return 0;
}
OUTPUT:
WEEK 3
Lab 3: Simple computational problems using arithmetic expressions.
i) Finding the square root of a given number
Source code:
#include <stdio.h>
#include <math.h>
int main ()
{
double number, result;
return 0;
}
Output:
Source code:
#include <stdio.h>
#include <math.h>
int main()
{
double principal, rate, time, amount, compoundInterest;
Source code:
#include <stdio.h>
#include <math.h>
int main ()
{
double a, b, c, s, area;
// Calculating semi-perimeter
s = (a + b + c) / 2;
// Display result
printf ("The area of the triangle is %.2f\n", area);
return 0;
}
Output:
#include <stdio.h>
int main ()
{
double speed, time, distance;
// Calculating distance
distance = speed * time;
// Display result
printf ("The distance travelled is %.2f km\n", distance);
return 0;
}
Output:
Source code:
Algorithm:
Step 1: Declare A, B, C, D, E, F, G, i, j.
Step 2: Read A, B ,C, D, E, F, G, i, j Values.
Step 3: Evaluate the below expressions result_a = A + B * C + (D * E) + F * G. result_b
= A / B * C - B + A * D / 3. result_c = A++ + ++B - --A.J = (i++) + (++i).
Step 4: Print result_a, result_b,result_c,j
Step 5: Exit or terminate the program.
Program:
#include <stdio.h>
int main()
{
int A = 5, B = 10, C = 2, D = 7, E = 3, F = 4, G = 6;
int i = 5, j, result_a, result_b, result_c;
result_a = A + B * C + (D * E) + F * G;
printf ("a. Result of expression A+B*C+(D*E) + F*G is: %d\n", result_a);
result_b = A / B * C - B + A * D / 3;
printf ("b. Result of expression A/B*C-B+A*D/3 is: %d\n", result_b);
result_c = A++ + ++B - --A;
printf ("c. Result of expression A+++B---A is: %d\n", result_c);
j = (i++) + (++i);
printf ("d. Value of J after J = (i++) + (++i) is: %d\n", j);
return 0;
}
Output:
a. Result of expression A+B*C+(D*E) + F*G is: 70
b. Result of expression A/B*C-B+A*D/3 is: 1
c. Result of expression A+++B---A is: 11
d. Value of J after J = (i++) + (++i) is: 12
vi) Find the maximum of three numbers using conditional operator
Source code:
Algorithm:
Step-1: Declare num1, num2, num3, max.
Step-2: Read num1, num2, num3 using scanf () function.
Step-3: Calculate max value using the conditional operator (ternary operator) to find
the maximum. Step-4: Print max value.
Step-5: Exit or terminate the program.
Program:
#include <stdio.h>
int main ()
{
int num1, num2, num3, max;
printf ("Enter three numbers: ");
scanf ("%d %d %d", &num1, &num2, &num3);
max = (num1 > num2) ? ((num1 > num3)? num1 : num3) : ((num2 > num3) ? num2 :
num3);
printf ("Maximum number is %d\n", max);
return 0;
}
Output:
iv)Take marks of 5 subjects in integers, and find the total, average in float
Source code:
#include <stdio.h>
main ()
{
int marks [5], i;
float total = 0.0, average;
printf ("Enter marks of 5 subjects: ");
for ( i = 0; i< 5; i++)
{
scanf ("%d", &marks[i]);
total += marks[i];
}
average = total / 5.0;
printf ("Total marks: %.2f\n", total);
printf ("Average marks: %.2f\n", average);
return 0;
}
Output:
Enter marks of 5 subjects: 45
56
35
50
70
Total marks: 256.00
Average marks: 51.20
WEEK 5
Lab 5: Problems involving if-then-else structures.
i) Write a C program to find the max and min of four numbers using if-else.
Source code:
#include <stdio.h>
int main()
{
int n1, n2, n3, n4;
int max_val, min_val;
max_val = n1;
min_val = n1;
Output:
Source code:
#include <stdio.h>
int main()
int unit;
else
Output:
iii) Find the roots of the quadratic equation.
Source code:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double d;
d = b * b - 4 * a * c;
if (d > 0)
{
return 0;
}
Output:
iv) Write a C program to simulate a calculator using switch case.
Source code:
#include <stdio.h>
int main()
{
char op;
double n1, n2, result;
return 0;
}
Output:
Source code:
#include <stdio.h>
int main()
{
int year;
scanf("%d", &year);
return 0;
}
Output:
Lab 6: Iterative problems e.g., the sum of series
i)Find the factorial of given number using any loop.
Program:
#include main ()
{
int i=1, f = 1, n;
printf("Input the number : ");
scanf("%d", &n);
while (i<= n)
{
f = f * i;
i++;
}
printf("The Factorial of %d is: %d\n", n, f);
return 0;
}
#include <stdio.h>
int main()
{
int n, i, j, a[10], b[10];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (i = 0; i<n ;i++)
scanf("%d", &a[i]);
for (i = n - 1, j = 0; i>= 0; i--, j++)
b[j] = a[i];
for (i = 0; i< n; i++)
a[i] = b[i];
printf("Reverse array is\n");
for (i = 0; i< n; i++)
printf("%d\n", a[i]);
return 0;
}
Programme:
#include<stdio.h>
int main()
{
int a[50],i,j,k, count = 0, dup[50], n;
printf("Enter size of the array");
scanf("%d",&n);
printf("Enter Elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<n;i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] == a[j]){
for(k = j; k <n; k++)
{
a[k] = a[k+1];
}
j--;
n--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Sample Input and Output:
Enter size of the array 10
Enter Elements of the array:0 1 3 4 5 3 2 4 2 1
Entered element are: 0 1 3 4 5 3 2 4 2 1
After deleting the duplicate element the Array is:0 1 3 4 5 2
Lab 8: Matrix problems, String operations, Bubble sort
i) Addition of two matrices
Programme:
#include <stdio.h>
main()
{
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
if (j == c - 1)
{
printf("\n\n");
}
}
return 0;
}
Sample Input and Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2
Enter elements of 1st matrix:
Enter element a11: 12
Enter element a12: 2
Enter element a21: 4
Enter element a22: 2
Enter elements of 2nd matrix:
Enter element b11: 1
Enter element b12: 3
Enter element b21: 2
Enter element b22: 4
Sum of two matrices:
13 5
6 6
ii) Multiplication two matrices
Programme:
#include<stdio.h>
main()
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Please insert the number of rows and columns for first matrix \n ");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
tot = tot + fst[c][k] * sec[k][d];
}
mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the matrices is: \n ");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}
Sample Input and Output:
Please insert the number of rows and columns for first matrix
22
Insert your matrix elements:
11
11
Please insert the number of rows and columns for second matrix
22
22
The result of matrix multiplication or product of the matrices is:
44
44
iii) Enter n students data using calloc() and display failed students
list .
Programme:
#include<stdio.h>
#include<stdlib.h>
Struct student
{
char name[50];
int rollNumber;
float marks;
};
int main()
{
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student *students = (struct Student *)calloc(n, sizeof(struct
Student));
for (int i = 0; i< n; ++i)
{
printf("\nEnter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("\nList of failed students:\n");
for (int i = 0; i< n; ++i)
{
if (students[i].marks<=40.0)
{
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
}
}
free(students);
return 0;
}
Sample Input and Output:
Enter the number of students: 2
Enter details for student 1:
Name: divitha
Roll Number: 1
Marks: 90
Enter details for student 2:
Name: deepu
Roll Number: 2
Marks: 40
List of failed students:
Name: deepu
Roll Number: 2
Marks: 40.00
iv) Read student name and marks from the command line and
display the student details along with the total.
Programme:
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int marks;
};
int main(int argc, char *argv[]) {
if (argc< 2) {
printf("Usage: %s <number_of_students>\n", argv[0]);
return 1;
}
int n = atoi(argv[1]);
struct Student *students = (struct Student *)calloc(n, sizeof(struct
Student));
if (students == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
}
for (int i = 0; i< n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter marks of student %d: ", i + 1);
scanf("%d", &students[i].marks);
}
int total = 0;
int failedCount = 0;
for (int i = 0; i< n; i++) {
total += students[i].marks;
if (students[i].marks< 40) {
failedCount++;
}
}
float average = (float)total / n;
printf("\nStudent Details:\n");
for (int i = 0; i< n; i++) {
printf("Name: %s, Marks: %d\n", students[i].name,
students[i].marks);
}
printf("\nTotal marks: %d\n", total);
printf("Average marks: %.2f\n", average);
if (failedCount> 0) {
printf("\nFailed students list:\n");
for (int i = 0; i< n; i++) {
if (students[i].marks< 40) {
printf("Name: %s, Marks: %d\n", students[i].name,
students[i].marks);
}
}
} else {
printf("\nNo students failed.\n");
}
free(students);
return 0;
}
Sample Input and Output:
Enter the number of students: 2
Enter name of student 1: sai
Enter marks of student 1: 12
Enter name of student 2: deepu
Enter marks of student 2: 23
Total marks: 35
Average marks: 17.5
Failed students list:
Name: sai, Marks: 12
Name: deepu, Marks: 23
programme:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 4, i, *p, s = 0;
p = (int*) calloc(n, sizeof(int));
if(p == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array : ");
for(i = 0; i< n; ++i)
{
scanf("%d", p + i);
s += *(p + i);
}
printf("Sum : %d", s);
p = (int*) realloc(p, 6);
printf("Enter elements of array : ");
for(i = 0; i< n; ++i)
{
scanf("%d", p + i);
s += *(p + i);
}
printf("Sum : %d", s);
return 0;
}
Sample Input and Output:
Enter elements of array: 2 3 5 4
Sum: 14
Enter elements of array: 2 3 4 5 0 1
Sum: 28
Lab10 : Bitfields, linked lists Read and print a date using dd/mm/yyyy format using bit-
fields and differentiate the same without using bit- fields