0% found this document useful (0 votes)
7 views51 pages

C Programming Lab Manual: Installation & Basics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views51 pages

C Programming Lab Manual: Installation & Basics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C PROGRAMMING LABMANUAL

WEEK 1

Lab1: Familiarization with programming environment


i) Basic environment and its editors like Devc++, Turbo c.

Lab1: Familiarization with programming environment

Steps in Installation Process of Turbo C

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.

Steps to Install Dev-C++

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.).

LAB2: Converting algorithms/ flowcharts into C Source code. Developing the


algorithms/flow charts for the following sample programs

When you convert algorithms/flowcharts into C source code, the process usually
involves:

1. Problem statement → Understand what needs to be solved.


2. Algorithm → Write step-by-step instructions.
3. Flowchart → Draw the logical flow using symbols.
4. C Program → Translate into C syntax.
5. Execution → Compile and run the program.

ii)Writing simple programs using printf (), scanf ()

source code:

#include <stdio.h>
int main()
{
int num1, num2, sum;

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

sum = num1 + num2;

printf("The sum of %d and %d is %d\n", num1, num2, sum);

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

i) Sum and Average of 3 Numbers

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:

SI=P×R×T100SI = \frac{P \times R \times T}{100}SI=100P×R×T

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;

// Take user input


printf ("Enter a number: ");
scanf ("%lf", &number);

// Calculate square root


result = sqrt(number);

// Output the result


printf ("The square root of %.2f is %.2f\n", number, result);

return 0;
}

Output:

ii) Finding compound interest

Source code:

#include <stdio.h>
#include <math.h>

int main()
{
double principal, rate, time, amount, compoundInterest;

// Taking user input


printf ("Enter the principal amount: ");
scanf ("%lf", &principal);
printf ("Enter the rate of interest: ");
scanf ("%lf", &rate);
printf ("Enter the time in years: ");
scanf ("%lf", &time);

// Compound interest formula


amount = principal * pow ((1 + rate / 100), time);
compoundInterest = amount - principal;
// Display result
printf ("The compound interest is %.2f\n", compoundInterest);
printf ("The total amount is %.2f\n", amount);
return 0;
}
Output:

iii) Area of a triangle using heron’s formulae

Source code:

#include <stdio.h>
#include <math.h>

int main ()
{
double a, b, c, s, area;

// Taking sides input


printf ("Enter the lengths of the sides of the triangle (a, b, c): ");
scanf ("%lf %lf %lf", &a, &b, &c);

// Calculating semi-perimeter
s = (a + b + c) / 2;

// Calculating area using Heron's formula


area = sqrt (s * (s - a) * (s - b) * (s - c));

// Display result
printf ("The area of the triangle is %.2f\n", area);

return 0;
}
Output:

iv) Distance travelled by an object


Source code:

#include <stdio.h>
int main ()
{
double speed, time, distance;

// Taking speed and time input


printf ("Enter the speed (in km/h): ");
scanf ("%lf", &speed);
printf ("Enter the time (in hours): ");
scanf ("%lf", &time);

// Calculating distance
distance = speed * time;

// Display result
printf ("The distance travelled is %.2f km\n", distance);

return 0;
}

Output:

Lab4: Simple computational problems using the operator’ precedence and


associativity
v)Evaluate the following expressions.
• A+B*C+(D*E) + F*G
• A/B*C-B+A*D/3
• A+++B---A
• J= (i++) + (++i)

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:

Enter three numbers: 55


65
45
Maximum number is 65

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;

printf("Enter four integers: ");


scanf("%d %d %d %d", &n1, &n2, &n3, &n4);

max_val = n1;
min_val = n1;

if (n2 > max_val)


{
max_val = n2;
}
if (n2 < min_val)
{
min_val = n2;
}

if (n3 > max_val)


{
max_val = n3;
}
if (n3 < min_val)
{
min_val = n3;
}

if (n4 > max_val)


{
max_val = n4;
}
if (n4 < min_val)
{
min_val = n4;
}

// Print the maximum and minimum values


printf("Maximum value: %d\n", max_val);
printf("Minimum value: %d\n", min_val);
return 0;
}

Output:

ii) Write a C program to generate electricity bill.

Source code:

#include <stdio.h>

int main()

int unit;

float amt, total_amt, sur_charge;

/* Input unit consumed from user */

printf ("Enter total units consumed: ");


scanf ("%d", &unit);

/* Calculate electricity bill according to given conditions */

if(unit <= 50)

amt = unit * 0.50;

else if(unit <= 150)

amt = 25 + ((unit-50) * 0.75);

else if(unit <= 250)

amt = 100 + ((unit-150) * 1.20);

else

amt = 220 + ((unit-250) * 1.50);

