0% found this document useful (0 votes)
27 views35 pages

C Lab Record-CSE

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)
27 views35 pages

C Lab Record-CSE

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/ 35

FRANCIS XAVIER ENGINEERING COLLEGE TIRUNELVELI

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

24CS1511 – PROGRAMMING PRACTICES


LABORATORY

2024 – 2025 / ODD SEMESTER

REGISTER NUMBER :

NAME OF THE STUDENT :

BRANCH :

BATCH : 2024 – 2028 BATCH

YEAR / SEMESTER : I YEAR / I SEMESTER

24CS1511 – PROGRAMMING PRACTICES LAB 1 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
FRANCIS XAVIER ENGINEERING COLLEGE (AUTONOMOUS),
TIRUNELVELI-627003

BONAFIDE CERTIFICATE

Certified that this is a bonafide record of work done by

Selvan/Selvi................................................................. with

Reg.No………………..…………………………………….. of I semester in COMPUTER SCIENCE

AND ENGINEERING branch of this institution in the 24CS1511 – PROGRAMMING

PRACTICES LABORATORY during 2024-2025.

Staff-in charge H.O.D

Submitted for the Practical Examination held on…………………

Internal Examiner External Examiner


24CS1511 – PROGRAMMING PRACTICES LAB 2 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex DATE
Name of the Experiment Marks Sign
No

1 Programs using simple statements

2 Programs using decision making statements

3 Programs using looping statements

Programs using one dimensional and two dimensional


4
Arrays

5 Programs using strings

Programs using user defined functions and recursive


6
Functions

7 Programs using functions and pointers

8 Programs using structures and pointers

9 Programs using structures and unions

10 Programs using file concept

24CS1511 – PROGRAMMING PRACTICES LAB 3 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING SIMPLE STATEMENTS
Ex No: 1a FINDING SQUARE AND CUBE OF A NUMBER
Date:
Aim
To write a C Program to find square and cube of a number.

Algorithm
1. Read a number.
2. Find the square of the number (square=number*number).
3. Find the cube of the number (cube= number*number*number).
4. Print the square and cube of the number.
5. Terminate the Program.

Program
/* Finding the Square and Cube of a number */
#include <stdio.h>
#include <conio.h>
void main()
{
int num, square, cube;
clrscr();
printf("Enter a integer : ");
scanf("%d", &num);
square = num * num;
cube = num * square;
printf("Given number = %d, Square Value = %d, Cube value = %d\n", num, square,
cube);
getch( );
}

Result
Thus the C Program to find the square and cube of a number is executed and the
Output is obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 4 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No 1b FINDING SIMPLE INTEREST AND COMPOUND INTEREST
Date:
Aim:
To write a C Program to find the Simple Interest and Compound Interest.
Algorithm:
1. Read two numbers p, n & r.
2. Calculate the Simple Interest = (p * n * r ) / 100
3. Print the Simple Interest
4. Calculate the Compound Interest = (p * pow(( 1 + r /100), n)) - p
5. Print the Compound Interest
6. Terminate the Program
Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
int n;
float p, q, r, si, ci;
clrscr();
printf("Enter the Principal Amount : ");
scanf("%f", &p);
printf("Enter the No. of Years : ");
scanf("%d", &n);
printf("Enter the rate of Interest : ");
scanf("%f", &r);
si = p * n * r/100;
q = pow((1 + r/100), n);
ci = (p * q) - p;
printf("\nSimple Interest = %.2f", si);
printf("\nCompound Interest = %.2f", ci);

getch( );
}

Result :
Thus the C Program to find Simple Interest and Compound Interest is executed
and the Output is obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 5 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No 1c FINDING THE AREA OF A TRIANGLE
Date:
Aim:
To write a C Program to find the Area of the Triangle.
Algorithm:
1. Read three sides a, b, and c.
2. Calculate s = (a + b + c)/2;
3. Calculate the area = sqrt(s * (s-a) * (s-b) * (s-c));
4. Print the Area of the Triangle
5. Terminate the Program
Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a, b, c, s, area;
clrscr();
printf("Enter the Three Sides : ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c)/2;
area = sqrt(s * (s-a) * (s-b) * (s-c));
printf("\nArea of the Triangle = %.2f", area);
getch( );
}

Result :
Thus the C Program to find Area of the Triangle is executed and the Output is
obtaine

