0% found this document useful (0 votes)
2 views

CS3271 C Lab

The document outlines the objectives, experiments, and outcomes for a C programming laboratory course. It includes various programming tasks such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, pointers, and structures. Each experiment consists of an aim, algorithm, program code, output, and result, demonstrating the practical application of C programming concepts.

Uploaded by

vimalraj17r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS3271 C Lab

The document outlines the objectives, experiments, and outcomes for a C programming laboratory course. It includes various programming tasks such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, pointers, and structures. Each experiment consists of an aim, algorithm, program code, output, and result, demonstrating the practical application of C programming concepts.

Uploaded by

vimalraj17r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CS3271 - PROGRAMMING IN C LABORATORY

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

PROGRAM USING I/O STATEMENTS


#include<stdio.h>
void main()
{
int num;
printf(“enter number “);
scanf(%d”,&num);
if(num<0)
printf(the no is negative “)
else
printf(“the no is n\positive)
}

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

You entered a consonant.

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

Step 2. Declare variables


Step 3. Read the 4 numbers
Step 4. Calculate avg=sum/n
Step 5. Display the output
Step 6. Stop
PROGRAM:

#include<stdio.h> void main()


{
inti,n,sum=0,nu[100]; float avg;
clrscr();
printf("\nEnter the numbers\n");
for(i=0;i<3;i++)
{
scanf("%d",&nu[i]); sum = sum + nu[i];
}
avg = (float)sum/n;
printf("\nAverage is : %.2f\n",n,avg); getch();
}
OUTPUT:

Enter the numbers 32


45
54
22
Average is 38.25
RESULT

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

persons and compute the Body Mass Index of the individuals..

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

How many people's BMI do you want to calculate? 2

Please enter the mass of the person 1 in kg: 88

Please enter the height of the person 1 in meter: 1.8288

Please enter the mass of the person 2 in kg:58

Please enter the height of the person 2 in meter: 2.2

Person 1's BMI is 26.31178 Person 2's BMI is 11.98347

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

To concatenate two strings

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 ";

char source[] = "World!";


printf("Concatenated String: %s\n", strcat(destination,source)); return 0;

Concatenated String: Hello World!

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

Step 1: Start the program


Step 2: Initialize the array elements
Step 3: Find the largest number of the array
Step 4: Display the largest number
Step 5: Stop the program
PROGRAM:

#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);

printf("\nmaximum number is %d",m);

getch();

max(int x[],int k)

int t,i; t=x[0];

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

To reverse a sentence using recursion

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

Step 1 : Start the program

Step 2 : Declare the variables in the structure data type.

Step 3 : Read the values of the variables in the structure.

Step 4 : Display the result

Step 5 : Stop the program

PROGRAM

#include <stdio.h>

struct Student

char name[50]; int roll;

float marks;

s[10];

int main()

int i;

printf("Enter information of students:\n");

for(i=0; i<10; ++i)

s[i].roll = i+1;

printf("\nFor roll number%d,\n",s[i].roll);


printf("Enter name: ");

scanf("%s",s[i].name);

printf("Enter marks: ");

scanf("%f",&s[i].marks)

; printf("\n");

printf("Displaying Information:\n\n");

for(i=0; i<10; ++i)

printf("\nRoll number: %d\n",i+1);

printf("Name: ");

puts(s[i].name);

printf("Marks: %.1f",s[i].marks);

printf("\n");

return 0;

}
OUTPUT

Enter information of students:


For roll number1
Enter name: Tom
Enter marks: 98
For roll number2,
Enter name: Jerry
Enter marks: 89

Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
For roll number2,
Name: Jerry
Marks: 89

RESULT

Thus the C program to store Student Information in Structure has been


successfully executed and verified successfully.
EX.NO: 10 PROGRAM USING FILES
DATE:

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];

};

void insert(FILE *);

void update(FILE *);

void del(FILE *);

void display(FILE *);

void search(FILE *);

int record = 0;

int main(void)
{

int choice = 0;

FILE *fp = fopen( "telephone.dat", "rb+" );

if (fp == NULL ) perror ("Error opening file");

while (choice != 6)

printf("\n1 insert\t 2 update\n");

printf("3 delete\t 4 display\n");

printf("5 search\t 6 Exit\n Enter choice:");

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;

void insert(FILE *fp)

struct dir contact, blank;

fseek( fp, -sizeof(struct dir), SEEK_END );

fread(&blank, sizeof(struct dir), 1, fp);

printf("Enter individual/company name: ");

scanf("%s", contact.name);

printf("Enter telephone number: ");

scanf("%s", contact.number);

fwrite(&contact, sizeof(struct dir), 1, fp);

void update(FILE *fp)

char name[20], number[10];

int result;

struct dir contact, blank;

printf("Enter name:");

scanf("%s", name);

rewind(fp);

while(!feof(fp))

{
result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(name, contact.name) == 0)

printf("Enter number:");

scanf("%s", number);

strcpy(contact.number, number);

fseek(fp, -sizeof(struct dir), SEEK_CUR);

fwrite(&contact, sizeof(struct dir), 1, fp);

printf("Updated successfully\n");

return;

printf("Record not found\n");

void del(FILE *fp)

char name[20], number[10];

int result, record=0;

struct dir contact, blank = {"", ""};

printf("Enter name:");

scanf("%s", name);

rewind(fp);

while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(name, contact.name)==0)

{
fseek(fp, record*sizeof(struct dir), SEEK_SET);

fwrite(&blank, sizeof(struct dir), 1, fp

printf("%d Deleted successfully\n", record-1);

return;

record++;

printf("not found in %d records\n", record);

void display(FILE *fp)

struct dir contact;

int result;

rewind(fp);

printf("\n\n Telephone directory\n");

printf("%20s %10s\n", "Name", "Number");

printf("*******************************\n");

while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strlen(contact.name) > 0)

printf("%20s %10s\n",contact.name, contact.number);

printf("*******************************\n");

void search(FILE *fp)

{
struct dir contact;

int result;

char name[20];

rewind(fp);

printf("\nEnter name:");

scanf("%s", name);

while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(contact.name, name) == 0)

printf("\n%20s %10s\n",contact.name, contact.number);

return;

printf("Record not found\n");

}
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

Enter number: 333333

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.

You might also like