0% found this document useful (0 votes)
8 views48 pages

Programming in C Lab Manual

The document contains multiple examples of C programs demonstrating various programming concepts such as calculating the area of a triangle, finding the largest of two or three numbers, checking leap years, implementing a calculator, verifying Armstrong numbers, and performing operations on arrays and matrices. Each example includes the aim, algorithm, program code, input/output, and a result statement confirming successful execution. These examples serve as practical applications of C programming fundamentals.

Uploaded by

Vijay Chellappan
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)
8 views48 pages

Programming in C Lab Manual

The document contains multiple examples of C programs demonstrating various programming concepts such as calculating the area of a triangle, finding the largest of two or three numbers, checking leap years, implementing a calculator, verifying Armstrong numbers, and performing operations on arrays and matrices. Each example includes the aim, algorithm, program code, input/output, and a result statement confirming successful execution. These examples serve as practical applications of C programming fundamentals.

Uploaded by

Vijay Chellappan
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

EX 1

PROGRAM USING I/O STATEMENTS AND EXPRESSIONS


DATE:

/*I/O STATEMENTS AND EXPRESSIONS*/


/* AREA OF TRIANGLE*/
AIM
To write a C program to find the area of triangle.
ALGORITHM
Step 1: Start the program.
Step 2: Read the value of a, b and c.
Step 3: To calculate
S=(a+b+c)/2
Step 4: To calculate
area=sqrt(s*(s-a)*(s-b)*(s-c))
Step 5: Print area
Step 6: Stop the process

PROGRAM
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float s,d,area;
printf(“\nEnter three sides:”);
scanf(‘”%d%d%d”,&a,&b,&c);
s=(a+b+c)/2;
d=(s*(s-a)*(s-b)*(s-c));
area=sqrt(d);
printf(“\nArea of triangle = %f\n”,area);
}
OUTPUT
Enter three sides: 5 6 7
Area of triangle = 14.696939

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 2
/* DECISION-MAKING CONSTRUCTS*/
DATE:

/*LARGEST OF 2 NUMBERS */

AIM
To create a C program to check the largest number from two numbers.
ALGORITHM
Step 1: Start the program
Step 2: Read the inputs for a and b.
Step 3: Check if a>b then go to step 4, otherwise go to step 5.
Step 4: Print A is greater.
Step 5: Print B is greater.
Step 6: Stop the program.

PROGRAM
#include<stdio.h>
main()
{
int a,b;
printf("Enter values for A and B \n");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("A is largest value \n");
}
else
{
printf("B is largest value \n");
}
}

OUTPUT
Enter values for A and B
55 22
A is largest value

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 3
/* LARGEST OF THREE NUMBERS*/
DATE:

AIM
To create a C program to check the largest number from three numbers.
ALGORITHM
Step 1: Start the program
Step 2: Read the inputs for a,b and c.
Step 3: Check if a>b and a>c then go to step 4, otherwise go to step 5.
Step 4: Print A is greater.
Step 5: Check if b>c then go to step 6, otherwise go to step 7.
Step 6: Print B is greater.
Step 7: Print C is greater.
Step 6: Stop the program.

PROGRAM
#include<stdio.h>
main()
{
int a,b,c;
printf(“\nEnter values for A,B and C:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“\n A is largest value”);
else if(b>c)
printf(“\n B is largest value”);
else
printf(“\n C is largest value”);
}
OUTPUT
Enter values for A,B and C: 5 6 7
C is largest value
Enter values for A,B and C: 7 6 5
A is largest value
Enter values for A,B and C: 5 7 6
B is largest value

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 4
/*LEAP YEAR OR NOT*/

DATE:

AIM
To write a C program for the logic of calculator using switch case.
ALGORITHM
Step 1: Start the program.
Step 2: Get input for year
Step 3: Check if year%400 is equal to 0 then go to step 4 otherwise
go to step 5.
Step 4: Assign leap=1
Step 5: Check if year%100 is equal to 0 then go to step 6 otherwise
go to step 7.
Step 6: Assign leap=0
Step 7: Check if year%4 is equal to 0 then go to step 8 otherwise
go to step 9.
Step 8: Assign leap=1
Step 9: Assign leap=0
Step 10: Check if leap is equal to 1 then go to step 11 otherwise
go to step 12.
Step 11: Print leap year
Step 12: Print not leap year.
Step 13: Stop the program.
PROGRAM
#include<stdio.h>
void main()
{
int year,leap=0;
printf("Enter the Year");
scanf("%d",&year);
if(year%400==0)
{
leap=1;
}
else if(year%100==0)
{
leap=0;
}
else if(year%4==0)
{
leap=1;
}
else
{
leap=0;
}
if(leap==1)
{
printf("%d is a Leap Year",year);
}
else
{
printf("%d is not a Leap Year",year);
}
}
INPUT & OUTPUT
Enter the Year 2000
2000 is a Leap Year
Enter the Year 1999
1999 is not a Leap Year

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 5 /*CALCULATOR PROGRAM USING SWITCH*/

DATE:

AIM
To write a C program for the logic of calculator using switch case.
ALGORITHM
Step 1: Start the program
Step 2: Get input for ‘a’ and ‘b’
Step 3: Print the choices
Step 4: Set loop, execute step 5 to 7 until get input of ‘N’
Step 5: Get input of choice for operation
Step 6: Construct the switch cases for all arithmetic operators.
Step 7: Get input as ‘Y’ or ‘N’
Step 8: Stop the program
PROGRAM:

PROGRAM
#include <stdio.h>
void main()
{
int a,b;
float c;
char ch,choice;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
do
{
printf("Choose operation to perform (+,-,*,/,^): ");
scanf(" %c",&choice);
c=0;
switch(choice)
{
case '+':
c=a+b;
printf("Result: %d %c %d = %f\n",a,choice,b,c);
break;
case '-':
c=a-b;
printf("Result: %d %c %d = %f\n",a,choice,b,c);
break;
case '*':
c=a*b;
printf("Result: %d %c %d = %f\n",a,choice,b,c);
break;
case '/':
c=(float)a/(float)b;
printf("Result: %d %c %d = %f\n",a,choice,b,c);
break;
case '^':

c=a*a;
printf("Result: %d %c %d = %f\n",a,choice,a,c);
break;
default:
printf("Invalid operation.\n");
}
printf("Continue Y/N");
scanf(" %c",&ch);
}while(ch=='y');
}
INPUT & OUTPUT
Enter first number: 5
Enter second number: 2
Choose operation to perform (+,-,*,/,^): -
Result: 5 - 2 = 3.000000
Continue Y/N
Y
Choose operation to perform (+,-,*,/,^): ^
Result: 5 ^ 5 = 25.000000
Continue Y/N

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 6
/*ARMSTRONG NUMBER*/

DATE:

AIM
To write a C program to check whether the given number is an Armstrong or not.
ALGORITHM
Step 1: Start the program
Step 2: Get input for ‘n’
Step 3: Assign temp = n
Step 4: Set loop, check if n> 0
Step 4.1: Calculate r=n%10
Step 4.2: Calculate sum=sum+(r*r*r)
Step 4.3: Calculate n=n/10
Step 5: Check if temp is equal to sum then go to step 5.1 otherwise
go to step 5.2
Step 5.1: Print Armstrong number
Step 5.2: Print Not Armstrong number