24CS1511 – PROGRAMMING PRACTICES LAB 6 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING DECISION-MAKING STATEMENTS
Ex No 2a CHECKING OF VOTING ELIGIBILITY
Date:
Aim
To write a C Program to get age of a person as input and tells whether he is eligible to
vote.
Algorithm
1. Read the age of a person
2. If age is less than 18 print “Sorry, You are not eligible for voting”
3. Else print “You are eligible for voting”
4. Terminate the Program.
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int age;
clrscr();
printf( "Please enter your age : " );
scanf( "%d", &age );
if ( age < 18 )
printf ("Sorry, You are not eligible for voting \n" );
else
printf ("You are eligible for voting \n" );
getch( );
}

Result:
Thus the C Program to check whether the person is eligible for voting or not is
executed and the Output is obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 7 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
CHECKING WHETHER A NUMBER IS ODD OR EVEN
Ex No :2b
Date:
Aim:
To write a C Program to check whether the given number is odd or even.
Algorithm:
1. Read a number.
2. If n%2=0 print the number is even
3. Else print the number is odd
4. Terminate the Program.
Program
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer : ");
scanf("%d", &n);
if (n%2 == 0)
printf("%d is an even number.\n", n);
else
printf("%d is an odd number.\n", n);
return 0;
getch( );
}

Result
Thus the Program to check whether the given number is odd or even is executed and the
Output is obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 8 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex.No: 2C Finding the Largest among Three Numbers
Date:
Aim
To find the largest among three given numbers.
Algorithm
1. Read three numbers a, b, c
2. If a is greater than b and a is greater than c then assign a to largest.
3. Else if b is greater than c then print assign b to largest.
4. Else assign c to largest.
5. Print largest.
6. Terminate the program
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, largest;
clrscr();
printf("Enter three numbers : ");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
if(a>b && a>c)
largest = a;
else if(b>c)
largest = b;
else
largest = c;
printf("Largest number is = %d",largest);
getch( );
}

Result

Thus the Program to find largest among three numbers is executed and the Output is
obtaine

24CS1511 – PROGRAMMING PRACTICES LAB 10 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING LOOPING STATEMENTS
Ex No: 3 a PRINT FIRST N PRIME NUMBERS USING LOOP
Date:
Aim
To write a C program to find the first n prime numbers using loop.
Algorithm
1. Start the program.
2. Read n.
3. Initialize count=0
4. Check n<2, if it is true, print there are no prime numbers. Go to step 6
5. Initialize i to 2
While i<N
Initialize j to 2
While j<i/2
If i%j=0
set flag to 1 and break
Increment j by 1
If (flag ==0)
Print i
Increment i by 1
6. Terminate the Program

Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, j, flag, count = 0;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
if(n < 2)
{
printf("There are no primes upto %d\n", n);
exit(0);
}
printf("The Prime numbers are\n");
for (i=2; i<=n; i++)
{
flag = 0;
for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ",i);
count++;
}
24CS1511 – PROGRAMMING PRACTICES LAB 11 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
}
printf("\nNumber of primes upto %d = %d",n,count);
getch( );

Result
Thus the C program to find n prime numbers using loop has been executed and
the output was obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 12 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No 3 b REVERSING THE NUMBER AND FINDING THE SUM
OF THE DIGITS OF GIVEN NUMBER
Date:
Aim
To write a c program to print the individual digits and to find the sum of digits
and the reverse for a given number.
Algorithm
1. Initialize the sum as 0
2. Read the input
3. Take the remainder of given number while dividing 10
4. Overwrite the sum by adding above remainder with available sum
5. Calculate reverse = reverse * 10 + remainder
6. Overwrite the number by divide with 10
7. Repeat the steps 3 to 5 until the number is greater than 0
8. Print the sum, reverse
9. Terminate the program
Program
#include <stdio.h>
#include <conio.h>
main()
{
int n, dig, sum=0, rev=0;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
while(n>0)
{
dig = n % 10;
sum = sum + dig;
n = n / 10;
rev = rev * 10 + dig;
};
printf("The Sum of digits of the number is %d",sum);
printf("\nThe reversed Number is %d", rev);
getch( );
}