sur_charge = amt * 0.20;

total_amt = amt + sur_charge;

printf("Electricity Bill = Rs. %.2f", total_amt);


return 0;

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;

printf("Enter coefficients a, b, and c: ");


scanf("%lf %lf %lf", &a, &b, &c);

d = b * b - 4 * a * c;

if (d > 0)
{

printf("Roots are real and distinct:\n");


}
else if (d == 0)
{
printf("Roots are real and equal:\n");
}
else
{

printf("Roots are complex and distinct:\n");


}

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;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &op);

printf("Enter two numbers: ");


scanf("%f%f", &n1, &n2);

// Use switch case to perform calculation based on operator


switch (op) {
case '+':
result = n1 + n2;
printf("%f + %f = %f\n", n1, n2, result);
break;
case '-':
result = n1 - n2;
printf("%f - %f = %f\n", n1, n2, result);
break;
case '*':
result = n1 * n2;
printf("%f * %f = %f\n", n1, n2, result);
break;
case '/':
if (n2 != 0) {
result = n1 / n2;
printf("%f / %f = %f\n", n1, n2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
}

return 0;
}
Output:

v) Write a C program to find the given year is a leap year or not.

Source code:

#include <stdio.h>

int main()
{
int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", 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;
}

Sample Input and Output:


Input the number :6
The Factorial of 6 is: 720
ii) Find the given number is a prime or not.
Programme:
#include<stdio.h>
main()
{
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf ("Number is prime");
return 0;
}
Sample input and output:
Enter the number to check prime:4
Number is not prime.
iii) Checking a number palindrome
Programme:
#include <stdio.h>
main()
{
int num,r,sum=0,t;
printf("Input a number: ");
scanf("%d", &num);
for(t = num; num != 0; num = num / 10)
{
r = num % 10;
sum = sum * 10 + r;
}
if(t == sum)
printf("%d is a palindrome number.\n", t);
else
printf("%d is not a palindrome number.\n", t);
return 0;
}
Sample Input and Output:
Number Palindrome Input a number: 121
121 is a palindrome number.
Input a number: 123
123 is not a palindrome number.
v) Construct a pyramid of numbers.
Programme:
#include <stdio.h>
int main()
{
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; ++i)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
printf("%d ", i + k);
++count;
}
else
{
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

Sample Input and Output:


Enter the number of rows: 5
1
232
34543
456765
567898765
Lab 7:1D Array manipulation, linear search
Aim:
[Link]: Implement a C program to find the min and max of a 1-D integer array.
Programme:
#include <stdio.h>
void main()
{
int arr1[100];
int i, max, min, n;
printf("\n\nFind maximum and minimum element in an array :\n");
printf("------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
max = arr1[0];
min = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>max)
{
max = arr1[i];
}
if(arr1[i]<min)
{
min = arr1[i];
}
}
printf("Maximum element is : %d\n", max);
printf("Minimum element is : %d\n\n", min);
}

Sample Input and Output:


Find maximum and minimum element in an array:
Input the number of elements to be stored in the array :5
Input 5 elements in the array:
element - 0: 12
element - 1: 35
element - 2: 3
element - 3: 90
element - 4: 1
Maximum element is: 90
Minimum element is: 1
[Link]: Implement a C program to Perform linear search on 1D array.
Programme:
#include <stdio.h>
int main()
{
int a[10], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (i = 0; i< n; i++)
{
if (a[i] == search)
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Sample Input and Output:
Enter number of elements in array
4
Enter 4 integer(s)
12
90
4
-1
Enter a number to search
12
12 is present at location 1.
[Link]: Implement a C program to reverse a 1D integer array.
Programme:

#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;
}

Sample Input and Output:


Enter the number of elements in array
5
Enter the array elements
12
34
9
1
0
Reverse array is
0
1
9
34
12
[Link]: Implement a C program to find 2’s complement of the given binary number.
Programme:
#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;\
printf("Enter %d bit binary value: ", SIZE);
/* Input 8-bit binary string */
gets(binary);

/* Find ones complement of the binary number */


for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';
/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
printf("Original binary = %s\n", binary);
printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);
return 0;
}
Sample Input and Output:
Enter 8 bit binary value: 01110111
Original binary = 01110111
Ones complement = 10001000
Twos complement = 10001001
iv) Eliminate duplicate elements in an array.

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) Sort array elements using bubble sort