PROGRAM
#include<stdio.h>
void main()
{
int n,r,sum=0,temp;
printf("Enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
{
printf("Armstrong Number");
}
else
{
printf("Not a Armstrong Number");
}
}
INPUT & OUTPUT
Enter the number=153
Armstrong Number
Enter the number=150
Not a Armstrong Number

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 7
/*SIMPLE ARRAY PROGRAM*/
DATE:

AIM
To write a C program to manipulate array mechanism.
ALGORITHM
Step 1: Start the program.
Step 2: Get input for array of numbers for the size 5 by using looping statement
Step 3: Print an array of numbers for the size 5 by using looping statement
Step 4: Print an array of numbers for the size 5 by using looping statement in reverse
order
Step 5: Stop the program.

PROGRAM:

PROGRAM
/* SIMPLE ARRAY PROGRAM */
#include<stdio.h>
main()
{
int i,a[5];
printf("Enter the 5 values \n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Array values \n");
for(i=0;i<5;i++)
{
printf("%d \n", a[i]);
}
printf("Array values \n");
for(i=4;i>=0;i--)
{
printf("%d \n", a[i]);
}
}
OUTPUT
Enter the 5 values
10 20 30 40 50
Array values
10
20
30
40
50
Array values
50
40
30
20
10

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 7 /* SUM AND AVERAGE OF N NUMBERS */

DATE:
AIM
To write a C program to find sum and also calculate average of n numbers using array
concept.
ALGORITHM
Step 1: Start the program.
Step 2: Get input for ‘n’
Step 3: Assign i=0
Step 4: Set loop, check if i<n then execute step 4.1 and 4.2 otherwise go to step 5
Step 4.1: Get input for a[i]
Step 4.2: Calculate i=i+1
Step 5: Assign sum=0
Step 6: Set loop, check if i<n then execute step 6.1 and 6.2 otherwise go to step 7
Step 6.1: Calculate sum=sum+a[i]
Step 6.2: Calculate i=i+1
Step 7: Calculate avg=sum/n
Step 8: Print the value of sum and avg
Step 9: Stop the program.

PROGRAM:

#include<stdio.h>
main()
{
int i,n,sum,a[10];
float avg;
printf("Enter the value of N ");
scanf("%d",&n);
printf("Enter the %d values \n", n );
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum = 0;
for(i=0;i<n;i++)
{
sum = sum + a[i];
}
avg = (float) sum / n;
printf("Sum = %d \n" , sum);
printf("Avg = %f \n" , avg);
}

OUTPUT
Enter the value of N 5
Enter the 5 values
10 20 30 40 50
Sum = 150
Avg = 30.000000

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 8
/* SEARCH AN ELEMENT IN THE ARRAY*/
DATE:

AIM
To write a C program to find sum and also calculate average of n numbers using array
concept.
ALGORITHM
Step 1: Start the program.
Step 2: Get input for ‘n’
Step 3: Assign i=0
Step 4: Set loop, check if i<n then execute step 4.1 and 4.2 otherwise go to step 5
Step 4.1: Get input for a[i]
Step 4.2: Calculate i=i+1
Step 5: Get input for searching element as ‘x’
Step 6: Assign flag=0
Step 7: Set loop, check if i<n then execute step 7.1 and 7.2 otherwise go to step 8
Step 7.1: Check if a[i]==x then go to step 7.1.1 otherwise go to step 8
Step 7.1.1: Assign flag=1 and break the loop
Step 7.2: Calculate i=i+1
Step 8: Check if flag==1 then go to step 8.1 otherwise go to step 9
Step 8.1: Print Elements Exist
Step 9: Else
Step 9.1: Print Elements NOT Exist
Step 10: Stop the program.

PROGRAM:

#include<stdio.h>
main()
{
int i,x,n,a[10], flag;
printf("Enter the value of N ");
scanf("%d",&n);
printf("Enter the %d values \n", n );
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value to be searched ");
scanf("%d",&x);
flag = 0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag = 1;
break;
}
}
if(flag == 1)
{
printf("Element Exist");
}
else
{
printf("Element NOT Exist\n");
}
}

OUTPUT
Enter the value of N 5
Enter the 5 values
10 20 30 40 50
Enter the value to be searched 20
Element Exist

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 9
/* MATRIX ADDITION*/
DATE:

AIM
To write a C program to find sum of two matrices using 2D array concept.

ALGORITHM
Step 1: Start the program. Step
2: Get input for ‘n’
Step 3: Get input for first matrix as a[i][j] through nested loop Step 4: Get input for
second matrix as b[i][j] through nested loop Step 5: Calculate c[i][j]=a[i][j]+b[i][j]
through nested loop
Step 6: Print the value of resultant matrix as c[i][j] through nested loop Step 7: Stop the
program.

PROGRAM:

PROGRAM
/* MATRIX ADDITION */
#include<stdio.h>
main()
{
int i,j,n,a[10][10],b[10][10],c[10][10];
printf("Enter the order of the matrix ");
scanf("%d",&n);
printf("Enter elements for A matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements for B matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
printf("The result is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}
}

OUTPUT
Enter the order of matrix 2 Enter the
elements of A matrix 1 2
34
Enter the elements of B matrix 5 6
78
The result is 6
8
10 12

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 8
/* MATRIX MULTIPLCATION*/

DATE:

AIM
To write a C program to find product of two matrices using 2D array concept.

ALGORITHM
Step 1: Start the program. Step
2: Get input for ‘n’
Step 3: Get input for first matrix as a[i][j] through nested loop Step 4: Get input for
second matrix as b[i][j] through nested loop Step 5: Calculate c[i][j]=c[i][j]+a[i]
[k]*b[k][j]through nested loop
Step 6: Print the value of resultant matrix as c[i][j] through nested loop Step 7: Stop the
program.

PROGRAM:

#include<stdio.h>
main()
{
int i,j,k,n,a[10][10],b[10][10],c[10][10];
printf("Enter the order of the matrix ");
scanf("%d",&n);
printf("Enter elements for A matrix \n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements for B matrix \n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The result is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}
}

OUTPUT:

Enter the order of matrix 2 Enter the


elements for A matrix 1 2
34
Enter the elements for B matrix 3 4
56
The result is

13 16
29 36

RESULT

Thus the program executed successfully without any error and required output was verified.
EX 9
/*LENGTH OF THE STRING */

DATE:

AIM
To write a C program to find length of a given string without using predefined string function.
ALGORITHM
Step 1: Start the Program

Step 2: Get input for a string

Step 3: Assign length=0


Step 4: Set loop, check if str[length] != '\0' then execute step 5 otherwise go to step 6. Step 5: Increment length
by 1.
Step 6: Print the value of length
Step 7: Stop the Program

PROGRAM
/*LENGTH OF THE STRING */
#include<stdio.h>
main()
{
int length=0;
char str[25];
printf("Enter the string:");
gets(str); while(str[length] != '\
0')
{
length++;
}
printf("The length of the string %s is %d", str, length);
}

OUTPUT
Enter the string: AREC
The length of the string mailam is 4

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 10
/* STRING COPY */

DATE:

AIM
To write a C program to copy the content from one string to another without using predefined string function.

ALGORITHM
Step 1: Start the Program

Step 2: Get input for a string

Step 3: Assign i=0


Step 4: Set loop, check if str[length] != '\0' then execute
step 5 and 6 otherwise go to step 7.
Step 5: Assign copystr[i] = str[i];
Step 6: Increment length by 1.
Step 7: Print the value of length Step 8:
Stop the Program

PROGRAM
/* STRING COPY */

#include<stdio.h>
main()
{
int i=0;
char str[25], copystr[25];
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
copystr[i] = str[i]; i++;
}
printf("The copied string is %s", copystr);
}

OUTPUT
Enter the string: AREC

The copied string is AREC

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 11
/* COUNT VOWELS AND CONSONANTS */

DATE:

AIM
To write a C program to count number of consonants and vowels of a given string.

ALGORITHM
Step 1: Start the Program

Step 2: Get input for a string

Step 3: Assign i=0


Step 4: Set loop, check if str[length] != '\0' then execute step 5 to 7 otherwise go to step 8.
Step 5: Calculate vowels by increment ‘v’ as 1 after satisfying the condition for vowels Step 6: Calculate
consonants by incrementing ‘c’ as 1 after satisfying the condition for
consonants
Step 7: Increment length by 1.

Step 8: Print the value of length

Step 9: Stop the Program

PROGRAM
/* COUNT VOWELS AND CONSONENTS */
#include<stdio.h>
main()
{
int v=0,c=0,i=0; char
str[25];
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
switch(str[i])
{
case 'a':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'e':
case 'i':
case 'o':
case 'u':
v++;
break;
default:
c++;
} i+
+;
}
printf("The number of vowels are %d\n",v);

printf("The number of consonants are %d",c);


}

OUTPUT
Enter the string: arec

The number of vowels are 2


The number of consonants are 2

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 12
/*FUNCTION TO SWAP TWO NUMBERS */
DATE:

AIM
To write a C program to swap the value for two variables using call by reference.
ALGORITHM
Step 1: Start the Program

Step 2: Get input ‘a’ and ‘b’


Step 3: Print the value for ‘a’ and ‘b’
Step 4: Function call, swap(&a,&b)
Step 5: Print the value for ‘a’ and ‘b’
Step 6: Stop the Program

Procedure for swap(&a,&b)


Step 1: Assign t=*x

Step 2: Assign *x=*y

Step 3: Assign *y=t


PROGRAM
/*FUNCTION TO SWAP TWO NUMBERS */
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
void swap(int*, int*);
printf("Enter values of X and Y \n");
scanf("%d%d",&x,&y); printf("Before
swapping \n"); printf("X=%d \t Y=%d \
n",x,y); swap(&x,&y);
printf("After swapping \n"); printf("X=
%d \t Y=%d \n",x,y);
}

void swap(int *a,int *b)


{
int t;
t=*a;
*a=*b;
*b=t;
}

OUTPUT
Enter the values of X and Y 50
20
Before swapping X=50
Y=20
After swapping X=20
Y=50

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 13

/*FACTORIAL USING RECURSION */


DATE:

AIM
To write a C program to swap the value for two variables using call by reference.
ALGORITHM
Step 1: Start the Program

Step 2: Get input ‘n’


Step 3: Function call, fact(n)
Step 5: Print the value factorial as ‘n’

Step 6: Stop the Program

Procedure for fact(n)


Step 1: Check if n==1 or n==0 then go to

step 2 otherwise go to

step 3. Step 2: return 1


Step 3: Else, return n*fact(n-1)

PROGRAM
/*FACTORIAL USING RECURSION */
#include<stdio.h>
main( )
{
int n;
long f;
long fact(int);
printf("Enter a number to find factorial: ");
scanf("%d",&n);
f = fact(n);
printf("The factorial of %d is %ld \n", n,f);
}

long fact(int n)
{
if((n==1)||(n==0))
{
return 1;
}
else
{
return n*fact(n-1);
}
}

OUTPUT
Enter a number to find factorial: 5 The
factorial of 5 is 120

RESULT
Thus the program executed successfully without any error and required output was verified.
EX 14
/*ARITHMETIC OPERATIONS UNSING POINTER*/

DATE:

AIM
To write a C program to find arithmetic operations using pointers
ALGORITHM
Step 1: Start the Program

Step 2: Assign x=1 and y=2


Step 3: Print the value of ‘x’ and ‘y’
Step 4: Print the address of ‘x’ and ‘y’
Step 5: Assign p = &x
Step 6: Assign y = *p
Step 7: Assign *p = 3
Step 9: Print the value of ‘x’ and ‘y’
Step 9: Stop the Program

/*ARITHMETIC x,y);
OPERATIONS }
UNSING
POINTER*/
#include<stdio.h>
main()
{
int x,y, *p;
x = 1;
y = 2;
printf("x and y \
n");
printf("%d, %d \n",
x,y);

printf("address of x
and y \n");
printf("%d, %d \n",
&x,&y);

p = &x; y = *p;

*p = 3;

printf("x and y \
n");
printf("%d, %d \n",
OUTPUT
X and y
1, 2
address of x and y
2293576, 22935572
x and y
3, 1

RESULT
Thus the program executed successfully without any error and required output was verified.
2
EX 15
/* STUDENT MARKS AND GRADE USING STRUCTURE */
DATE:

AIM
To write a C program to construct structure for students details using array of structure concept.
ALGORITHM
Step 1: Start the Program
Step 2: Construct structure for students with necessary members
Step 3: Get input for all the details of individual students using array of structure Step 4: Calculate total
and average of every students
Step 5: Print the basic details and total, average for every students Step 6: Stop the
Program

PROGRAM
/* STUDENT MARKS AND GRADE USING STRUCTURE */
#include<stdio.h>
struct student
{
int rollno,mark1,mark2,mark3,total; char
name[25],grade;
float avg;
}s[50];

main()
{
int i,n;
printf("\nEnter number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter roll number:");
scanf("%d",&s[i].rollno);
printf("Enter name:");
scanf("%s",s[i].name);
printf("Enter mark 1:");
scanf("%d",&s[i].mark1);
printf("Enter mark 2:");
scanf("%d",&s[i].mark2);
printf("Enter mark 3:");
3
scanf("%d",&s[i].mark3);
s[i].total=s[i].mark1 + s[i].mark2 + s[i].mark3;
s[i].avg=s[i].total/3;
if(s[i].avg>=75)
{
s[i].grade='S';
}
else if((s[i].avg<75)&&(s[i].avg>=60))
{
s[i].grade='A';
}
else if((s[i].avg<60)&&(s[i].avg>=50))
{
s[i].grade='B';
}
else
{
s[i].grade='C';
}
}
printf("Students details\n");
printf("Roll no Name Mark1 Mark2 Mark3 Total Avg Grade\n");
for(i=0;i<n;i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \t %f \t %c \n",
s[i].rollno,s[i].name,s[i].mark1,s[i].mark2,s[i].mark3,s[i].total,s[i].avg,s[i].grade);
}
}

OUTPUT
Enter the number of student: 2 Enter
roll number: 1
Enter name: Ram
Enter mark1: 50
Enter mark2: 60
Enter mark3: 80

Enter roll number: 2


Enter name: Lakshman
Enter mark1: 98
3
Enter mark2: 86
Enter mark3: 95
Roll no Name Mark1 Mark2 Mark3 Total Avg Grade

1 Ram 50 60 80 190 63.000000 A


2 Laksh 98 86 95 279 93.000000 S

RESULT
Thus the program executed successfully without any error and required output was verified.
3

EX 16
/* UNION */

DATE:

AIM
To write a C program to for union concept
ALGORITHM
Step 1: Start the Program
Step 2: Construct union for employee with necessary members
Step 3: Get input and print the data for all the details of individual employee. Step 4: Stop the
Program

PROGRAM
/* UNION */
#include<stdio.h>
union employee
{
int id;
char name[20];
float salary;
}emp;

main()
{
printf("Enter the employee id: ");
scanf("%d",&[Link]);
printf("Employee ID is %d \n\n",[Link]);
printf("Enter the employee name:"); scanf("%s",
[Link]);
printf("Employee Name is %s \n\n",[Link]);
printf("Enter the employee Salary:");
scanf("%f",&[Link]);
printf("Employee Salary is %f \n\n",[Link]);
//Error - Overwritten Data
printf("Overwritten Employee ID is %d",[Link]);
}

OUTPUT
Enter the employee id: 1000
Employee ID is 1000
3
Enter the employee name: Kumar
Employee Name is Kumar
Enter the employee Salary: 50000
Employee Salary is 50000
Overwritten Employee ID is 1195593728

RESULT
Thus the program executed successfully without any error and required output was verified.
3
EX 17
/*SALARY SLIP OF EMPLOYEE USING STRUCTURES AND POINTERS*/

DATE:

AIM
To write a C program to prepare Salary Slip of Employee using structures and pointers.

ALGORITHM
Step 1: Start the program.
Step 2: Construct employee structure
Step 3: Declare pointer variable for structure
Step 4: Get input for empid, empname and basic data for salary.

Step 5: Calculate total=ptr->bp+ptr->hra+ptr->da.


Step 6: Calculate salary=total-deduction.
Step 7: Print salary
Step 8: Stop the program.

PROGRAM
#include<stdio.h>
struct employee
{
int empid;
char empname[20]; int
bp;
int hra;
int da;
int pf;
int lic;
};
void main()
{
struct employee e,*ptr; int
salary,total,deduction; ptr=&s;
printf("Enter the Employee Number\n");
scanf("%d",&ptr->empid);
printf("Enter the Employee Name\n");
scanf("%s",ptr->empname); printf("Enter the
Employee Basic Pay\n"); scanf("%d",&ptr->bp);
printf("Enter the Employee HRA\n");
scanf("%d",&ptr->hra); printf("Enter the
3
Employee DA\n"); scanf("%d",&ptr->da);
printf("Enter the Employee PF Amount\n");
scanf("%d",&ptr->pf);
printf("Enter the Employee LIC Amount\n");
scanf("%d",&ptr->lic);
total=ptr->bp+ptr->hra+ptr->da;
deduction=ptr->pf+ptr->lic; salary=total-
deduction;
printf("Employee Total Salary=%d \n",salary);
}

INPUT & OUTPUT


Enter the Employee Number 101
Enter the Employee Name
satheesh
Enter the Employee Basic Pay 6000
Enter the Employee HRA
1500
Enter the Employee DA 500
Enter the Employee PF Amount 500
Enter the Employee LIC Amount 200
Employee Total Salary=7300

RESULT
Thus the program executed successfully without any error and required output was verified.
3

EX 18
/* STUDENT INTERNAL MARK CALCULATION*/
DATE:

AIM
To write a C program to calculate internal mark calculation using structure concept.

ALGORITHM
Step 1: Start the program.
Step 2: Construct structure of student with necessary particulars.

Step 3: Get input for rollno, name, m1, m2, m3, m4 and m5.
Step 4: Call calculation()
Step 5: Stop the program.

Procedure for calculation()


Step 1: Check if(m1>=50&&m2>=50&&m3>=50&&m4>=50&& m5>=50) then
execute step 2 to step 6 otherwise
go to step 7.
Step 2: Print Result=Pass
Step 3:Calculate total=m1+m2+m3+m4+m5

Step 4:Print the value of total


Step 5:Calculate percentage=total/5
Step 6: Print the value of percentage.
Step 7: Print the result=Fail

PROGRAM
#include<stdio.h>
struct student
{
int rollno;
char name[20]; int
m1;
int m2;
int m3;
int m4;
int m5;
};
void calculation(int,int,int,int,int); void
3
main()
{
struct student s;
printf("Enter the Student Number\n");
scanf("%d",&[Link]);
printf("Enter the Student Name\n");
scanf("%s",[Link]);
printf("Enter the Student Mark1\n");
scanf("%d",&s.m1);
printf("Enter the Student Mark2\n");
scanf("%d",&s.m2);
printf("Enter the Student Mark3\n");
scanf("%d",&s.m3);
printf("Enter the Student Mark4\n");
scanf("%d",&s.m4);
printf("Enter the Student Mark5\n");
scanf("%d",&s.m5);
calculation(s.m1,s.m2,s.m3,s.m4,s.m5);
}
void calculation(int m1,int m2,int m3,int m4,int m5)
{
float percentage; int
total;
if(m1>=50&&m2>=50&&m3>=50&&m4>=50&&m5>=50)
{

printf("Result=Pass \n");
total=m1+m2+m3+m4+m5; printf("Total=%d\
n",total); percentage=total/5; printf("Percentage=
%f",percentage);
}
else
{
printf("Result=Fail");
}
}

INPUT & OUTPUT


Enter the Student Number 101
Enter the Student Name
Suresh
3
Enter the Student Mark1 60
Enter the Student Mark2 70
Enter the Student Mark3 80
Enter the Student Mark4 75
Enter the Student Mark5 65
Result=Pass
Total=350
Percentage=70.000000

RESULT
Thus the program executed successfully without any error and required output was verified.
3

EX :19
/*RANDOM ACCESS FILE – TELEPHONE DETAILS*/
DATE:

AIM
To write a C Program to add, delete , display ,Search and exit options for telephone details of an
individual into a telephone directory using random access file.

ALGORITHM
Step 1: Start the program.
Step 2: Declare variables, File pointer and phonebook structures.

Step 3: Create menu options.


Step 4: Read the option.
Step 5: Develop procedures for each option.
Step 6: Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.

Step 7: Display the message for operations performed.


Step 8: Stop the program.

PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20]; char
LastName[20]; char
PhoneNumber[20];
} phone;

void AddEntry(phone * ); void


DeleteEntry(phone * ); void
PrintEntry(phone * );
void SearchForNumber(phone * ); int
counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite; int
main (void)
{
4
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100); int
iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit"); return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu"); printf("\n\n\t(1)\tAdd
Friend"); printf("\n\t(2)\tDelete Friend"); printf("\n\t(3)\
tDisplay Phonebook Entries"); printf("\n\t(4)\tSearch
for Phone Number"); printf("\n\t(5)\tExit Phonebook");

printf("\n\nWhat would you like to do? ");


scanf("%d", &iSelection);

if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);
}
if (iSelection == 3)
{
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n"); return 0;
}
} while (iSelection <= 4);
}