Result
Thus the C program to find the sum of digits and reverse for a given number was
written, entered, executed and the output was obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 13 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No: 3 c PRINTING THE AVERAGE OF NUMBERS UNTIL ZERO IS
ENTERED
Date:
Aim
To write a C Program to read in values until a zero is entered, then print
average.
Algorithm
1. Read a number
2. If number is equal to zero go to step 4 and calculate the average.
3. Else calculate the sum and read the next number.
4. Print the average.
5. Terminate the Program.
Program
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int count = 0, sum = 0, value;
float average;
clrscr();
/* read in values until a zero is entered, then print average */
printf("Enter a integer: ");
scanf("%d", &value);
while(value != 0)
{
sum = sum + value;
count++;
printf("Enter a integer: ");
scanf("%d", &value);
}
if(count > 0)
{
average = sum / count;
printf("The average of the values is %.2f\n", average);
}
else
printf("No Values has been entered");
getch( );

Result :
Thus the C Program to find average of numbers is executed and the output is
obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 14 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No 3 d FINDING GCD, LCM OF TWO NUMBERS
Date:
Aim:
To write a C Program to find GCD and LCM.
Algorithm:
1. Read two numbers a & b.
2. Calculate the GCD of the two numbers
a. Do the following until b not equal zero
i. r = a % b;
ii. a = b;
iii. b = r;
3. Print the a (GCD)
4. Calculate the LCM of the two numbers that is lcm = (x*y)/gcd;
5. Print LCM.
6. Terminate the Program
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, x, y, r, gcd, lcm;
clrscr();
printf("Enter the first integer : ");
scanf("%d", &x);
printf("Enter the second integer : ");
scanf("%d", &y);
if(x > y)
{
a = x;
b = y;
}
else
{
a = y;
b = x;
}
while (b != 0)
{
r = a % b;
a = b;
b = r;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest Common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least Common multiple of %d and %d = %d\n", x, y, lcm);
getch( ) ;
}
Result :
Thus the C Program to find GCD and LCM of two numbers is executed and the
Output is obtained

24CS1511 – PROGRAMMING PRACTICES LAB 15 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No :3e PRINTING THE FIBONACCI SERIES
Date:
Aim
To write a c program to print the Fibonacci Series.
Algorithm
1. Read how many numbers, n
2. Initialize the n1 as 1
3. Initialize the n2 as 1
4. Print n1, n2
5. Ctr = 2
6. While(ctr<=n)
n3 = n1 + n2
print, n3
n1 = n2
n2 = n3
ctr = ctr + 1
7. Terminate the Program

Program
#include <stdio.h>
#include <conio.h>
main()
{
int n1=1, n2=1, n3, ctr=2, n;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
printf("%d %d ", n1, n2);
while(ctr<=n)
{
n3 = n1 + n2;
printf("%d ", n3);
n1 = n2;
n2 = n3;
ctr++;
};
getch( );
}

Result
Thus the C program to print the Fibonacci Series is done and output Obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 16 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING ONE DIMENSIONAL AND TWO
DIMENSIONAL ARRAYS
Ex no : 4a ASCENDING AND DESCENDING ORDER OF THE GIVEN
NUMBERS

AIM:
To write a C Program to print numbers in Ascending and Descending order.

ALGORITHM:
Step 1: Start
Step 2: Declare variables
Step 3: Get the no. of terms N.
Step 4: Get the numbers using for loop and store it in array A[10].
Step 5: Set two for loops to arrange the given numbers in ascending order in the
array A[10].
Step 5.1: Check IF(A[I]>A[j]) then exchange the current two numbers and
continue
the same process until end of the array A[10].
Step 6: Print the numbers in Ascending order and Descending order using for
loop.
Step 7: Stop
Program:
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("\nASCENDING AND DESCENDING ORDER OF THE GIVEN NUMBERS");
printf("\n ---------------------------------------------------------------------------");
printf("\nEnter the no. of terms :");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nAscending order :");
for(i=0;i<n;i++)
24CS1511 – PROGRAMMING PRACTICES LAB 17 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
printf("%d\t",a[i]);
printf("\nDescending order:");
for(i=n-1;i>=0;i--)
printf("%d\t",a[i]);
getch();
}

