CS3271 C Lab
CS3271 C Lab
COURSE OBJECTIVES:
• To familiarise with C programming constructs.
• To develop programs in C using basic constructs.
• To develop programs in C using arrays.
• To develop applications in C using strings, pointers, functions.
• To develop applications in C using structures.
• To develop applications in C using file processing.
LIST OF EXPERIMENTS:
1. I/O statements, operators, expressions
2. Decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operation
6. Functions: call, return, passing parameters by (value, reference), passing arrays to
function.
7. Recursion
8. Pointers: Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10. Files: reading and writing, File pointers, file operations, random access, processor
directives.
TOTAL: 60 PERIODS
COURSE OUTCOMES:
Upon completion of the course, the students will be able to
CO1: Demonstrate knowledge on C programming constructs.
CO2: Develop programs in C using basic constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers, functions.
CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.
EX.NO: 1 PROGRAM USING OF EXPERIMENTS I/O STATEMENTS,
OPERATORS, EXPRESSIONS
DATE:
AIM
To write a C Program to perform I/O statements and expressions for sum of odd and even
numbers.
ALGORITHM
1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Using I/O statements and expressions for computational processing.
5. Display the output of the calculations.
6. Stop
OUTPUT:
Enter number :5
The number is positive
Enter number :-5
The number is negative
PROGRAM USING OPERATORS
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);
return 0;
}
OUTPUT:
Enter any two numbers : 20 10
SUM = 30
DIFFERENCE = 10
PRODUCT = 200
QUOTIENT = 2.000000
MODULUS = 0
PROGRAM USING EXPRESSIONS
#include <stdio.h>
int main()
{
int a,b,result;
printf("Enter 2 numbers for Arithmetic operation \n");
scanf("%d\n%d",&a,&b);
result = a+b;
printf("================ARITHMETIC EXPRESSIONS==============\n");
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \n",a,b,result);
result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
result = a%b;
printf("Modulus(Remainder) when %d divided by %d = %d \n",a,b,result);
int c=a;
result = a++;
printf("Post Increment of %d is = %d \n",c,result);
result = ++a;
printf("Pre Increment of %d is = %d \n",c,result);
result=a--;
printf("Post decrement of %d is = %d \n",c,result);
result=--a;
printf("Pre decrement of %d is = %d \n",c,result);
printf("======================================================");
return 0;
}
OUTPUT:
Enter 2 numbers for Arithmetic operation
10
5
================ARITHMETIC EXPRESSIONS==============
Addition of 10 and 5 is = 15
Subtraction of 10 and 5 is = 5
Multiplication of 10 and 5 is = 50
Division of 10 and 5 is = 2
Modulus(Remainder) when 10 divided by 5 = 0
Post Increment of 10 is = 10
Pre Increment of 10 is = 12
Post decrement of 10 is = 12
Post decrement of 10 is = 10
RESULT
Thus a C Program using I/O statements, operators, expressions
was executed and the output was obtained successfully
EX.NO: 2 PROGRAM USING OF EXPERIMENTS DECISION-MAKING
CONSTRUCTS
DATE:
AIM
To write a C program to find if a number is negative, positive or zero using if ... else if ...
else statement.
ALGORITHM
1. Start the program
2. Get the number
3. Check the number if it is negative, positive or equal to using if statement.
4. If the number is < 0print number is negative, else if the number is >0 print it is
positive else the number =0.
5. Display the result
6. Stop the program.
PROGRAM
#include<stdio.h> void main()
{
int n;
printf("Enter a number:"); scanf("%d",&n);
if(n<0)
printf("Number is negative"); printf("Number is positive");
else printf("Number is equal to zero");
if(n>0)
else
}
OUTPUT:
Enter a number:109
Number is positive
Enter a number:-56
Number is negative
Enter a number:0
Number is equal to zero
RESULT
Thus a C Program using decision-making constructs was executed and the output was
obtained.
EX.NO: 2(B) PROGRAM USING OF EXPERIMENTS DECISION-MAKING
CONSTRUCTS
DATE:
AIM
To write a C program to check if entered alphabet is vowel or a consonant using switch
case.
ALGORITHM
Start the program
Get the character for choice
Give the multiple choices using case statement whether one of the choices are in
the consonants and print the same.
In the default case print that it is a consonant.
Display the result
Stop the program.
PROGRAM
#include <stdio.h> int main()
{
char alphabet;
printf("Enter an alphabet:"); scanf("%c",&alphabet); switch(alphabet)
{
case 'a':
printf("Alphabet a is a vowel.\n");
break;
case 'e':
printf("Alphabet e is a vowel.\n");
break;
case 'i':
printf("Alphabet i is a vowel.\n");
break;
case 'o':
printf("Alphabet o is a vowel.\n");
break;
case 'u': printf("Alphabet u is a vowel.\n");
break;
default:
printf("You entered a consonant.\n");
}
return
0;
}
OUTPUT
Enter an alphabet: i
Alphabet i is a vowel.
Enter an alphabet: o
Alphabet o is a vowel.
Enter an alphabet: u
Alphabet u is a vowel.
Enter an alphabet: c
RESULT
Thus the C program using decision-making construct has been verified and
executed successfully
EX.NO: 3 PROGRAM USING OF EXPERIMENTS LOOP
DATE:
AIM
To find average of 4 integers using for loop.
ALGORITHM
Step 1. Start
Thus the C Program to find the average of 4 numbers has been has been verified and
executed successfully
EX.NO: 4 PROGRAM USING ARRAYS
DATE:
AIM
To write a C Program to Populate a two dimensional array with height and weight of
ALGORITHM
1. Start
2. Declare variables
3. Read the number of persons and their height and weight.
4. Calculate BMI=W/H2for each person
5. Display the output of the BMI for each person.
6. Stop
PROGRAM
#include<stdio.h> #include<math.h> int main(void)
{
int n,i,j;
float massheight[3][2]; float bmi[3];
printf("How many people's BMI do you want to calculate?\n"); scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
{
switch(j)
{
case 0:
printf("\nPlease enter the mass of the person %d in kg: ",i+1); scanf("%f",&massheight[i]
[0]);
break;
case 1:
printf("\nPlease enter the height of the person %d in meter: ",i+1);
scanf("%f",&massheight[i][1]);
break;
}
}
}
for(i=0;i<n;i++)
{
bmi[i] = massheight[i][0] / pow(massheight[i][1],2.0); printf("Person %d's BMI is %f\
n",i+1,bmi[i]);
}
return 0;
}
OUTPUT
RESULT
Thus a C Program Body Mass Index of the individuals was executed and the output
was obtained successfully.
EX.NO: 5 PROGRAM USING STRING OPERATION.
DATE:
AIM
ALGORITHM:
Step1: Start
Step 2: Get the two Strings to be concatenated.
Step 3: Declare a new String to store the concatenated String.
Step 4: Insert the first string in the new string.
Step 5: Insert the second string in the new string.
Step 6: Print the concatenated string.
Step 7: Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Hello ";
OUTPUT:
Concatenated String: Hello World!
RESULT
Thus the C Program to concatenate two strings has been executed and the
result was verified. successfully.
EX.NO: 6 PROGRAM USING FUNCTIONS
DATE:
AIM
To get the largest element of an array using function
ALGORITHM
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
getch();
max(int x[],int k)
for(i=1;i<k;i++)
if(x[i]>t)
t=x[i];
return(t);
OUTPUT:
Maximum number is 45
RESULT
Thus the C Program to display the largest number in an array using function has been
executed and verified successfully.
EX.NO: 7 PROGRAM USING RECURSION.
DATE:
AIM
ALGORITHM
Step 1: Start
Step 2: Declare the function reverse
Step 3: Call the reverse function
Step 4: Get the sentence from the user and reverse it recursively
Step 5: stop the execution.
PROGRAM:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence()
{
char c;
scanf("%c", &c);
if (c != '\n')
{
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorp emosewa awesome PROGRAM:
RESULT
Thus the C Program to reverse a sentence has been executed and verified
successfully.
EX.NO: 8 PROGRAM USING POINTERS
DATE:
AIM
To implement a c program using pointer
ALGORITHM:
1.Start the program.
2. Declare variable and initialize it .
3.Get details from the user.
4.Assign the student to the printer.
5. Display the output.
6. Stop the program.
PROGRAM:
#include<stdio.h>
int main(void)
{
//student structure
Struct student
{
char id[15];
char firstname[64];
char lastname[64];
float points;
};
Struct student std ;
Struct student*ptr=NULL;
printf(“enter Id”);
scanf(%s”,ptr->id);
printf(“enter first name “);
scanf(%s”,ptr->first name);
printf(enter last name “);
scanf(“%s”,ptr->last name);
printf(“enter points”);
scanf(“%f”&ptr->points);
printf(“ID:%s/n”,ptr->id);
printf(“firstname:%s/n”,ptr->firstname);
printf(“lastname:%s/n”,ptr-.lastname);
printf(“ponts:%f/n”,ptr->points);
return 0;
}
OUTPUT:
Enter ID: 623049
Enter first name: ABC
Enter lastname : XYZ
Enter points: 8.44
RESULT
Thus the C Program to implement a c program using pointer has been executed and
verified successfully.
EX.NO: 9 PROGRAM USING STRUCTURES
DATE:
AIM
To write a C program to store the student information using structure.
ALGORITHM
PROGRAM
#include <stdio.h>
struct Student
float marks;
s[10];
int main()
int i;
s[i].roll = i+1;
scanf("%s",s[i].name);
scanf("%f",&s[i].marks)
; printf("\n");
printf("Displaying Information:\n\n");
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
return 0;
}
OUTPUT
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
For roll number2,
Name: Jerry
Marks: 89
RESULT
AIM
To insert, update, delete and append telephone details of an individual
or a company into a telephone directory using random access file.
ALGORITHM
Step 1: Create a random access file
Step 2: Call the respective procedure to insert, update, delete or append
based on user choice
Step 3: Access the random access file to make the necessary changes and
save
PROGRAM
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<fcntl.h>
struct dir
char name[20];
char number[10];
};
int record = 0;
int main(void)
{
int choice = 0;
while (choice != 6)
scanf("%d", &choice);
switch(choice)
case 1:
insert(fp);
break;
case 2:
update(fp);
break;
case 3:
del(fp);
break;
case 4:
display(fp);
break;
case 5:
search(fp);
break;
default: ;
fclose(fp);
return 0;
scanf("%s", contact.name);
scanf("%s", contact.number);
int result;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number, number);
printf("Updated successfully\n");
return;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
fseek(fp, record*sizeof(struct dir), SEEK_SET);
return;
record++;
int result;
rewind(fp);
printf("*******************************\n");
while(!feof(fp))
printf("*******************************\n");
{
struct dir contact;
int result;
char name[20];
rewind(fp);
printf("\nEnter name:");
scanf("%s", name);
while(!feof(fp))
return;
}
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
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: 6
RESULT
Thus the C program To insert, update, delete and append telephone details of an
individual or a company into a telephone directory using random access file was successfully
written and executed.