void AddEntry (phone * phonebook)


{
4
pWrite = fopen("phonebook_contacts.dat", "a"); if ( pWrite
== NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName); printf("Phone
Number (XXX-XXX-XXXX): "); scanf("%s", phonebook[counter-
1].PhoneNumber); printf("\n\tFriend successfully added to
Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-
1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}

void DeleteEntry (phone * phonebook)


{
int x = 0;
int i = 0;
char deleteFirstName[20]; // char
deleteLastName[20]; printf("\nFirst
name: "); scanf("%s",
deleteFirstName); printf("Last name:
"); scanf("%s", deleteLastName); for
(x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter -1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
4
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}

void PrintEntry (phone * phonebook)


{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r"); if ( pRead
== NULL)
{
perror("The following error occurred: ");
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName,
phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);

}
}
fclose(pRead);
}

void SearchForNumber (phone * phonebook)


{
int x = 0;
char TempFirstName[20]; char
TempLastName[20];

printf("\nPlease type the name of the friend you wish to find a number for.");

printf("\n\nFirst Name: ");


4
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName); for (x
= 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n",
phonebook[x].FirstName, phonebook[x].LastName,
phonebook[x].PhoneNumber);
}
}
}
}

OUTPUT
Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook

What would you like to do?1 First


