Lab Manual: Sri Sri University
Lab Manual: Sri Sri University
Lab Manual: Sri Sri University
LAB MANUAL
PRINCIPLES OF PROGRAMMING AND DATA STRUCTURE
(FET/B.Tech. AIML/01/ 101)
1st YEAR – ODD SEMESTER
Academic Year: 2021-22
FACULTY OF EMERGING TECHNOLOGIES
SRI SRI UNIVERSITY
Ward No.3, Sandhapur, Godisahi, Odisha,India- 754006
https://srisriuniversity.edu.in
Principle of Programming and Data Structure Lab B.TECH – I YEAR
Preface
This manual was developed specifically for freshmen students taking up their first
course in programming. Its aim is to supplement classroom lectures by focusing on C/
C++ programming. Topics are arranged based on the order of class room discussion.
It is assumed that the student will be working under the Linux environment and
programming using GNU C/C++ / the Borland Turbo C/C++ compiler/Dev C, C++
Compiler under the Windows environment. Coding standards are to be followed.
Case Studies are also included at the end of the manual which will help students to
implement the concepts learned by them. T h e y can also develop a mini project
based on their knowledge and understanding.
Course Objectives:
1. To understand the various steps in Program development.
2. To understand the basic concepts in C/C++ Programming Language.
3. To learn how to write modular and readable C/C++ Programs.
4. To learn to write programs (using structured programming approach /Object
Oriented Programming Approach) in C/C++ to solve problems.
5. To introduce basic data structures such as lists, stacks and queues.
Week 1:
a. Write a program to calculate simple interest(SI) for a given principal (P), time (T),
and rate of interest (R) (SI = P*T*R/100)
b. Write a program to find the roots of a quadratic equation.
c. Write a program to swap two variables values with and without using third
variable
d. Write a program to find both the largest and smallest number in a list of integers.
e. Write a program to perform addition of two matrices.
f. Write a program to perform multiplication of two matrices.
Week 2:
a. Write a program to find the length of the string using Pointer.
b. Write a program to count the number of lines, words and characters in a given
text.
c. Write a program to find factorial of a given integer using non-recursive function
and recursive function.
d. Write program to find GCD of two integers using non-recursive function and
recursive function.
Week 3:
a. Write a program using user defined functions to determine whether the given
string is palindrome or not.
b. Write a Program to swap the values of two variables using
i) Call by Value ii) Call by Reference
c. Write a program to find the sum of integer array elements using pointers, use dynamic
memory allocation to allocate memory.
d. Write a program to perform subtraction of two matrices, Design functions to perform read,
display and subtract
Week 4:
a. Write a program to create a structure named book and display the contents of a
book.
b. Write a Program to Calculate Total and Percentage marks of a student using
structure.
c. Write a program that uses functions to perform the following operations:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complex numbers
Week 5:
a. Write a program to create a class named book and display the contents of a
book.
b. Write a Program to Calculate Total and Percentage marks of a student using
class.
c. Write a program using Class that uses functions to perform the following
operations:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complex numbers
Week 6:
a. Write a program to perform various operations on singly linked list
Week 7:
a. Write a program for static implementation of stack
b. Write a program to perform various applications on stack
Week 8:
a. Write a program for static implementation of Queue
b. Write a program to perform various applications on Queue
Week 9:
a. Write a program for dynamic implementation of stack
b. Write a program to perform various applications on stack
Week 10:
a. Write a program for dynamic implementation of Queue
b. Write a program to perform various applications on Queue
Case Studies
Case 1: Student Record Management System
The main features of this project include basic file handling operations; you will learn how to
add, list, modify and delete data to/from file. The source code is relatively short, so
thoroughly go through the mini project, and try to analyze how things such as functions,
pointers, files, and arrays are implemented.
Currently, listed below are the only features that make up this project, but you can add new
features as you like to make this project a better one!
Add record
List record
Modify record
Delete record
A librarian can add, search, edit and delete books. This section is password protected. That
means you need administrative credentials to log in as a librarian.
A student can search for the book and check the status of the book if it is available. Here is
list of features that you can add to the project.
1. You can create a structure for a student that uniquely identify each student. When a
student borrows a book from the library, you link his ID to Book ID so that librarian
can find how a particular book is borrowed.
2. You can create a feature to bulk import the books from CSV file.
3. You can add REGEX to search so that a book can be searched using ID, title, author or
any of the field.
4. You can add the student login section.
Course Outcomes:
1. Ability to apply solving and logical skills to programming in C/C++ language.
2. Able to apply various conditional expressions and looping statements to solve
problems associated with conditions.
3. Acquire knowledge about role of Functions involving the idea of modularity.
4. Understand and apply the Concept of Arrays, Strings and Pointers dealing with
memory management.
5. Acquire knowledge about basic data structures and their implementation.
INSTRUCTIONS TO STUDENTS
These are the instructions for the students attending the lab:
Before entering the lab the student should carry the following things (MANDATORY)
1. Identity card issued by the college.
2. Class notes
3. Lab observation book
4. Lab Manual
5. Lab Record
Student must sign in and sign out in the register provided when attending the lab
session without fail.
Come to the laboratory in time. Students, who are late more than 15 min., will not
be allowed to attend the lab.
Students need to maintain 100% attendance in lab if not a strict action will be taken.
All students must follow a Dress Code while in the laboratory
Foods, drinks are NOT allowed.
All bags must be left at the indicated place.
Refer to the lab staff if you need any help in using the lab.
Respect the laboratory and its other users.
Workspace must be kept clean and tidy after experiment is completed.
Read the Manual carefully before coming to the laboratory and be sure about what
you are supposed to do.
Do the experiments as per the instructions given in the manual.
Copy all the programs to observation which are taught in class before attending the
lab session.
Students are not permitted to use phones, Flash drives, Internet without permission
of lab-in charge.
Lab records need to be submitted on or before the date of submission.
CONTENTS
Page
Week Name of the Program No’s
a) Write a program to calculate simple interest(SI) for a given
1 principal (P), time (T), and rate of interest (R) (SI = P*T*R/100) 01
b) Write a program to find the roots of a quadratic equation.
c) Write a program to swap two variables values with and
without using third variable
d) Write a program to find both the largest and smallest
number in a list of integers.
e) Write a program to perform addition of two matrices.
f) Write a program to perform multiplication of two matrices.
What is a Compiler?
A compiler is a special program that processes statements written in a particular
programming language and turns them into machine language or "code" that a computer's
processor uses. Typically, a programmer writes language statements in a language such as
Pascal or C one line at a time using an editor. The file that is created contains what are
called the source statements. The programmer then runs the appropriate language
compiler, specifying the name of the file that contains the source statements.
Standardization of C language
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
4. To run the program select run from execute menu or press F10
Released by the Free Software Foundation. gcc is a Linux-based C compiler usually operated
via the command line. It often comes distributed with a Linux installation.
Compilation process of a C program
$gcc filename
The default executable output of gcc is "a.out",
Running the output file
$ ./a.out
It is also possible to specify a name for the executable file at the command line by using the
syntax -o outputfile , as shown in the following example : -
gcc filename -o outputfile
2. Compilation
[jayapalmedida@webminal.org ~]$gcc sample.c -o output
3. Running the program
[jayapalmedida@webminal.org ~]$./output
Hello
Week 1
1. a) Write a program to calculate simple interest(SI) for a given principal (P), time
(T), and rate of interest (R) (SI = P*T*R/100)
Flow chart
SAMPLE INPUT:
Input principle: 10000
Rate of interest: 12
Enter time(in years): 2
EXPECTED OUTPUT:
Simple interest = 2400
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise
1.Write a program to find distance when initial velocity, acceleration and time is given.
2.Write a program to find compound interest.
3. Write a program to find amount of memory required by different types of variables.
4. Write a program to evaluate algebraic expression (ax+b)/(ax-b).
calculate Interest(p,t,r);
int main()
{
double p, t, r, interest;
cout << "Enter principal amount, time and rate:";
cin >> p >> t >> r;
interest = calculateInterest(p, t, r);
cout << "Interest = " << interest << endl;
return 0;
}
double calculateInterest(p,t,r)
{
return (p*t*r)/100;
}
RECORD NOTES
2) #include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" <<
endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
RECORD NOTES
3) #include <iostream>
using namespace std;
int main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
4)#include<iostream>
using namespace std;
int main()
{
int a[50],i,n,large,small;
cout << “\nEnter the number of elements : “;
cin >> n;
cout << “\nInput the array elements : “;
for(i=0;i<n;++i)
cin >> a[i];
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
cout << “\nThe smallest element is ” << small << endl;
cout << “\nThe largest element is ” << large << endl;
return 0;
}
Faculty of Emerging Technologies, Sri Sri University 22 | P a g
e
Principle of Programming and Data Structure Lab B.TECH – I YEAR
RECORD NOTES
5) #include<iostream>
using namespace std;
void sum(int, int);
int main(){
int row, col;
cout<<"Enter the number of rows(should be >1 and <10): ";
cin>>row;
cout<<"Enter the number of column(should be >1 and <10): ";
cin>>col;
sum(row, col);
return 0;
}
void sum(int r, int c){
int m1[r][c], m2[r][c], s[r][c];
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<r;i++ ) {
for (int j = 0;j < c;j++ ) {
cin>>m1[i][j];
}
}
RECORD NOTES
6)
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}
cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}
return 0;
}
Week 2
2 a)Write a program to swap two variables values with and without using third variable
PROGRAM:
using a third variable
#include<stdio.h>
int main()
{
int x, y, t;
printf("Enter two integers: ");
scanf("%d%d", &x, &y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
}
SAMPLE INPUT:
Enter two integers: 10 20
EXPECTED OUTPUT:
Before Swapping
First integer = 10
Second integer = 20
After Swapping
First integer = 20
Second integer = 10
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
EXPECTED OUTPUT:
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Flow Chart
PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float disc, root1, root2;
float img,real;
printf("ENTER VALUES FOR a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
disc=(float)b*b-4*a*c;
if(disc>0)
{ printf("THE ROOTS ARE REAL & UNEQUAL:\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else if(disc==0)
{ printf("THE ROOTS ARE REAL AND EQUAL:\n");
root1=-b/(2*a);
root2=root1;
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else
{
printf("THE ROOTS ARE IMAGINARY:\n");
}
return 0;
}
SAMPLE INPUT:
ENTER VALUES FOR a, b, c
1 4 4
EXPECTED OUTPUT:
THE ROOTS ARE EQUAL AND THEY ARE.. Root1=-2 Root2=-2
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise:
1) Write a program to swap two variables values without using third variable using XOR(^)
operator.
2) Write a program to check whether the entered year is leap year or not (a year is leap if it
is divisible by 4 and divisible by 100 or 400)
3) Write a program to Check whether given number is even or odd through command line
4) Write a program to find largest of three numbers
5) Write a program to determine whether an input number is even or odd
RECORD NOTES
RECORD NOTES
RECORD NOTES
3) #include <iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cout<<"Enter any Number: ";
cin>>number;
for(i=1;i<=number;i++)
{
fact=fact*i;
}
cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
return 0;
}
RECORD NOTES
4)#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
int main()
{
int a = 98, b = 56;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
return 0;
}
Week 3
ALGORITHM:
Start
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0) Read n
Begin
Step 5: r←n%10
Step 6: sum← sum + r
Step 7: n←n/10
Sum = 0
End
Step 8: Print “sum”
Step 9: Stop
False
FLOWCHART: while
n!=0
True
r=n%10
um=sum+r
n=n/10
Print Sum
Stop
PROGRAM:
#include<stdio.h>
int main()
{
int n,r,sum=0;
printf("ENTER A POSITIVE INTEGER \n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUMOF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..%d", sum);
return 0;
}
SAMPLE INPUT:
ENTER A POSITIVE INTEGER
5321
EXPECTED OUTPUT:
THE SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..11
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Flowchart
Program:
#include<stdio.h>
int main()
{
int a,b,c;
char ch,t;
printf("ENTER TWO VALUES FOR a & b\n");
scanf("%d %d",&a,&b);
scanf(“%c”,&t); // to skip the newline character
printf("MENU OPTIONS \n");
printf("************\n");
printf("Addition\n");
printf("Subtraction\n");
printf("Multiplication\n");
printf("Division\n");
printf("Modulus\n");
printf("\n");
printf("ENTER Operator : \n");
scanf("%c",&ch);
switch(ch)
{
case ‘+’:c=a+b;
printf("The addition of %d and %d is..%d\n",a,b,c); break;
case ‘-‘:c=a-b;
printf("The subtraction of %d and %d is..%d\n",a,b,c); break;
case ‘*’:c=a*b;
printf("The multiplication of %d and %d is..%d\n",a,b,c); break;
case ‘/’:c=a/b;
printf("The division of %d and %d is..%d\n",a,b,c); break;
case ‘%’:c=a%b;
printf("The modulus of %d and %d is..%d\n",a,b,c); break;
default:printf("INVALID CHOICE\n"); }
}
return 0;
}
SAMPLE INPUT:
ENTER TWO VALUES FOR a & b: 20 16
EXPECTED OUTPUT:
MENU OPTIONS
*************
Addition
Subtraction
Multiplication
Division
Modulus
ENTER Operator : +
The addition of 20 and 16 is..36
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise:
1) Write a program to generate the first n terms of the Fibonacci using all loops.
2) Write a program to check whether given number is Armstrong number or not.
3) Write a program to find HCF and LCM using recursion.
4) Write a program to generate prime numbers from 1 to n.
5) Write a program to Check for a vowel using switch statement.
RECORD NOTES
RECORD NOTES
RECORD NOTES
RECORD NOTES
Week 4
4 a) Write a program to find both the largest and smallest number in a list
of integers
AIM: To find the largest and smallest number in a list of integers.
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
Read a[i]
Increment i
goto step 4
Step 5: small=a[0], large=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
If a[i]<small
Assign small=a[i]
If a[i]>large
Assign large=a[i]
Increment i goto Step 7
Step 8: print small, large
Step 9: stop
Start
Flow chart:
Read n
for i =0 to n-1
Read a[i]
small=large=a[0]
a[i]<small small=a[i]
large=a[i]
Print small
Print large
Stop
Program:
#include<stdio.h>
int main()
{
int a[10],i,n,small,large;
printf("Enter The Array Size:");
scanf("%d",&n);
printf("Enter The Array elements:");
for(i=0;i<n;i++)// read the elements of an array
scanf("%d",&a[i]);
small=a[0];
large=a[0];
for(i=1;i<n;i++)// read the elements of an array
{
if(a[i]<small)// check the condition for minimum value
small=a[i];
if(a[i]>large)//check the condition for maximum value
large=a[i];
}
printf("largest value is:%d\n",large);
printf("smallest value is:%d\n",small);
return 0;
}
SAMPLE INPUT:
Enter The Array Size: 10
ENTER THE ELEMENTS OF ARRAY
7 10 9 8 6 5 2 3 4 1
EXPECTED OUTPUT:
largest value is : 10
smallest value is : 1
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Flow chart:
Read A[i]
Sum=0
for i=0 to n-1
for i = 0 to n
sum=sum+*(A+i);
Print sum
Stop
PROGRAM:
#include<stdio.h>
int main( )
{
int A[50],sum=0,i,n;
printf("Enter how many values to read");
scanf("%d",&n);
printf("enter elements into array");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++)
sum=sum+*(A+i);
printf("the addition of array elements is %d",sum);
return 0;
}
SAMPLE INPUT:
Enter the array elements : 1 2 3 4 5 6 7 8 9 1
EXPECTED OUTPUT:
the addition of array elements is : 46
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise
1) Write a C program to generate the first n terms of the Fibonacci, use one dimensional
array to store the series.
2) Write a program to search for a given element in an array using linear search.
3)Write a program to find Fibonacci prime numbers.
4) Write a C Program to Sort the Array in an Ascending Order.
5) Write a program to count a total number of duplicate elements in an array.
RECORD NOTES
RECORD NOTES
RECORD NOTES
RECORD NOTES
Week 5
Flowchart:
PROGRAM:
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,p,q,r,s;
printf("ENTER ORDER OF A MATRIX\n");
scanf("%d%d",&p,&q);
printf("ENTER ORDER OF B MATRIX\n");
scanf("%d%d",&r,&s);
if(p==r&&q==s)
{
printf("ENTER A MATRIX\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&a[i][j]);
printf("ENTER B MATRIX\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
c[i][j]=a[i][j]+b[i][j];
printf(" After Addition of two matrices :\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
SAMPLE INPUT:
ENTER ORDER OF A MATRIX 2 2
ENTER ORDER OF B MATRIX 2 2
ENTER A MATRIX
12
34
ENTER B MATRIX
12
34
EXPECTED OUTPUT:
After Addition of two matrices :
2 4
6 8
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
scanf("%d",&a[i][j]);
printf("Enter Matrix B Values Row by Row\n");
for (i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
//logic for multiplication
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+= a[i][k]*b[k][j];
}
}
printf("A Matrix is :\n");
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("B Matrix is :\n");
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%5d",b[i][j]);
}
printf("\n");
}
printf("C Matrix is :\n");
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%5d",c[i][j]);
}
printf("\n");
}
}
return 0;
SAMPLE INPUT:
Enter the size of A Mtrix (Row and Col): 2 2
Enter the size of B Mtrix (Row and Col): 2 2
Enter Matrix Value Row by Row
1 0
2 6
Enter Matrix Value Row by Row
3 4
4 2
EXPECTED OUTPUT:
A matrix is:
1 0
2 6
B Matrix is:
3 4
4 2
C matrix is:
2 4
24 20
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise
1. Write a program to find whether given matrix is symmetric or not.
2. Write a Program that uses functions to perform transpose of a given Matrices.
3. Write a program to find sum of rows and columns of a Matrix.
RECORD NOTES
RECORD NOTES
RECORD NOTES
Week 6
Algorithm:
Step 1:start
start
Step 2:read string
Step 3: count=0;i=0
Step 4: if string*i+!=’\0’
count:= count +1
i:=i+1
goto step 4 read string
step 5:print count
step 6 stop
Flowchart: count=0;i=0
T
count:= count +1
i:=i+1
print count
stop
Program:
#include<stdio.h>
int string_Len(char*);
int main()
{
char str[20];
int length;
printf("\nEnter any string : ");
gets(str);
length = string_Len (str);
printf("The length of the given string %s is : %d", str, length);
return 0;
}
int string_Len (char *p) /* p=&str[0] */
{
int count = 0;
while (*p != '\0')
{ count++;
p++;
}
return count;
}
SAMPLE INPUT:
Enter the String : pritesh
EXPECTED OUTPUT:
Length of the given string pritesh is : 7
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Flow Chart:
PROGRAM:
#include <stdio.h>
int main()
{
char ch;
int i=0,wc=0,lc=0,cc=0;
printf("Enter text at end $");
while((ch=getchar())!='$')
{
if(ch==' ')
wc++;
else if(ch=='\n')
{
lc++;
wc++;
}
else
cc++;
}
printf("No. of Characters : %d\n",cc);
printf("No. of Words : %d\n",wc);
printf("No. of Lines : %d\n",lc);
return 0;
}
SAMPLE INPUT:
EXPECTED OUTPUT:
No. of Characters : 18
No. of Words : 6
No. of Lines : 3
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise:
RECORD NOTES
RECORD NOTES
RECORD NOTES
RECORD NOTES
Week 7
7 a) Write a program to find factorial of a given integer using non-recursive function and
recursive function.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Call fact(n) goto step 6
Step 4: Store result in “f”
Step 5: Print “f” goto step 10
Step 6: Begin //sub program
Initialize f ← 1
Step 7: for i is 1 to n by step 2
Step 8: Calculate f = f*i
Step 9: return “f”
End fact(n)
Step 10: Stop
FLOWCHART:
f=1
Start
for i=1 to n
Read n
f=f*i
f=fact(n)
Print f Return f
Stop
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: call sub program f=fact(n)
Step 4: print the f value
Step 5: stop
Sub program fact(n):
Step 1: if n=0 return 1 to main program
Step 2: return n*fact(n-1) to main program
FLOWCHART:
fact(n)
Start
If n=0
Read n
Stop
PROGRAM:
#include<stdio.h>
int fact(int);
int main()
{
int n,res;
printf("ENETR A NUMBER:");
scanf("%d",&n);
res=fact(n);
printf("THE FACTORIAL OF A GIVEN NUMBER IS..%d",res);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
SAMPLE INPUT:
ENTER A VALUE FOR n 5
EXPECTED OUTPUT:
THE FACTORIAL OF A GIVEN NUMBER IS..120
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
7 b) Write a program to find GCD of two integers using non-recursive function and
recursive function.
Aim:To find the GCD of two given integers by using the non recursive function
Description:
GCD means Greatest Common Divisor. i.e the highest number which divides the given
number
Ex: GCD(12,24) is 12
Formula: GCD= product of numbers/ LCM of numbers
Algorithm:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
Sub program:
Step 1: if b=0 return a to main program
Step 2: remainder=a%b
Step 3: a=b, b=remainder goto Step 1
Flowchart
Program:
#include<stdio.h>
int gcd(int a,int b);
int main()
{
int a,b;
int r,t;
printf("Enter any two integers");
scanf("%d%d",&a,&b);
r=gcd(a,b);
printf("GCD=%d",r);
return 0;
}
int gcd(int a,int b)
{
int t,rem;
while(1)
{
if(b>a)
{
t=a;
a=b;
b=t;
}
if(b==0)
return a;
else
{
rem=a%b;
a=rem;
}
}
}
SAMPLE INPUT:
enter the two numbers whose gcd is to be found:5,25
EXPECTED OUTPUT:
GCD of a,b is : 5
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Aim: To find the Gcd of two given integers by using the recursive function
Description: The greatest common divisor (gcd) of two or more integers, when at least
one of them is not zero, is the largest positive integer that divides thenumbers without a
remainder.
For example, the GCD of 8 and 12 is 4.
Algorithm:
Main program:
Step 1: start
Step 2: read a,b
Step 3: call the sub program GCD(a,b) for print the value
Step 4: stop
Sub program: GCD(n,m)
Program:
#include<stdio.h>
int gcdrecursive(int m,int n)
{ if(n>m)
return gcdrecursive(n,m);
if(n==0)
return m;
else
int a,b;
printf("enter the two numbers whose gcd is to be found:");
scanf("%d%d",&a,&b);
printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub program
return 0;
}
SAMPLE INPUT:
Enter the two numbers whose gcd is to be found: 5 25
EXPECTED OUTPUT:
GCD of a,b is : 5
Record at least 2 results
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise:
RECORD NOTES
RECORD NOTES
RECORD NOTES
RECORD NOTES
Week 8
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
EXPECTED OUTPUT:
The length of the string 'madam' = 5
madam is a palindrome
Record at least 2 results
Signatu
re of
faculty
with
date
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Assessment
Not Satisfactory
Needs Improvement
Partially Executed
Executed Successfully
Signature of faculty with date
Exercise:
1.Write a program which consists of user defined function to reverse a string
2.Design a user defined function to convert decimal number to binary.
3. Design a user defined function to print triangle as shown below
*
**
***
****
4. Design a user defined function to print triangle as shown below
1
12
123
1234
5. Design a user defined function to generate first n prime numbers.
RECORD NOTES
RECORD NOTES
RECORD NOTES