cds-lab-1-3-lab-manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

lOMoARcPSD|21099380

CDS LAB 1-3 - lab manual

Programming using C++ (Anna University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Sheik Mohideen ([email protected])
lOMoARcPSD|21099380

CS3362 C PROGRAMMING AND DATA STRUCTURES LABORATORY LTPC


0 0 3 1.5
COURSE OBJECTIVES:

-linear data structures

amiliarized to sorting and searching algorithms

LIST OF EXPERIMENTS
1. Practice of C programming using statements, expressions, decision making and iterative statements
2. Practice of C programming using Functions and Arrays
3. Implement C programs using Pointers and Structures
4. Implement C programs using Files
5. Development of real time C applications
6. Array implementation of List ADT
7. Array implementation of Stack and Queue ADTs
8. Linked list implementation of List, Stack and Queue ADTs
9. Applications of List, Stack and Queue ADTs
10. 10.Implementation of Binary Trees and operations of Binary Trees
11. Implementation of Binary Search Trees
12. Implementation of searching techniques
13. Implementation of Sorting algorithms : Insertion Sort, Quick Sort, Merge Sort
14. Implementation of Hashing – any two collision techniques
TOTAL: 45 PERIODS
COURSE OUTCOMES:
At the end of the course, the students will be able to:
CO1: Use different constructs of C and develop applications
CO2: Write functions to implement linear and non-linear data structure operations
CO3: Suggest and use the appropriate linear / non-linear data structure operations for a given problem
CO4: Apply hash functions that result in a collision free scenario for data storage and Retrieval
CO5: Implement Sorting and searching algorithms for a given application

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

EX. NO: 1 C PROGRAMMING USING STATEMENTS, EXPRESSIONS


DATE:
AIM
To write a C program to illustrate the concept of Statements and Expressions.

a. AREA AND CIRCUMFERENCE OF A CIRCLE


ALGORITHM
Step 1: Start
Step 2: Read the input r
Step 3: Calculate area = pi * r * r
Step 4: Calculate circum = 2 * pi * r
Step 5: print area, circum ,
Step 6: Stop

PROGRAM
#include <stdio.h>
int main()
{
float radius;
double area, circumference;
printf("\n Enter the radius of the circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circumference=2*3.14*radius;
printf(" \n Area = %lf", area);
printf(" \n Circumference = %lf", circumference);
return 0;
}

OUTPUT
Enter the radius of the circle : 3
Area = 28.260000
Circumference = 18.840000

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

b. ADDITION OF TWO NUMBERS C=A+B


ALGORITHM
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop

PROGRAM
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculating sum
sum = number1 + number2;
printf("Sum of two numbers: ");
printf("%d + %d = %d", number1, number2, sum);
return 0;
}

OUTPUT
Enter two integers: 4 5
Sum of two numbers: 4 + 5 = 9

c. SUM AND AVERAGE OF THREE NUMBERS


ALGORITHM
Step 1: Start
Step 2 :Read the three number let "a","b","c" form the user.
Step 3: Declared a variable "sum" and "Avg".
Step 4 : sum=a+b+c;

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

Step 5: Avg=sum/3;
Step 6: Display "sum " and "Avg".
Step 7 :End

PROGRAM
#include <stdio.h>
int main()
{
int a,b,c;
float sum;
float avg;
printf("\nEnter First Number : ");
scanf("%d", &a);
printf("\nEnter Second Number : ");
scanf("%d",&b);
printf("\nEnter Third Number : ");
scanf("%d",&c);
sum=a+b+c;
printf("\nsum: %.2f",sum);
avg=sum/3.0;
printf("\nAverage of Three Numbers : %.2f",avg);
return 0;
}

OUTPUT
Enter First Number : 5
Enter Second Number : 7
Enter Third Number : 9
sum: 21.00
Average of Three Numbers : 7.00

RESULT: Thus the C Program to illustrate concept of Statements and Expressions has been executed.

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

EX. NO: 2 C PROGRAMMING USING DECISION MAKING

DATE: (CONTROL OR BRANCHING)

AIM

To write a C Program to illustrate the concept of Decision making and Branching statements.

DECISION MAKING DESCRIPTION

C supports decision control statements that can alter the flow of a sequence of instructions. These
statements help to jump from one part of the program to another depending on whether a particular
condition is satisfied or not.

These decision control statements include:

(a) if statement,

(b) if–else statement,

(c) if–else–if statement, and

(d) switch–case statement.

(A) IF-ELSE STATEMENT - NUMBER IS EVEN OR ODD

ALGORITHM

Step 1 : Input a number N

Step 2 : if (N%2==0)

Step 3 : print <Even Number=

Step 4 : else

Step 5: Print <Odd number=

Step 6: Exit

PROGRAM

#include <stdio.h>

int main()

int a;

printf("\n Enter the value of a : ");

scanf("%d", &a);

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

if(a%2==0)

printf("\n %d is even", a);

else

printf("\n %d is odd", a);

return 0;

OUTPUT

Enter the value of a : 6

6 is even

Enter the value of a : 61

61 is odd

B. IF–ELSE–IF STATEMENT

C Program to check whether a number is negative, positive or zero using if–else–if Statement

ALGORITHM:

1. Input a number from the user.

2. If number is less than zero, then it is a negative integer.

3. Else if number is greater than zero, then it is a positive integer.

4. Else, the number is equal to zero.

5. End.

PROGRAM

#include <stdio.h>

int main()

int num;

printf("\n Enter any number : ");

scanf("%d", &num);

if(num==0)

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

printf("\n The value is equal to zero");

else if(num>0)

printf("\n The number is positive");

else

printf("\n The number is negative");

return 0;

OUTPUT:

Enter any number : -50

The number is negative

Enter any number : 9

The value is equal to zero

C. SWITCH–CASE STATEMENT

Check whether entered character is a vowel or not using switch–case Statement

ALGORITHM
Step 1: Start
Step 2: Declare character type variable ch and Read ch from User
Step 3: // Checking both lower and upper case vowels.
IF (ch == 'a' || ch == 'A' ||
ch == 'e' || ch == 'E' ||
ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' ||
ch == 'u' || ch == 'U' )
Print "Vowel"
ELSE
Print "Consonant"
Step 4: Stop

PROGRAM:

#include <stdio.h>

int main()

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

char c;

printf("Enter an Alphabet\n");

scanf("%c", &c);

switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("%c is VOWEL", c);
break;
default: printf("%c is CONSONANT", c);
}
return 0;
}

