CP Lab
CP Lab
PANDUR, THIRUVALLUR
IIET-CSE-THIRUVALLUR
INDIRA INSTITUTE OF ENGINEERING &TECHNOLOGY
Pandur, Thiruvallur – 631 203
CERTIFICATE
Certified to be bonafide record of work done by _______________________ on 2ND
IIET-CSE-THIRUVALLUR
INDEX
PAGE. STAFF
EXP.NO DATE EXPERIMENT NAME
NO SIGNATURE
IIET-CSE-THIRUVALLUR
EXP No. 1
Programs Using I/O Statements And Expressions
Write a C program to find the area and circumference of the circle.
Aim
Algorithm
Source Code
#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output
Enter radius of circle :6
Area of circle :113.040001
Circumference :37.680000
Result
IIET-CSE-THIRUVALLUR
EXP No.
2(a) Decision making constructs (If-else, goto,switch-case, break-
continue)
Write a program in C using If-else to determine Whether a person is Eligible for Voting
or Not
Aim
Algorithm
Source Code :
#include<stdio.h>
#include<conio.h
int main()
{
int a,b,c;
printf(“Enter the three numbers: “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
printf(“The biggest number is: %d”,a);
else if(b>a&&b>c)
printf(“The biggest number is: %d”,b);
else
printf(“The biggest number is: %d”,c);
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output:
Enter the three numbers:
758
The biggest number is: 8
Result:
IIET-CSE-THIRUVALLUR
EXP No.
2(b) Decision making constructs (If-else, goto, switch-case,
break-continue)
Write a C program to find and print the present Elevator position using switch case.
Aim
Algorithm
Source Code :
#include <stdio.h>
int main()
{
int a;
printf("Enter Floor No : ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\nFirst Floor");
break;
case 2:
printf("\nSecond Floor");
IIET-CSE-THIRUVALLUR
break;
case 3:
printf("\nThird Floor");
break;
default:
printf("Invalid No");
break;
}
return 0;
}
Output :
Enter Floor No : 3
Third Floor
Result.
IIET-CSE-THIRUVALLUR
EXP No.
2(c) Decision making constructs (If-else, goto, switch-case,
break-continue)
Note : If any input is Zero, program should take the input again, if any input is negative
the loop should be terminated.
Aim
Algorithm
IIET-CSE-THIRUVALLUR
Source Code :
#include <stdio.h>
int main()
{
int number;
while(1)
{
printf("Enter integer number: ");
scanf("%d",&number);
if(number==0)
{
printf("Invalid input...\n");
continue;
IIET-CSE-THIRUVALLUR
}
else if(number<0)
{
printf("Terminating loop...\n");
break;
}
printf("Number is: %d\n",number);
}
printf("Bye, Bye...\n");
return 0;
}
Output :
Enter integer number : 10
Number is : 10
Result.
IIET-CSE-THIRUVALLUR
EXP No.
3(a)
Loops : for, while, do-while
Aim
Algorithm
Source Code.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, a ,f = 1;
printf("Enter a number:\n");
scanf("%d", &a);
for (i = 1; i <= a; i++)
f=f*i;
printf("Factorial of %d is %d\n", a, f);
getch();
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.
Enter a number : 5
Factorial of 5 is 120.
Result.
IIET-CSE-THIRUVALLUR
EXP No.
3(b)
Loops : for, while, do-while
Aim
Algorithm
IIET-CSE-THIRUVALLUR
Source Code
}
Temp = Number;
while( Temp > 0)
{
}
if ( Number == Sum )
Output :
Please Enter number to Check for Armstrong
153
153 is Armstrong Number.
Result.
IIET-CSE-THIRUVALLUR
EXP No.
4(a)
Arrays : 1D and 2D, Multi-dimensional arrays, Traversal
Aim
.
Algorithm
Source Code
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, i, j;
int temp;
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nArray elements after sorting:\n");
for (i = 0; i < n; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
Output.
Enter total number of elements : 5
Result
IIET-CSE-THIRUVALLUR
EXP No.
4(b)
Arrays : 1D and 2D, Multi-dimensional arrays, Traversal
Aim:
Algorithm:
Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("\nEnter the elements of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\n\nEnter the Elements of Matrix B\n");
IIET-CSE-THIRUVALLUR
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]= a[i][j] + b[i][j];
}
}
printf("\n\nThe sum of the two given matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
Output:
IIET-CSE-THIRUVALLUR
EXP No. 5
Strings : Operations
Aim
Algorithm
Source Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[str_size];
int i, len, vowel, cons;
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
printf("\n\nCount total number of vowel or consonant :\n");
printf(" \n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
vowel = 0;
cons = 0;
len = strlen(str);
IIET-CSE-THIRUVALLUR
Output
Count total number of vowel and consonant :
Result
IIET-CSE-THIRUVALLUR
EXP No. 6
Functions: Call, Return, Passing Parameters By (Value, Reference),
Passing Arrays To Function.
Aim
.
Algorithm
Source Code
#include <stdio.h>
void swap(int*, int*); //Swap function declaration
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
swap(&x, &y);
return 0;
}
//Swap function definition
void swap(int *a, int *b)
{
int t;
t = *b;
*b = *a;
*a = t;
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output
Before Swapping
x =45
y = 90
After Swapping
x=90
y=45
Result
IIET-CSE-THIRUVALLUR
EXP No. 7
Recursion
Aim
.
.
Algorithm
Source Code
#include<stdio.h>
#include<conio.h>
void main()
int n;
long int fact();
clrscr();
printf("enter the number whose factorial is
to be found"); scanf("%d",&n);
printf("the factorial of %d is: %d
\n",n,fact(n)); getch();
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
long int fact(n)
{
int n;
if(n==0)
return(1);
else
return(n*fact(n-1);
}
Output:
enter the number whose factorial is to
be found:6 The factorial of 6 is: 720
Result:
IIET-CSE-THIRUVALLUR
EXP No.
8(a) Pointers : Pointers to functions, Arrays, Strings, Pointers to Pointers,
Arrays of Pointers
Write a C program to compute the sum of all elements stored in an array using Pointers
Aim
Algorithm
Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,sum=0;
int*p;
printf("Enter 10 elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<10;i++)
{
sum=sum+*p;
p++;
}
printf("The sum of array elements is %d",sum);
}
IIET-CSE-THIRUVALLUR
Output
Enter 10 elements :
1
2
3
4
5
6
7
8
9
12
The sum of array elements is 57
Result
IIET-CSE-THIRUVALLUR
EXP No.
8(b) Pointers : Pointers to functions, Arrays, Strings, Pointers to
Pointers, Arrays of Pointers
Aim
Algorithm
Source Code
#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
length = string_ln(str);
printf("The length of the given string %s is : %d", str,
length); getch();
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.
Enter any string:
Technology
The length of the given string Technology is : 10
Result.
IIET-CSE-THIRUVALLUR
EXP No.
9(a) Structures : Nested Structures, Pointers to Structures, Arrays of
Structures and Unions
Write a C program that gets and display the report of the student details using Nested
Structures
Aim
Algorithm
Source Code.
IIET-CSE-THIRUVALLUR
{
struct student stud;
return 0;
}
Output.
Enter name and roll number of student:
Ramesh
17
Enter street name, house number and state number:
Jawagarnagar
23
45
Result
IIET-CSE-THIRUVALLUR
EXP No.
9(b) Structures : Nested Structures, Pointers to Structures, Arrays of
Structures and Unions
Write a 'C' program to display the student Name, Marks and Percentage using Union
Aim..
Algorithm.
Source Code.
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
// assigning values to record1 union variable
printf("Union record1 values example\n");
strcpy(record1.name, "Raju");
printf(" Name : %s \n", record1.name);
IIET-CSE-THIRUVALLUR
strcpy(record1.subject, "Maths");
printf(" Subject : %s \n", record1.subject);
record1.percentage = 86.50;
printf(" Percentage : %f \n\n", record1.percentage);
Output.
Union record1 values example
Name : Raju
Subject : Maths
Percentage : 86.500000
Result.
IIET-CSE-THIRUVALLUR
EXP No.
10(a) Files : reading and writing, File pointers, file operations,
random access, processor directives.
Aim.
Algorithm
Source Code.
IIET-CSE-THIRUVALLUR
fp=fopen(fName,"w");
putc('B',fp);
putc('C',fp);
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
printf("Contents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
}
IIET-CSE-THIRUVALLUR
Output.
Enter file name to create : program.txt
ABC
Result.
IIET-CSE-THIRUVALLUR
EXP No.
10(b) Files : reading and writing, File pointers, file operations,
random access, processor directives.
Aim.
Algorithm.
IIET-CSE-THIRUVALLUR
Source Code.
#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];
IIET-CSE-THIRUVALLUR
FILE *pRead;
FILE *pWrite;
void main (void)
{
phone *phonebook;
int iSelection=0;
phonebook = (phone*)
malloc(sizeof(phone)*100); //int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
}
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);
IIET-CSE-THIRUVALLUR
}
if (iSelection == 3)
{
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
getch();
}
} while (iSelection<= 4);
}
void AddEntry (phone * phonebook)
{
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);
IIET-CSE-THIRUVALLUR
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);
IIET-CSE-THIRUVALLUR
}
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);
}
}
IIET-CSE-THIRUVALLUR
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: ");
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);
}
}
}
}
IIET-CSE-THIRUVALLUR
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 : Arun
1. Add Friend
2. Delete Friend
3. Display Phonebook Entries
4. Search for Phone Number
5. Exit Phonebook
Result.
IIET-CSE-THIRUVALLUR
CONTENT BEYOND SYLLABUS
IIET-CSE-THIRUVALLUR
EXP No.
11(a)
PRODUCT OF TWO MATRICES
Aim.
Algorithm:
Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
printf("\nEnter the elements of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &a[i][j]);
IIET-CSE-THIRUVALLUR
}
}
printf("\n\nEnter the Elements of Matrix B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
printf("\nThe Product of the Two Matrices are\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter the elements of A
1 2 34 5 67 8 9
Enter the Elements of Matrix B
9 8 76 5 43 2 1
The Product of the Two Matrices are
30 24 18
84 69 54
138 114 90
IIET-CSE-THIRUVALLUR
Result:
IIET-CSE-THIRUVALLUR
EXP No. DATA MANIPULATION FILE HANDLING SEQUENTIAL ACCESS
11(b)
Aim
Algorithm.
Source Code.
Use fprintf() to write a text file:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Welcome “!\n\n");
char character;
FILE *fpointer;
fpointer = fopen("C:\\program.txt","w"); // Here "w" refers to write on file
if(fpointer == NULL)
{
printf("Error! The file does not exist.");
exit(0);
}
printf("Enter a character: ");
scanf("%c",&character);
return 0;
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char character;
FILE *fpointer;
IIET-CSE-THIRUVALLUR
Result.
IIET-CSE-THIRUVALLUR