c Lab Manual Final Copy
c Lab Manual Final Copy
LAB MANUAL
PEO 1: Graduates shall have professional competency in the field of Computer Science and
Engineering for pursuing higher education, research or as entrepreneurs.
PEO 2: Graduates shall work in a business environment with ethical standards, leadership
qualities and communication necessary for engineering principles.
PEO 3: Graduates shall adapt to emerging technologies and respond to the challenges of the
environment and society forever.
PSO 2: To apply software engineering principles and practices for developing quality
software for scientific and business applications.
PROGRAMME OUTCOMES
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
CS3102 PROGRAMMING FOR PROBLEM SOLVING
USING C LABORATORY LTPC
0042
COURSE OBJECTIVES:
LIST OF EXPERIMENTS
COURSE OUTCOMES:
At the end of the course, students would develop C programs with
• Simple applications making use of basic constructs
• Control statements.
• Involving Arrays, strings and pointers.
• Involving functions, and structures.
• Sequential and random access files processing.
S.A. ENGINEERING COLLEGE, CHENNAI 600 077
(An Autonomous Institution, Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COURSE OBJECTIVES
● To develop programs in C using basic constructs.
● To develop programs in C using control statements.
● To develop applications in C using arrays, strings, pointers.
● To develop applications in C using functions, structures.
● To develop applications in C using file processing
COURSE OUTCOMES
CS3102.1: Develop C programs for simple applications making use
of basic constructs.
CS3102.2: Develop C programs for control statements.
CS3102.3: Develop C programs involving arrays, strings and pointers.
CS3102.4: Develop C programs involving functions, and structures.
CS3102.5: Design applications using sequential and random access file processing.
CS3102 PROGRAMMING FOR PROBLEM SOLVING USING C
LABORATORY (CO-PO ,PSO MAP)
CO/P PO PO PO PO PO PO PO PO PO PO PO PO PS PS PS
O 1 2 3 4 5 6 7 8 9 10 11 12 O1 O2 O3
CO1 2 2 2 2 1 1 2 1
CO2 2 2 2 2 2 2 1
CO3 2 3 2 1
CO4 3 2 1
CO5 1 1 1 1 1 1 2 1
CO6 2 2 2 1
Radius->integer
Step 3: Get the radius.
void main()
float radius,area;
printf("Enter the Radius of the circle:\n");
scanf("%f",&radius);
printf(“\nradius=%f”,radius);
area=3.14*radius*radius;
printf("\nArea=%f",area);
getch();
clrscr();
}
OUTPUT:
Area=78.500000
RESULT:
PROGRAM-1:
#include <stdio.h>
void main()
char ch;
printf(“\nEnter any character : “);
ch = getchar();
PROGRAM-2:
#include<stdio.h>
void main()
char ch;
ch = getch();
putchar(ch);
OUTPUT:
PROGRAM-3:
#include <stdio.h>
void main()
{
char name[30];
gets(name);
puts(name);
OUTPUT:
www.saec.ac.in
PROGRAM-4:
#include <stdio.h>
int main()
int num;
printf(%d”,num);
return 0;
OUTPUT:
C Programming
5
5
RESULT:
AIM: To write a C program to find the size of variables using sizeof function.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
C PROGRAM
#include<stdio.h>
int main()
int intType;
float floatType;
double doubleType;
char charType;
OUTPUT:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
RESULT:
ALGORITHM:
C PROGRAM
#include <stdio.h>
int main()
{
char c;
return 0;
}
OUTPUT:
Enter a character: G
ASCII value of G = 71
ALGORITHM: -
x = x + y;
y = x - y;
x = x - y;
#include<stdio.h>
int main()
int x, y;
printf("Enter the value of x and y?");
scanf("%d %d",&x,&y);
printf("before swapping numbers: %d %d\n",x,y);
/*swapping*/
x = x + y;
y = x - y;
x = x - y;
printf("After swapping: %d %d",x,y);
return 0;
22
After swapping: 22 13
RESULT:
Thus, the program was compiled and executed successfully.
EX.NO: 1F ILLUSTRATION OF UNARY OPERATORS
ALGORITHM:
#include <stdio.h>
int main()
int a, unaryMinus;
unaryMinus=-(a);
OUTPUT:
-56
#include <stdio.h>
int main()
int a, pre_increment,post_increment;
scanf("%d",&a);
//take temp variable for showing actual
number in output int temp=a;
pre_increment=++a;
//displaying output
OUTPUT:
10
Pre increment operation of 10 is =11
RESULT:
AIM:
To write the c Program to generate pascal’s triangle.
ALGORITHM
PROGRAM:
#include <stdio.h>
int main()
int n,row,r,space,ncr;
printf("enter the row value");
scanf("%d",&row);
for(n=0;n<row;n++)
for(space=1;space<row-n;space++)
{ printf(" "); }
for(r=0;r<=n;r++) {
if(n==0||r==0)
{ ncr=1;
printf("%d ",ncr);
}
else
{ ncr=ncr*(n-r+1)/r;
printf("%d ",ncr);
}
} printf("\n");
}
return 0;
OUTPUT:
AIM:-
ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
Step 3: Get the Number.
Step 4: Read the choice to perform the operation using switch case.
Step 5: Print the result.
Step 6: Stop.
PROGRAM:-
#include<stdio.h>
#include<conio.h>
void main()
int a,b,choice;
printf("Sum of %d and %d is :
%d",a,b,a+b); break;
case 2 :
printf("Difference of %d and %d is :
%d",a,b,a-b); break;
case 3 :
printf("Multiplication of %d and %d is :
%d",a,b,a*b); break;
case 4 :
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
clrscr();
}
OUTPUT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Sum of 7 and 2 is : 9
RESULT:
AIM:-
ALGORITHM:-
n,r,sum->integer.
PROGRAM:-
#include<stdio.h>
#include<conio.h>
void main()
while(number>0)
{
r=number%10;
sum=sum+r;
number=number/10;
}
getch();
clrscr();
}
OUTPUT :-
Enter the number: 92
Sum of digits=11
RESULT:-
Thus the sum of digits using C program has been executed and verified
successfully
AIM:
Step 1: Start
Step 2: Declare variable n, first, second, next, c;
Step 6: Repeat until c < n : next = first + second; first = second; second = next;
Step 7: Stop
Program:
#include<stdio.h>
main()
{
Output:
01123
Result:
Aim:
Step 6:stop
program:
#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range: \n");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
}
Output:
2 3 5 7 11 13 17 19 23 29
Result:
AIM
Step6: If the number searched is found, it will return the position. If,
not will return the searched element is not found
#include <stdio.h>
#include<conio.h>
int main()
} }
if (c == n)
printf("%d isn't present in the array.\n", search);
getch();
return 0;
} OUTPUT
RESULT:
AIM
To write the C program to find both the largest and smallest number
in a list of integers
ALGORITHM
Step1: Start the program
Step2: Declare the variables with respective data type.
Step3: Get the number of elements and get the elements for
the array given Step4: Compare the elements in the array
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,n,large,small;
clrscr();
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
getch();
return 0;
OUTPUT
RESULT:
Thus the program to find both the largest and smallest number in a list of
integers is executed successfully.
AIM:
To write the C program to perform the
addition of matrix
ALGORITHM:
PROGRAM
#include <stdio.h>
#include<conio.h>
int main() {
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
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");
} }
getch();
return 0;
OUTPUT
RESULT:
AIM
To Write the C program to perform
multiplication of matix ALGORITHM
PROGRAM
#include <stdio.h>
# include<conio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
printf("\n");
}
}
getch();
return 0;
}
OUTPUT
RESULT
ALGORITHM
Step1: Start the program
Step2: Declare the variables with their respective data type
Step3: Get the number of elements for the array and get the values
for the array given Step4: Compare the adjacent elements and do
swapping if needed. Step5: Once all the elements are in sorted
order, print the sorted list of elements Step6: Stop the program
PROGRAM
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;
clrscr();
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
} printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
return 0;
OUTPUT
RESULT
Thus the program for arranging the elements in sorted order using
Bubble sort is executed successfully.
EX NO 4 A) WRITE A C PROGRAM THAT USES FUNCTIONS TO
PERFORM THE FOLLOWING OPERATIONS
ALGORITHM:
Step 1: Start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string(p)
Step 7: copy sub string into main string from position p - 1
Step 8: copy temporary string into main string from position p + n - 1
Step 9: print the strings
Step 10: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
sachin
tendulkar
Thus the a sub-string in to a given main string from a given position has
been inserted using C program and verified successfully
ALGORITHM:
Step 1: Start
Step 2: read string
Step 3: find the length of the string
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();
sachin
Enter the position where characters are to be deleted
RESULT:-
Thus the n Characters from a given position in a given string has been
deleted using C program and verified successfully
EX NO 4 B) PALINDROME OR NOT
AIM: To Write a C - program to determine if the given string is palindrome or not
ALGORITHM:
Step 1: start
Step 2: read string A
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
clrscr();
else
printf("The string isn't a palindrome.\n");
getch();
return 0;
OUTPUT:
RESULT: Thus the given string whether palindrome or not has been executed using
C program and verified successfully.
ALGORITHM:
Main Program
Step 1: start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop
Sub Program:
Program
#include <stdio.h>
int n,result;
printf("Enter a positive integer: ");
scanf("%d", &n);
result=factorial(n);
printf("Factorial of %d = %d", n,result);
return 0;
int temp ;
if( n == 0)
return 1 ;
else
return temp ;
output:
RESULT:-
Thus the given number’s factorial value using C program has been executed
and verified successfully.
EX NO:-5B TOWERS OF HANOI
AIM:- To solve tower of Hanoi problem using recursion in C with Turbo C IDE.
ALGORITHM:-
1. START
3. STOP
PROGRAM:-
#include <stdio.h>
#include<conio.h>
void main()
if (n == 1)
{
}
hanoi(n-1, from_rod, aux_rod, to_rod);
RESULT:-
Algorithm:
Main Function
Step 1: Start
Step 2: Declare the sub function
Step 8: Stop
Sub Function
Step 1: initialize a, b, temp
Step 2: swap the value, a to temp, b to a, temp to b
Program:
#include <stdio.h>
#include<conio.h>
void swap(int,int) ;
void main()
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1,num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
int temp ;
temp = a ;
a=b;
b = temp ;
Output:
Before swap: num1 = 10, num2 = 20
Result: Thus the program to swap the variables using call by value in C
programming is developed and executed successfully.
Algorithm:
Main Function
Step 1: Start
Step 2: Declare the sub function
Step 3: Declare variables num1, num2
Step 4: Read values num1, num2
#include<conio.h>
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1,&num2) ;
getch() ;
int temp ;
temp = *a ;
*a = *b ;
*b = temp ; }
OUTPUT:
Result: Thus the program to swap the variables using call by reference in C
programming is developed and executed successfully.
AIM: Write a C program to generate marksheet of students and compute grade for
five different subjects using structures.
ALGORITHM:-
1. Start the program
#include<stdio.h>
#include<conio.h>
struct mark_sheet
{ char name[20];
float average;
char res[10];
char cl[20];
}students[100];
int main() {
int a,b,n,flag=1;
char ch;
clrscr();
scanf("%d",&n);
for(a=1;a<=n;++a){
clrscr();
scanf("%s", students[a].name);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b)
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
students[a].average = (float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");
else if((students[a].average>=60)&&(flag==1)) strcpy(students[a].cl,"First
Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
for(a=1;a<=n;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
for(b=1;b<=5;b++)
{
printf("\n\n------------------------------------------------------------------------\n");
OUTPUT:-
RESULT:-
ALGORITHM:-
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct employee {
int empId;
char name[32];
int pf;
printf("daily allowance:%d",e1.da);
return;
void main()
clrscr();
scanf("%d", &num);
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
printf("Employee Name:");
scanf("%s",e1[i]);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary");
scanf("%d", &(e1[i].basic));
scanf("%d", &(e1[i].hra));
scanf("%d", &(e1[i].da));
scanf("%d",&e1[i].pf);
printf("\n");
}
for (i = 0; i < num; i++)
e1[i].net = e1[i].gross-(e1[i].basic*e1[i].pf/100) ;
while (1)
scanf("%d", &empID);
flag = 0;
if (empID == e1[i].empId)
printSalary(e1[i]);
flag = 1;
if (!flag)
scanf("%d", &ch);
if (!ch)
break;
}
getch();
OUTPUT:-
Provident fund : 14
Gross salary : 10168.00 Rupees
Net salary : 10079.00 Rupees
Do you want to continue(1/0) : 0
RESULT:-
Thus the program to generate the payslip of the employees using structure and
functions using C programming is developed and executed successfully.
AIM:
ALGORITHM:
Step 3: To insert a record in random file create a file pointer and get
necessary details from user.
Step 4: Using SEEK command move file pointer position to the desired
location and perform file write operation
Step 6: To delete a record get the record number from the user. If it is
matched in the record number in the file then the details are deleted
PROGRAM:-
#include "stdio.h"
#include "string.h"
struct dir
char name[20];
char number[10];
};
int record = 0;
int main(void)
{
int choice = 0;
switch(choice)
{
default: ;
fclose(fp);
return 0;
}
scanf("%s", contact.number);
fwrite(&contact, sizeof(struct
dir), 1, fp); }
int result;
FILE *fptr;
fptr=fopen("data4.dat","a+");
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fptr))
printf("Enter number:");
scanf("%s",number);
strcpy(contact.number,number);
fclose(fptr);
return;
}
void del(FILE *fp)
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
return;
}
record++;
rewind(fp);
printf("*************************
******\n"); }
void search(FILE *fp)
rewind(fp);
printf("\nEnter name:");
scanf("%s", name);
while(!feof(fp))
{
OUTPUT:-
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
*******************************
bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 5
Enter name: bb
bb 11111
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 1
Enter
individual/company
name:
aa Enter telephone
number:
222222
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 2
Enter name: aa
Updated successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
**********************
********* bb 11111
aa 333333
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 3
Enter name: aa
1 Deleted successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
**********************
********* bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 6
RESULT:
Aim:
● If the position is last, then move the current node to previous and
make its next as null.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);
struct Node
{ int data;
}*head = NULL;
void main()
{ int choice,value,choice1,loc1,loc2;
clrscr();
while(1){
scanf("%d",&choice);
switch(choice)
{ case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
while(1){
scanf("%d",&choice1);
switch(choice1)
{ case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
insertBetween(value,loc1,loc2);
break;
goto mainMenu;
}
goto subMenuEnd;
subMenuEnd:
break;
case 2: display();
break;
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd(value);
break;
scanf("%d",&loc2);
removeSpecific(loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
newNode->next = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
} }}
void removeEnd()
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
temp2 = temp1;
temp1 = temp1->next;
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}
void removeSpecific(int delValue)
}
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = temp1 -> next;
free(temp1);
printf("\nOne node deleted!!!\n\n");
functionEnd:
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
Output:
Result: Thus the C Program to implement List using Linked list has been
executed successfully.