RESULT:
Thus a C program to print numbers in Ascending and Descending order was executed and the output
was obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 18 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
24CS1511 – PROGRAMMING PRACTICES LAB 19 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Exno 4 b MATRIX MULTIPLICATION
AIM:
To write a C Program to perform Matrix multiplication using array.
ALGORITHM:
Step 1: Start
Step 2: Declare variables
Step 3: Get the rows and columns of two matrices M, N, O, P respectively.
Step 4: Check N is not equal to O, if go to step 10.
Step 5: Set a loop and get the elements of first matrix A[i][i].
Step 6: Set a loop and get the elements of second matrix B[i][j].
Step 7: Repeat the step 6 until i<m, j<p and k<n.
Step 8: Initialise C[i][j]=0 and multiply the two matrices and store the resultant in
C[i][j]= C[i][j]+A[i][k]*B[k][j].
Step 9: Print the resultant matrix C[i][j] and go to step 11.
Step 10: Print the message “Column of first matrix must be same as row of

second
matrix”.
Step 11: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,o,p;
clrscr();
printf("\nMATRIX MULTIPLICATION\n");
printf("\n ----------------------------- \n");
printf("\nEnter the rows & columns of first matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the rows & columns of second matrix: ");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix-->");

24CS1511 – PROGRAMMING PRACTICES LAB 20 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the Second matrix-->");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n\nThe First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++) //row of first matrix
{
for(j=0;j<p;j++) //column of second matrix
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}

24CS1511 – PROGRAMMING PRACTICES LAB 21 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
}
getch();
}

RESULT:
Thus a C program for the implementation of matrix multiplication was executed
and the output was obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 22 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex. No.5 PROGRAMS USING STRINGS

AIM:
To write a C program to illustrate the concept of string handling functions.
ALGORITHM:
Step 1: Start the program.
Step 2: Get two strings as input.
Step 3: Use the string compare function for checking the two strings are equal.
Step 4: Use the string concatenation function for joining first string with second string.
Step 5: Find the length of the strings using the strlen function.
Step 6: Print the reverse of a string using strrev function.
Step 7: Show the upper case and lower case of a string using strupr and strlwr functions.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int s;
char string1[10],string2[10];
clrscr();
printf("Enter string 1 : ");prp
scanf("%s",&string1);
printf("Length of the string is : %d",strlen(string1)); // string length
printf("Uppercase of the string is : %s",strupr(string1)); // string uppercase
printf("Lowercase of the string is : %s",strlwr(string1)); // string lowercase
printf("Reverse of the string is : %s",strrev(string1)); // string reverse
strcpy(string2,string1);
printf("The copied string : %s",string2); // string copy
strcat(string2,string1);
printf("The concatenated string : %s",string2); //string concatenation
s=strcmp(string2,string1);
printf("string comparison\n"); // string comparison
if(s==0)
24CS1511 – PROGRAMMING PRACTICES LAB 23 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
{
printf("strings 1&2 are equal");
}
else
{
printf("strings 1&2 are not equal\n");
}
getch();
}

RESULT:
Thus a C program for the implementation of String functions were executed and
the output was obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 24 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING USER DEFINED FUNCTIONS AND RECURSIVE
FUNCTIONS
Ex no 6 a USER DEFINED FUNCTIONS
Aim:
To write a C Program to add two numbers using user defined functions
Algorithm:
1. Read the value a and b.
2. Pass the values as an argument inside the fuction
3. Perform the addition inside the function.
4. Print the factorial of the given number and return the value to the main function.
5. Print the value
6. Terminate the Program