Name: Ram
Last Name: Mohan
Phone Number (XXX-XXX-XXXX): 717-675-0909
Friend successfully added to Phonebook

Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do?5
You have chosen to exit the Phonebook.

RESULT
Thus the program executed successfully without any error and required output was verified.
4
EX :20
/*SEQUENTIAL ACCESS FILE – BANK DETAILS*/

DATE:

AIM
To write a C Program to Count the number of account holders whose balance is less than the minimum
balance using sequential access file.

ALGORITHM
Step 1: Start the program.
Step 2: Declare variables and file pointer.

Step 3: Display the menu options.


Step 4: Read the Input for transaction processing.
Step 5: Check the validation for the input data.
Step 6: Display the output of the calculations.
Step 7: Repeat step 3 until choose to stop.
Step 8: Stop the program.

PROGRAM
/* Count the number of account holders whose balance is less than the minimum balance using sequential access
file.*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10]; char
name[20];
char balance[15];
}acc;

void main()
{
long int pos1,pos2,pos; FILE
*fp;
char *ano,*amt;
4
char choice;
int type,flag=0;
float bal;
do
{
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum Balance\n");
printf("5. Stop\n"); printf("Enter
your choice : "); choice=getchar();
switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("[Link]","a");
printf("\nEnter the Account Number : ");
gets([Link]);
printf("\nEnter the Account Holder Name : ");
gets([Link]);
printf("\nEnter the Initial Amount to deposit : ");
gets([Link]);
fseek(fp,0,2); fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("[Link]","r"); if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder Name Balance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t%-20s\t%s\n",
[Link],[Link],[Link]); fclose(fp);
}
break;
case '3' :
fflush(stdin);
flag=0;
fp=fopen("[Link]","r+"); printf("\nEnter the
Account Number : "); gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
4
{
if(strcmp([Link],ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 for withdraw
: ");
scanf("%d",&type);
printf("\nYour Current Balance is : s",[Link]);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
gets(amt);
if(type==1)
bal = atof([Link]) + atof(amt);
else
{
bal = atof([Link]) -atof(amt); if(bal<0)
{
printf("\nRs.%s Not available in your A/c\
n",amt);
flag=2;
break;
}
}
flag++;
break;
}
}
if(flag==1)
{
pos2=ftell(fp); pos =
pos2-pos1; fseek(fp,-
pos,1);
sprintf(amt,"%.2f",bal);
strcpy([Link],amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again"); fclose(fp);
break;
case '4' :
fp=fopen("[Link]","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
4

bal = atof([Link]);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum
Balance : %d",flag);
fclose(fp);
break;
case '5' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....................");
} while (choice!='5');
}
4

OUTPUT
1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
Enter your choice : 1
Enter the Account Number : 547898760 Enter the
Account Holder Name : Rajan Enter the Initial
Amount to deposit :2000

Press any key to continue....


1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop

Enter your choice : 4


The Number of Account Holder whose Balance less than the Minimum Balance : 0

RESULT
Thus the program executed successfully without any error and required output was verified.

You might also like