OUTPUT
Enter an Alphabet
e
e is VOWEL
Enter an Alphabet
Z
Z is CONSONANT

RESULT:

Thus the C Program to illustrate the concept of Decision making and Branching statements.

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

EX. NO: 3 C PROGRAMMING USING ITERATIVE STATEMENTS

DATE: (LOOPING STATEMENTS)

AIM

To write a C Program to illustrate the Iteration concepts using Looping Statements.

DESCRIPTION

ITERATIVE OR LOOPING STATEMENTS

Iterative statements are used to repeat the execution of a sequence of statements until the specified

expression becomes false. C supports three types of iterative statements also known as looping

statements. They are

 while loop

 do–while loop

 for loop

A.WHILE LOOP

PRINT THE FIRST 8N9 NUMBERS USING A WHILE LOOP

ALGORITHM

Step 1: Start

Step 2: Assign i=1

Step 3: Read a number, num

Step 4: While i<=num

Print i

Compute i=i+1

Step 5: Stop

PROGRAM

#include <stdio.h>

int main()

int i = 1,n;

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

printf("Enter the Number");

scanf("%d",&n);

while(i<=n)

printf("\n %d", i);

i = i + 1;

return 0;

OUTPUT:

Enter the Number 10


1
2
3
4
5
6
7
8
9
10

SUM OF 8N9 NUMBERS

ALGORITHM

Step 1: Start

Step 2 :Read the value of N.

Step 3: Declared a variable i=0, sum=0

Step 4 : While i<=N

Sum=sum+i

i=i+1

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

Step 5: Display "sum "

Step 6 :End

PROGRAM

C program to calculate the sum of numbers upto 8n9

#include <stdio.h>

int main()

int n, m, i=0, sum =0;

printf("\n Enter the value of n : ");

scanf("%d", &n);

while(i<=n)

sum = sum + i;

i = i + 1;

printf("\n The sum of numbers upto %d = %d", n, sum);

return 0;

OUTPUT

Enter the value of n : 5

The sum of numbers upto 5 = 15

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

B. DO–WHILE LOOP

PRINT NUMBERS FROM 1 TO 10.

ALGORITHM

Step 1: Start

Step 2: Assign i=1

Step 3: Read a number, num

Step 4: DO

Print i

Compute i=i+1

While i<=num

Step 5: Stop

PROGRAM

#include <stdio.h>

int main()

int i = 1;

do

printf("\t %d", i);

i = i + 1;

while(i<=10);

return 0;

OUTPUT:

1 2 3 4 5 6 7 8 9 10

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

CALCULATE THE AVERAGE OF FIRST 8n9 NUMBERS.

#include <stdio.h>

int main()

int n, i = 0, sum =0;

float avg = 0.0;

printf("\n Enter the value of n : ");

scanf("%d", &n);

do

sum = sum + i;

i = i + 1;

while(i<=n);

avg = (float)sum/n;

printf("\n The sum of first %d numbers = %d",n, sum);

printf("\n The average of first %d numbers = %.2f", n, avg);

return 0;

OUTPUT

Enter the value of n : 20

The sum of first 20 numbers = 210

The average of first 20 numbers = 10.05

Downloaded by Sheik Mohideen ([email protected])


lOMoARcPSD|21099380

C. FOR LOOP

C Program to print the first n numbers using for loop.

ALGORITHM

Step 1: Start
Step 2: Assign i=1
Step 3: Read a number, num
Step 4: For i<=num
Print i
Compute i=i+1
Step 5: Stop

PROGRAM

#include <stdio.h>

int main()

int i, n;

printf("\n Enter the value of n :");

scanf("%d", &n);

for(i=1;i<=n;i++)

printf("\n %d", i);

return 0;

OUTPUT:

Enter the value of n : 5


1
2
3
4
5

RESULT: Thus the C Program to illustrate Iteration using Looping Statements was executed.

Downloaded by Sheik Mohideen ([email protected])

You might also like