Programme:
#include<stdio.h>
bubble(int *a,int n)
{
int tem,count, i,j;
for(i=0;i<n-1;i++)
{
count=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
count=1;
}
}
if(count==0)
break;
}
}
main()
{
int n,a[100],i;
printf("\nenter number of elements:");
scanf("%d",&n);
printf("\nenter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements before sorting :");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
bubble(a,n);
printf("\nThe elements after sorting are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
return 0;
}
Sample Input and Output:
Enter number of elements:5
Enter array elements: 45 9 21 3 57
The elements before sorting: 45 9 21 3 57
The elements after sorting are : 3 9 21 45 57

iv) Concatenate two strings without built-in functions


Programme:
void main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Sample Input and Output:
Enter First String: Sai
Enter Second String: deepthi
Concatenated String is saideepthi

v) Reverse a string using built-in and without built-in string functions


Programme:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100];
int l, i;
printf("\n\nPrint individual characters of string in reverse order :\n");
printf(" \n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are : \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
Sample Input and Output:
Print individual characters of string in reverse order:
Input the string: saideepthi
The characters of the string in reverse are: I h t p e e d i a s
Lab 9: Pointers and structures, memory dereference.
i) Write a C program to find the sum of a 1D array using
malloc()
Programme:

ii) Write a C program to find the total, average of n students


using structures.
Programme:
#include<stdio.h>
struct student
{
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
}
Sample Input and Output:
Enter the number of student=2
Enter the detail of 1 students
Enter Name=deepu
Enter the three subject score 45 56 67
Enter the detail of 2 students
Enter Name=deepthi
Enter the three subject score 78 45 78
deepu has scored above the average marks
deepthi has scored above the average marks1

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

v)Write a C program to implement realloc()

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

i) Demonstrate the differences between structures and unions using a C program.


Programme:
#include <stdio.h>
struct Person
{
char name[50];
int age;
};
union Data {
int integerData;
float floatData;
};
int main()
{
struct Person person1;
strcpy([Link], "John");
[Link] = 25;
union Data data1;
[Link] = 42;
printf("Structure - Person:\n");
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("\n");
printf("Union - Data:\n");
printf("Integer Data: %d\n", [Link]);
[Link] = 3.14;
printf("Float Data: %f\n", [Link]);
return 0;
}
Sample Input and Output:
Structure - Person:
Name: John
Age: 25
Union - Data:
Integer Data: 42
Float Data: 3.140000
ii) Write a C program to copy one structure variable to another structure of the
same type.
Programme:
#include <stdio.h>
struct Student
{
char name[50];
int age;
float marks;
};
void copyStruct(struct Student *dest, const struct Student *src)
{
strcpy(dest->name, src->name);
dest->age = src->age;
dest->marks = src->marks;
}
void displayStudent(const struct Student *student)
{
printf("Name: %s\n", student->name);
printf("Age: %d\n", student->age);
printf("Marks: %.2f\n", student->marks);
}
int main()
{
struct Student student1 = {"John Doe", 20, 85.5};
struct Student student2;
copyStruct(&student2, &student1);
printf("Original Student:\n");
displayStudent(&student1);
printf("\nCopied Student:\n");
displayStudent(&student2);
return 0;
}
Sample Input and Output:
Original Student:
Name: John Doe
Age: 20
Marks: 85.50
Copied Student:
Name: John Doe
Age: 20
Marks: 85.50
Lab 11: Simple functions using call by value, solving differential equations using Eulers
theorem.
i) Write a C function to calculate NCR value
Programme:
#include <stdio.h>
int fact(int z);
void main()
{
int n, r, ncr;
printf("\n Enter the value for N and R \n");
scanf("%d%d", &n, &r);
ncr = fact(n) / (fact(r) * fact(n - r));
printf("\n The value of ncr is: %d", ncr);
}
int fact(int z)
{
int f = 1, i;
if (z == 0)
{
return(f);
}
else
{
}
for (i = 1; i<= z; i++)
{
f = f * i;
}
return(f);
}
Sample Input and Output:
Enter the value for N and R
52
The value of ncr is: 10
ii) Write a C function to find the length of a string.
Programme:
#include<stdio.h>
#include<string.h>
int main() {
char str[100]; int len;
printf("\nEnter the String : ");
scanf("%s",str);
len = strlen(str);
printf("\nLength of the given string is %d", len);
return(0);
}
Sample input and output:
Enter the String: deepthi
Length of the given string is 7
iii) Write a C function to transpose of a matrix.
Programme:
#include <stdio.h>
#define N 4
void transpose(int A[][N], int B[][N])
{
}
int i,j;
for (i = 0; i< N; i++)
for (j = 0; j < N; j++)
B[i][j]=A[j][i];
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },{2,2,2,2},{3,3,3,3},{4,4,4,4} };
int B[N][N], i, j;
transpose(A, B);
printf("Result matrix is \n");
for (i = 0; i< N; i++) {
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}
return 0;
}
Sample Input and Output:
Result matrix is
1234
1234
1234
1234

You might also like