Program:
#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{
int a=10,b=20;
int c=add(10,20); //function call
printf("Addition:%d\n",c);
getch();
}
int add(int a,int b) //function body
{
int c;
c=a+b;
return c;
getch( );

Result :
Thus the C Program to add two numbers for the given number is executed and the
output is verified.
24CS1511 – PROGRAMMING PRACTICES LAB 25 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
FINDING THE FACTORIAL OF A NUMBER
Ex No 6 b
Date:
Aim:
To write a C Program to find the Factorial of a given number.
Algorithm:
1. Read the number n.
2. Pass the value as an argument inside the fuction
3. If n>=1
Return n*factorial(n-1)
Else
Return 1
4. Print the Factorial of the given number
5. Terminate the Program
Program
#include<stdio.h>
long int factorial(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
long int factorial(int n) {
if (n>=1)
return n* factorial(n-1);
else
return 1;
getch( );
}
Output:
Enter the Number : 5

Factorial of 5 = 120

Result :
Thus the C Program to find Factorial of a given number is executed and the
Output is obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 26 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAM USING FUNCTIONS AND POINTERS
Ex no : 7 a PROGRAM TO PERFORM SWAPPING USING FUNCTION.
Date:

Aim:
To write a C program to perform swapping using function.

Algorithm:
1. Start the program
2. Declare and get the two integer variables a and b.
3. call the swap () function
In swap definition use the temporary variable and assign temp =a
a=b
b=temp
4. Print the a and b value.
5. Display the result
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(inta,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
getch( );
}

Result:
Thus the C program to perform swapping using function has been successfully executed
and verified.
24CS1511 – PROGRAMMING PRACTICES LAB 27 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex no 7 b PROGRAM USING POINTERS
Aim
To write a program to perform pointer operations
Algorithm:
1. Declare the variable and pointer variable
2. Assign values to the variable
3. Assigning the address of variable var to the pointerb * p.
4. The p can hold the address of var because var is * an integer type variable.
5. Print all the values and address
6. Terminate the process
Program:
#include <stdio.h>
int main()
{
/* Pointer of integer type, this can hold the
* address of a integer type variable.
*/
int *p;

int var = 10;

p= &var;

printf("Value of variable var is: %d", var);


printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p);
return 0;
getch( );
}

Result:
Thus the program to perform pointer operations has been successfully executed
and verified.
24CS1511 – PROGRAMMING PRACTICES LAB 28 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING STRUCTURES AND POINTERS

Ex No: 8 SALARY SLIP OF EMPLOYEES

Date:
Aim
To write a C Program to Generate salary slip of employees using structures and
pointers.
Algorithm
1. Start
2. Declare variables
3. Read the number of employees .
4. Read allowances, deductions and basic for each employee.
5. Calculate net pay= (basic+ allowances)-deductions
6. Display the output of the Pay slip calculations for each employee.
7. Stop
Program
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int empno ;
char name[10], answer ;
int bpay, allow, ded, npay ;
} e[100];
void main()
{
int i, n;
clrscr() ;
printf("Enter No. of employees : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("Enter the name : ") ;
scanf("%s",&e[i].name) ;
printf("Enter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t\t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i=0; i<n; i++)
{
printf("%d \t %s \t\t %d \t %d \t %d \t %d \n", e[i].empno, e[i].name, e[i].bpay,
e[i].allow, e[i].ded, e[i].npay);
}
getch() ;
}
24CS1511 – PROGRAMMING PRACTICES LAB 29 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Result
Thus a C Program for Salary Slip of employees was executed and the output was
obtained
24CS1511 – PROGRAMMING PRACTICES LAB 30 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING STRUCTURES AND UNIONS

Ex No: 9 INTERNAL MARKS OF STUDENTS


Date:
Aim
To write a C program to compute internal marks of students for five different
subjects using structures and functions.
Algorithm
1. Start
2. Declare variables
3. Read the number of students .
4. Read the student mark details
5. Calculate internal mark by i=total of three test marks / 3 for each subject per
student.
6. Display the output of the calculations for all the students .
7. Stop
Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct stud
{
char name[20];
long int rollno;
int marks[5][3];
int internal[5];
}students[10];
void calcinternal(int);
int main()
{
int i, j, k, n, total;
clrscr();
printf("Enter how many students : ");
scanf("%d",&n);
for(i=0;i<n; i++)
{
printf("Enter the details of %d student \n", i+1);

printf("Enter student %d Name : ", i+1);


scanf("%s", students[i].name);
printf("Enter student %d Roll Number : ", i+1);
scanf("%ld", &students[i].rollno);
for(j=0; j<5; j++)
{
students[i].internal[j]=0;
for(k=0; k<3; k++)
{
printf("Enter the subject %d mark of Test-%d : ",j+1, k+1);
scanf("%d", &students[i].marks[j][k]);
24CS1511 – PROGRAMMING PRACTICES LAB 31 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
students[i].internal[j] = students[i].internal[j] + students[i].marks[j][k];
}
}
}
calcinternal(n);
for(i=0; i<n; i++)
{
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[i].name);
printf("\t\t\t\t Roll No : %ld", students[i].rollno);
printf("\n ----------------------------------------------------------------------");
for(j=0; j<5; j++)
{
printf("\n\n\t Subject %d internal \t\t :\t %d", j+1, students[i].internal[j]);
}
printf("\n\n ---------------------------------------------------------------------- \n");
getch();
}
return(0);
}
void calcinternal(int n)
{
int i,j, k;
for(j=0; j<5; j++)
students[i].internal[j] = students[i].internal[j] / 3;
}

Result
Thus a C program for internal marks of students was executed and the output was
obtained.

24CS1511 – PROGRAMMING PRACTICES LAB 32 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No: 10 PROGRAMS USING FILE CONCEPT
Date:
Aim
To perform various file operations in C, such as creating, writing, reading, appending,
copying, and deleting a file, using standard file handling functions.
Algorithm:
1. Start the program.
2. Declare necessary variables and file pointers.
3. Provide options to the user for the following operations:
o Create and write to a file.
o Read from a file.
o Append to a file.
o Copy the contents of one file to another.
o Delete a file.
4. Based on the user's choice:
o Perform the respective file operation.
o Handle errors (e.g., file not found) using condition checks.
5. Display appropriate messages after each operation.
6. Repeat until the user chooses to exit.
7. End the program.
Program:
#include <stdio.h>
#include <stdlib.h>

void createAndWrite();
void readFile();
void appendFile();
void copyFile();
void deleteFile();

int main() {
int choice;

do {
printf("\n=== File Operations Menu ===\n");
printf("1. Create and Write to a File\n");
printf("2. Read from a File\n");
printf("3. Append to a File\n");
printf("4. Copy a File\n");
printf("5. Delete a File\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
createAndWrite();
break;
case 2:
readFile();
break;
case 3:

24CS1511 – PROGRAMMING PRACTICES LAB 33 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
appendFile();
break;
case 4:
copyFile();
break;
case 5:
deleteFile();
break;
case 6:
printf("Exiting program. Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);

return 0;
}

void createAndWrite() {
FILE *file;
char filename[100], content[1000];

printf("Enter the file name to create: ");


scanf("%s", filename);

file = fopen(filename, "w");


if (file == NULL) {
printf("Error: Could not create file.\n");
return;
}

printf("Enter content to write to the file: ");


getchar(); // Clear newline character from input buffer
fgets(content, sizeof(content), stdin);

fprintf(file, "%s", content);


fclose(file);
printf("File '%s' created and written successfully.\n", filename);
}

void readFile() {
FILE *file;
char filename[100], ch;

printf("Enter the file name to read: ");


scanf("%s", filename);

file = fopen(filename, "r");


if (file == NULL) {
printf("Error: File '%s' not found.\n", filename);
return;
}
24CS1511 – PROGRAMMING PRACTICES LAB 34 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
printf("Contents of the file '%s':\n", filename);
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

fclose(file);
}

void appendFile() {
FILE *file;
char filename[100], content[1000];

printf("Enter the file name to append to: ");


scanf("%s", filename);

file = fopen(filename, "a");


if (file == NULL) {
printf("Error: File '%s' not found.\n", filename);
return;
}

printf("Enter content to append: ");


getchar(); // Clear newline character from input buffer
fgets(content, sizeof(content), stdin);

fprintf(file, "%s", content);


fclose(file);
printf("Content appended to file '%s' successfully.\n", filename);
}

void copyFile() {
FILE *srcFile, *destFile;
char srcFilename[100], destFilename[100], ch;

printf("Enter the source file name: ");


scanf("%s", srcFilename);
printf("Enter the destination file name: ");
scanf("%s", destFilename);

srcFile = fopen(srcFilename, "r");


if (srcFile == NULL) {
printf("Error: Source file '%s' not found.\n", srcFilename);
return;
}

destFile = fopen(destFilename, "w");


if (destFile == NULL) {
printf("Error: Could not create destination file '%s'.\n", destFilename);
fclose(srcFile);
return;
}

24CS1511 – PROGRAMMING PRACTICES LAB 35 | Page


PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
while ((ch = fgetc(srcFile)) != EOF) {
fputc(ch, destFile);
}

fclose(srcFile);
fclose(destFile);
printf("File '%s' copied to '%s' successfully.\n", srcFilename, destFilename);
}

void deleteFile() {
char filename[100];

printf("Enter the file name to delete: ");


scanf("%s", filename);

if (remove(filename) == 0) {
printf("File '%s' deleted successfully.\n", filename);
} else {
printf("Error: Could not delete file '%s'. File may not exist.\n", filename);
}
}

Result
Thus a C program for various file operations in C was executed and the output was
obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 36 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY

You might also like