SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
Ramapuram Campus, Chennai- 600089.
FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Science & Engineering
LAB MANUAL
CLASS : B.Tech. [U.G]
YEAR : I YEAR
SEM. : I
SOFTWARE REQUIREMENT : C, Python
Ex No List of Experiment
1 Input and Output Statements, Variables
2 Data Types & Operators – I
3 Data Types & Operators – II
4 Control Statements
5 Arrays – One Dimensional
6 Arrays with Pointers
7 Strings
8 Functions
9 Arrays and Functions
10 Input, Output in Python
11 Python data structures
12 Arrays in python
13 Operations with NumPy
14 Operations with Pandas
15 Case study: Data science with NumPy, Pandas
Ex No: 1
INPUT AND OUTPUT STATEMENT, VARIABLES
Date:
Input and Output Functions
The Printf() Function
The Use of printf function for printing captions and numerical results. The Printf
Function provides certain features that can be effectively exploited to control the
alignment and spacing print-outs on the terminals. The general form of printf statement
is
printf(“controlstring”,arg1,arg2,…,a
rg-n);
Control string consists of three types of items:
1. Characters That Will be printed on the screen.
2. Format specifications that define the output format for display of each item.
3. Escape sequence characters such as \n,\t,and \b.
The control string indicates how many arguments follow and what their types are. The
arguments arg1,arg2,…,arg-n are the variables whose values are formatted and printed
according to the specifications of the control string.
A Simple Format Specification Has the Following Form:
%w.ptype-specifier
where w specifies the total number of columns for the output value and p specifies the
number of digits to the right of the decimal point or the number of characters to be printed
from a string.
The scanf() Function
The scanf function can be used to read formatted input. The general form of scanf s
scanf(“controlstring”,arg1,arg2,…,arg-n);
Example:
scanf(“%d%c%f%s”,&count,&code,&ratio,name);
Commonly used formatting codes are given the following table.
CODE MEANING
%c Read a single character
%d Read a decimal integer
%e Read a floating-point value
%f Read a floating-point value
%g Read a floating-point value
%h Read a short integer
%i Read a decimal, hexadecimal,or octal integer
%o Read an octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a hexadecimal integer
1(a) Take multiple inputs from the user and display them
AIM:
Write a C Program to take multiple inputs from the user and display them
ALGORITHM:
STEP 1: Start
STEP 2: Create a file named arith.c
STEP 3: declare a integers “a” & a float “b”.
STEP 4: Get the input values using scan function and store the input.
STEP 5: Print the values given by the user.
STEP 6: stop
PROGRAM:
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a float: ");
scanf ("%d%f", &a, &b); // Taking multiple inputs
printf("You entered %d and %f", a, b);
return 0;
}
Sample Output:
Enter integer and then a float: -3
3.4
You entered -3 and 3.400000
1(b) AREA OF THE CIRCLE
AIM:
Write a C Program to Find Area of The Circle.
ALGORITHM:
STEP1: Read the radius r
STEP2: Calculate area=3.14*r*r
STEP3: Print area
STEP4: Stop
PROGRAM:
#include<stdio.h>
int main()
{
float a, r;
printf("Enter the radius:");
scanf ("%f”, &r);
a=3.14*r*r;
printf("Area: %f",a);
return 0;
}
Sample Output:
Enter the radius:5
Area:78.500000
1(c) SWAP TWO NUMBERS
AIM:
Write a C Program to swap two numbers.
ALGORITHM:
STEP1: Read the numbers a and b
STEP2: t=a
STEP3: a=b
STEP4: b=t
STEP5: Print a and b
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int a,b,t;
printf("Enter the numbers:");
scanf("%d%d”, &a,&b);
printf("\n Before Swapping“);
printf("\n a=%d",a);
printf("\n b=%d”,b);t=a;
a=b;b=t;
printf("\n After Swapping");
printf("\n a=%d",a);
printf("\n b=%d",b);
return 0;
}
Sample Output:
Enter the numbers:5 10
Before Swapping
a=5
b=10
After Swapping
a=10
b=5
Ex No: 2
DATA TYPES AND OPERATORS - I
Date:
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators can be classified into a number of categories. They include
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators.
Arithmetic Operators
C provides all the basic arithmetic operators. They are listed in the following table:
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
Relational Operators
The relational operators are used to compare two values. An expression containing
a relational operator is termed as a relational expression. The value of a relational
expression is either one or zero supports six relational operators. These operators and
their meanings are shown in the following table
Operator Meaning
< Is less than
<= Is Less Than or Equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
== and! = relational operators are also known as equality operators.
Logical Operators
In addition to the relational operators, Cash Three Logical Operators. They Are
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Assignment Operators
Assignment operators are used to assign the result of an expression to a variable.
The assignment operator is ‘=’. Assignment expressions that make use of this operator
are written in the form
identifier=expression;
Where identifier generally represents a variable and expression represents a constant, a
variable or a more complex expression.
Example:
a=3;
x=y;
Increment and Decrement Operators
The increment and decrement operators are
++ and --
The increment operators cause its operand to be increased by 1, whereas the
decrement operator causes its operand to be decreased by 1.
Conditional Operator (Ternary Operator)
A ternary operator pair“?:” is available in C to construct conditional expressions of the
form
exp1? exp2:exp3;
Where exp1, exp2, and exp3 are expressions.
Here exp1 is evaluated first. If it is non zero (true), then the expression exp2 is
evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and
its value becomes the value of the expression.
Example:
c=(a>b)?a:b;
Bitwise Operators
The bitwise operators are used for testing the bits, or shifting them right or left.
Bitwise operators may not be applied to float or double. The following table lists the
bitwise operators and their meanings.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive
OR
<< Shift left
>> Shift right
~ One’s complement
Special Operators
C supports some special operators. They are
● comma operator
● size of operator
● pointer operators (& and *)
● member selection operator (. and ->)
The comma operator
The comma operator can be used to link the related expressions together.
Examples:
1. The statement value= (x=10, y=5, x+y);
first assigns the value10 to x,the n assigns 5 to y,and finally assigns 15 to value.
2. In for loops: for (n=1, m=10; n<=m;n++,m++)
3. In while loops: while(c=getchar(),c!=’\0’)
4. Exchanging values:t=x,x=y,y=t;
The sizeof operator
The size of is a compile time operator and returns the number of bytes the oper
and occupies.
Example:
m=sizeof(sum);
n=sizeof(longint);
k=sizeof(234L);
Ex No: 3
DATA TYPES AND OPERATORS - II
Date:
3(a) ARITHMETIC OPERATIONS
AIM:
Write a C Program to perform arithmetic operations.
ALGORITHM:
STEP1: Read the numbers a and b
STEP2: c=a+b
STEP3: d=a-b
STEP4: e=a*b
STEP5: f=a/b
STEP6: Print c, d,e,f
STEP7: Stop
PROGRAM:
#include<stdio.h>
int main()
{
float a,b,c,d,e,f;
printf("Enter the numbers");
scanf(“%f%f”,&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf("\na+b=%f",c);
printf("\na-b=%f",d);
printf("\na*b=%f",e);
printf("\na/b=%f",f);
return 0;
}
Sample Output:
Enter the numbers 6.0 4.0
a+b=10.0
a-b=2.0
a*b=24.0
a/b=1.5
3(b) INCREMENT & DECREMENT OPERATORS
AIM:
Write a C program to demonstrate increment and decrement operators
ALGORITHM:
STEP1: Read the value of i
STEP2: Print i++
STEP3: Print ++i
STEP4: Print i--
STEP5: Print --i
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int i;
printf("Enter the value of i");
scanf("%d",&i);
printf("\n%d",i++);
printf("\n%d",++i);
printf("\n%d",i--);
printf("\n%d",--i);
return 0;
}
Sample Output:
Enter the value of i 10 10
10 12 12 10
3(c) Find the size of different data types
AIM:
Write a C program to demonstrate increment and decrement operators
ALGORITHM:
STEP1: Declare the variables
STEP2: Print sizeof int
STEP3: Print float
STEP4: Print double
STEP5: Print char
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int int Type;
float float Type;
double double Type;
char char Type;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Sample Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Ex No: 4
CONTROL STATEMENTS (BRANCHING AND LOOPING)
Date:
Decision Making and Branching
C language supports control or decision-making statements. They are
1. if statement
2. switch statement
3. Conditional operator statement
4. go to statement
Decision making with if statement
The if statement is a powerful decision-making statement and is used to control the flow of execution
of statements. The syntax of if statement is
if(condition)
It allows the computer to evaluate the condition first and then, depending on whether the value of the
condition is ‘true’ (non-zero) or ‘false’ (zero), it transfers the control to a particular statement.
Types of if statements
1. Simple if statement
2. if…. else statement
3. Nested if…. else statement
4. else if ladder
Simple if Statement
The general form of a simple if statement is
if(condition)
Statement-block;
Statement-x;
4(a) GREATEST AMONG TWO NUMBERS
AIM:
Write a C program to find greatest among two numbers using simple-if statement.
ALGORITHM:
STEP1: Read a and b
STEP 2: if (a>b) then Print “A is greatest”
STEP 3: if (b>a) then Print “B is greatest”
STEP4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“Enter the numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“A is greatest”);
if(b>a)
printf(“B is greatest”);
}
Sample Output:
Enter the numbers: 5 7
B is greatest
Nesting of if…else Statement
When a series of decisions are involved, we may have to use more than one if…else statement in nested
form as follows:
If the condition-1 is false, the statement-3 will be executed; otherwise, it continues to perform the
second test. If the condition-2 is true, the statement-1 will be evaluated; otherwise, the statement-2 will
be evaluated and then the control is transferred to the statement-x.
4(b) GREATEST AMONG THREE NUMBERS
AIM:
Write a C program to find greatest among three numbers using nested-if statement.
ALGORITHM:
STEP1: Read a,b and c
STEP2: if (a>b) then if (a>c) then Print“A is greatest” else
Print“C is greatest”
STEP3: else if (b>c) then Print“B is greatest” else
Print“C is greatest”
STEP4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the numbers : “);
scanf(“%d%d%d”,&a,&b,&c);if(a>b)
{
if(a>c)
printf(“A is greatest”);
else
printf(“C is greatest”);
}
else
{
if(b>c)
printf(“B is greatest”);
else
printf(“C is greatest”);
}
getch();
}
Sample Output:
Enter the numbers: 3 4 5
C is greatest
The else…if ladder (if…else if statement)
A multi path decision is a chain of ifs in which the statement associated with each else is an if. The
general form of if…else if statement is
4(c) CHECK POSITIVE, NEGATIVE OR ZERO
AIM: number is positive, negative or zero
Write a C Program to check whether the given
ALGORITHM:
STEP1: Read the number n
STEP2: if (n>0) then Print“Positive”
STEP3: else if (n<0) then Print“Negative”
STEP4: else Print“Zero”
STEP5: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter the numbers:“);
scanf(“%d”,&n);
if(n>0)
printf(“The given number is Positive”);
else if(n<0)
printf(“The given number is Negative”);
else
printf(“The given number is Zero”);
getch();
}
Sample Output:
Enter the numbers: 55
The given number is Positive
The switch Statement
The switch statement is a built-in multiway decision statement. The switch statement tests the value of
a given variable against a list of case values and when a match is found, a block of statements
associated with that case is executed.
4(d) ARITHMETIC OPERATIONS
AIM:
Write a C program to perform arithmetic operations using switch statement.
ALGORITHM:
STEP1: Display the menu
STEP2: Read the numbers a and b
STEP3: Read the choice
STEP4: if choice = 1 then c=a+b
STEP 5: if choice =2 then c=a-b
STEP6: if choice =3 then c=a*b
STEP7: if choice=4 then c=a/b
STEP 8: else Print “Invalid Choice” and exit
STEP9: Print c
STEP10: Stop
PROGRAM:
#include<stdio.h>
int main( )
{
int a, b, choice;
printf(“\nEnter Two Numbers: ”);
scanf(“%d%d”, &a,&b);
printf(“\n Enter 1 for Addition”);
printf(“\n Enter 2 for Subtraction”);
printf(“\n Enter 3 for Multiplication”);
printf(“\n Enter 4 for Division”);
printf(“ Enter your Choice”);
scanf(“%d”,&choice)
switch (choice)
{
case 1:
printf(“Sum is : %d”, a+b);
break;
case 2:
printf(“Difference is : %d”, a-b);
break;
case 3:
printf(“Multiplication is : %d”, a*b);
break;
case 4:
printf(“Difference is : %d”, a/b);
break;
default:
printf(“Invalid Choice:”);
}
getch( );
}
Sample Output:
Enter two numbers 20 10
Enter 1 for Addition
Enter 2 for Subtraction
Enter 3 for Multiplication
Enter 4 for Division
Enter your Choice: 3
Product is: 200
Decision Making and Looping
Loop is a sequence of statements executed until some conditions for termination of the
loop are satisfied. A program loop consists of two segments, one known as the body of the
loop and the other known as the control statement. The control statement tests certain
conditions and then directs the repeated execution of the statements contained in the body of
the loop.
Depending on the position of the control statement in the loop, a control structure may be
classified either as the entry-controlled loop or as the exit-controlled loop.
The C language provides for three loop constructs for performing loop operations. They are:
1. The while statement.
2. The do statement
3. The for statement.
The while Statement
The simplest of all the looping structures in C is the while statement. The while is an entry-
controlled loop statement. The condition is evaluated and if the condition is true, then the body
of the loop is executed. After execution of the body, the condition is once again evaluated and
if it is true, the body is executed once again. This process of repeated execution of the body
continues until the condition finally becomes false and the control is transferred out of the
loop. On exit, the program continues with the statement immediately after the body of the
loop.
The body of the loop may have one or more statements. The braces are needed only if the
body contains two or more statements.
4(e) SUM OF DIGITS
AIM:
Program to find sum of digits using while statement.
ALGORITHM:
STEP1: Start
STEP2: Read the value of n
STEP3: Repeat Step4toStep5if(n>0)
STEP4: Computes=s+(n%10)
STEP5: Compute n=n/10
STEP6: Print s
STEP7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0;
clrscr();
printf(“Enter the value of N:”);
scanf(“%d”,&n);
while(n>0)
{
s=s+(n%10);
n=n/10;
}
printf(“Sum of digits:%d”,s);
getch();
}
Sample Output:
Enter the value of N:5
Sum of digits:5
The do…while Statement
In do…while statement, the body of the loop is evaluated first.At the end of the loop,the
condition in the while statement is evaluated.If the condition is true, the program continues to
evaluate the body of the loop once again.This process continues as long as the condition is
true.When the condition becomes false, the loop will be terminated and the control goes to the
statement that appears immediately after the while statement.
4(f)MULTIPLICATION TABLE
AIM:
Write a C Program to print multiplication table
ALGORITHM:
STEP1: Initialize i=1
STEP2: Read the value of n
STEP3: Print n xi=n*i
STEP4: Compute i=i+1
STEP5: if(i<=10) go to Step3
STEP6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i=1;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
do
{
printf("\n%d*%d=%d",n,i,n*i);
i++;
}
while(i<=10);
getch();
}
Sample Output:
Enter the value of n:5
51=5
52=10
53=15
54=20
55=25
56=30
57=35
58=40
59=45
510=50
The for Statement
The for loop is another entry-controlled loop that provides a more concise loop control structure
4(g) Sum of N numbers
AIM:
Write a C Program to find sum of ‘N’ numbers using for statement.
ALGORITHM:
STEP1: Initializes=0
STEP2: Read the value of n
STEP3: For i=1 t on, do Step4, and Step5
STEP4: Read a
STEP5: Computes=s+a
STEP6: Print s
STEP7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,a,s=0;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the numbers \n");
for(i=1;i<=n;i++)
{
scanf(“%d”,&a);
s+=a;
}
printf("Sum:%d",s);
getch();
}
Sample Output:
Enter the value of n:5
Enter the numbers
5
6
7
8
9
Sum:35
Ex No: 5
ARRAYS-ONE DIMENSIONAL
Date:
Arrays
An array is a group of related data items that share a common name. For example,
a[10] represents any 10 values. While the complete set of values is referred to as an array, the
individual values are called elements.
One-Dimensional Arrays
A list of items can be given one variable name using only one subscript and such
a variable is called a single-subscripted variable or a one-dimensional array. The subscript
can begin with number 0. That is a[0] is allowed. For example, if we want to represent
a set of five numbers by an array variable number, then we may declare the variable no
as follows int no[5]; and the computer reserves five storage locations as shown below:
no[0]
no[1]
no[2]
no[3]
no[4]
MAXIMUM OF ‘N’ NUMBERS
AIM:
Write a C Program to find maximum of ‘N’ numbers using array.
ALGORITHM:
STEP1: Initializes=0
STEP2: Read the value of n
STEP3: For i=1 t on, do Step4
STEP4: Read a[i]
STEP5:max=a[1]
STEP6: For i=2 ton,do Step6
STEP7: If(max<a[i]) then max=a[i]
STEP8: Print s
STEP9: Stop
PROGRAM:
#include <stdio.h>
int main()
{
int arr[5] = { 1, 2, 4, 8,16}
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
arr[3] = 9721;
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Sample Output:
1 2 4 8 16
1 2 4 9721 16