lab manual - laxmishabadi.files.wordpress.com€¦ · computer programming laboratory [as per...

42
B.L.D.E.A’s Vachana Pitamaha Dr. P. G. Halakatti College of Engineering and Technology, Vijayapur-586103. Department of Computer Science and Engineering Lab Manual Subject : Computer Programming Laboratory Subject Code : 15CPL16/15CPL26 Class : I / II Semester B.E. 2016-17

Upload: others

Post on 18-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

1 Dept. of CSE

B.L.D.E.A’s Vachana Pitamaha Dr. P. G. Halakatti College of Engineering and

Technology, Vijayapur-586103.

Department of Computer Science and Engineering

Lab Manual

Subject : Computer Programming Laboratory

Subject Code : 15CPL16/15CPL26

Class : I / II Semester B.E.

2016-17

Page 2: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

2 Dept. of CSE

Page 3: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

3 Dept. of CSE

COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme]

(Effective from the academic year 2015 -2016) SEMESTER - I/II

Laboratory Code 15CPL 16 / 15CPL26 IA Marks 20

Number of Lecture Hours/Week 01Hr Tutorial (Instructions) Exam Marks 80 + 02 Hours Laboratory

Total Number of Lecture Hours 48 Exam Hours 03

CREDITS - 02 Course objectives: To provide basic principles C programming language. To provide design & develop of

C programming skills. To provide practical exposures like designing flowcharts, algorithms, how to debug

programs etc.

Expt.

No. Experiment Name

1

Design and develop a flowchart or an algorithm that takes three coefficients (a, b,

and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots.

Implement a C program for the developed flowchart/algorithm and execute the same to

output the possible roots for a given set of coefficients with appropriate messages.

2

Design and develop an algorithm to find the reverse of an integer number NUM

and check whether it is PALINDROME or NOT. Implement a C program for the

developed algorithm that takes an integer number as input and output the

reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a

Palindrome

3

3a. Design and develop a flowchart to find the square root of a given number N.

Implement a C program for the same and execute for all possible inputs with

appropriate messages. Note: Don’t use library function sqrt(n).

3b. Design and develop a c program to read a year as an input and find whether it is

leap year or not. Also consider end of the centuries.

4

Design and develop an algorithm to evaluate polynomial f(x) = a4x4

+ a3x3

+

a2x2 + a1x + a0, for a given value of x and its coefficients using Horner’s

method. Implement a C program for the same and execute the program with different set

of values of coefficients and x.

5

Draw the flowchart and Write a C Program to compute Sin(x) using Taylor

series approximation given by Sin(x) = x - (x3/3!) + (x

5/5!) - (x

7/7!) + …….

Compare your result with the built- in Library function. Print both the results

with appropriate messages.

6 Develop an algorithm, implement and execute a C program that reads N integer

numbers and arrange them in ascending order using Bubble Sort.

7

Develop, implement and execute a C program that reads two matrices A (m x

n ) and B (p x q ) and Compute product of matrices A and B. Read matrix A and

matrix B in row major order and in column major order respectively. Print both the

input matrices and resultant matrix with suitable headings and output should be in

matrix format only. Pr o gr a m m u st check the compatibility of orders of the

matrices for multiplication. Report appropriate message in case of incompatibility.

Page 4: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

4 Dept. of CSE

Expt.

No. Experiment Name

8 Develop, implement and execute a C program to search a Name in a list of names

using Binary searching Technique.

9

Write and execute a C program that,

i.) Implements string copy operation STRCOPY (str1, str2) that copies a string

str1 to another string str2 without using library function.

ii) Read a sentence and print frequency of vowels and total count of consonants.

10

10a. Design and develop a C function RightShift(x ,n) that takes two integers x

and n a s i n p u t a n d returns value of the integer x rotated to the right by n

positions. Assume the i n t e g e r s are unsigned. Wr i t e a C program that invokes

this function with different values for x and n and tabulate the results with suitable

headings.

10b. Design and develop a C function isprime(num) that accepts an integer argument

and returns 1 if the argument is prime, a 0 otherwise. Write a C program that

invokes this function to generate prime numbers between the given range.

11

Draw the flowchart and write a recursive C function to find the factorial of a

number, n!, defined by fact(n)=1, if n=0. Otherwise fact (n) =n*fact (n-1). Using

this function, write a C program to compute the binomial coefficient nCr . Tabulate

the results for different values of n and r with suitable messages.

12

Given two university information files “studentname.txt” and “usn.txt” that

contains students Name and USN respectively. Write a C program to create a

new file called “output.txt” and copy the content of files “studentname.txt” and

“usn.txt” into output file in the sequence shown below. Display the contents of

output file “output.txt” on to the screen.

Student Name USN

Name1 USN

Name2 USN

… …

… …

13

Write a C program to maintain a record of n student details using an array of

structures with four fields (Roll number, Name, Marks, and Grade). Assume

appropriate data type for each field. Print the marks of the student, given the student

name as input.

14 Write a C program using pointers to compute the sum, mean and standard

deviation of all elements stored in an array of n real numbers.

Course outcomes:

• Gaining Knowledge on various parts of a computer.

• Able to draw flowcharts and write algorithms

• Able design and development of C problem solving skills.

• Able design and develop modular programming skills.

• Able to trace and debug a program

Heading

Page 5: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

5 Dept. of CSE

Program 1: Design and develop a flowchart or an algorithm that takes three coefficients

(a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots.

Implement a C program for the developed flowchart/algorithm and execute the same to

output the possible roots for a given set of coefficients with appropriate messages.

******************************************************************

Algorithm for finding roots of a Quadratic equation

Step 1 : Start

Step 2 : Read a,b,c

Step 3 : if a!=0 then goto Step 4

Otherwise goto Step 20

Step 4 : desc = b2- 4ac

Step 5 : if(desc>0) then goto Step 6

Otherwise goto Step 10

Step 6 : printf “roots are real and distinct” and stop

Step 7 : root1 = descb /2a

Step 8 : root2 = descb /2a

Step 9 : print root1, root2 and stop

Step 10 : if(desc<0) then goto Step 11

Otherwise goto Step 16

Step 11 : print “ Roots are imaginary”

Step 12 : root1 = -b/2a

Step 13 : root2 = a2/desc

Step 14 : print root1 “+i” root1

Step 15 : print root2 “-i” root2 and stop

Step 16 : print “Roots are real and equal”

Step 17 : root1 = -b/2a

Step 18 : root2 = root1

Step 19 print root1, root2 and stop.

Step 20: print “Given equation is not quadratic equation”

Step 21: Stop

Page 6: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

6 Dept. of CSE

Yes

No No

Yes

No

Yes

Roots are real & distinct

r1 = - b + sqrt ( (desc) ) / (2 * a)

r2 = - b – sqrt ((desc) ) / (2 * a)

Print r1 & r2

r1 = r2= - b/2 * a

Print r1 & r2

Roots are real & equal

Stop

S

Read a, b & c

Start

Is desc > 0 ?

Is desc<0

Roots are imaginary

r1 = - b / (2 * a)

r2 = sqrt (desc) / (2 * a)

Print r1 ”+ i “root2

Print r1 ”- i “root2

Is a != 0

?

desc = b2 - 4ac

Print “The given coefficients do not form a

quadratic”

S

Print “The given coefficients form a quadratic”

Page 7: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

7 Dept. of CSE

/*C Program to find all the roots of a quadratic equation for non zero coefficients*/

#include<stdio.h>

#include<math.h>

void main()

{

float a, b, c, d, r1, r2;

clrscr();

printf("\n Enter the coefficients (a, b & c) of a quadratic equation:");

scanf("%f%f%f", &a, &b, &c);

if(a!=0)

{

printf(“\n The given co efficients form a quadratic equation”);

d=(b*b)-(4.0 *a*c);

if(d>0)

{

printf("\n Roots are real and distinct");

r1=(-b + sqrt(d)) / (2.0 *a);

r2=(-b - sqrt(d)) / (2.0 *a);

printf("\n Root1=%f \t Root2= %f", r1, r2);

}

else if(d = =0)

{

printf("\n Roots are real and equal");

r1=-b / (2.0 *a);

r2=r1;

printf(" \n Root1=%f \t Root2=%f ",r1,r2);

}

else

{

printf("\nRoots are imaginary");

r1=-b / (2.0 *a);

r2=sqrt (abs(d)) / (2.0 *a);

printf("\n Root1= %f + i %f \t Root2= %f –i %f ",r1, r2, r1, r2);

}

}

else

printf("\n The given co-efficients do not form a quadratic equation ");

getch();

}

Page 8: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

8 Dept. of CSE

OUTPUT:

RUN 1

Enter the coefficients (a, b & c) of a quadratic equation: 1 2 1

The given co efficients form a quadratic equation

Roots are real and equal

Root1=-1.000000

Root2=-1.000000

RUN 2

Enter the coefficients (a, b & c) of a quadratic equation: 1 2 3

The given co efficients form a quadratic equation

Roots are Imaginary

Root1=-1.000000+i1.414214

Root2=-1.000000-i1.414214

RUN 3

Enter the coefficients (a, b & c) of a quadratic equation: 1 5 6

The given co efficients form a quadratic equation

Roots are real and distinct

Root1=-2.000000

Root2=-3.000000

RUN 4

Enter the coefficients (a, b & c) of a quadratic equation: 0 2 2

The given co efficients do not form a quadratic equation

Page 9: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

9 Dept. of CSE

Program 2: Design and develop an algorithm to find the reverse of an integer number

NUM and check whether it is PALINDROME or NOT. Implement a C program for the

developed algorithm that takes an integer number as input and output the reverse of the

same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome.

******************************************************************

/*Algorithm to check whether an integer number is a palindrome or not*/

Step 1: Start

Step 2: read num

Step 3: dup=num, rev=0

Step 4: if(num!=0) then goto next step 5 otherwise goto Step 6

Step 5: rev=(rev * 10)+(num % 10)

num=num / 10

goto Step 4

Step 6: if(rev = = dup) then print “Not a Palindrome”

otherwise print “Palindrome”

Step 7: Stop

Page 10: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

10 Dept. of CSE

/* C program to check whether an integer number NUM is a palindrome or not */

#include<stdio.h>

#include<conio.h>

void main()

{

int num, rev=0, dup, rem;

clrscr();

printf("\n Enter an integer number ");

scanf("%d", &num);

dup=num;

while (num!=0)

{

rem=num%10;

rev=(rev*10)+rem;

num=num/10;

}

printf("\n The reverse of %d is %d", dup, rev);

if(dup= =rev)

printf("\n Number %d is a palindrome", dup);

else

printf("\n Number %d is not a palindrome", dup);

getch();

}

Page 11: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

11 Dept. of CSE

OUTPUT:

RUN 1

Enter an integer number 1221

The reverse of 1221 is 1221

Number 1221 is a palindrome

RUN 2

Enter an integer number 2014

The reverse of 2014 is 4102

Number 2014 is not a palindrome

Page 12: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

12 Dept. of CSE

3a) Design and develop a flowchart to find the square root of a given number N. Implement

a C program for the same and execute for all possible inputs with appropriate messages.

****************************************************************************

No

print “temp”

Stop

S

c

No

Yes

No

Yes Is

pro==num

?

c

No

Yes

mid=(min+max)/2, pro=mid*mid

Start

Read num

Min=0, Max=num

num<0

Print “Error: Negative Value”

Yes

S max-min >0.000001

Min=mid

pro>num

Max=mid

Page 13: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

13 Dept. of CSE

#include<stdio.h>

void main()

{

float num,min,max,mid,pro;

clrscr();

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

scanf("%f",&num);

min=0;

max=num;

if (num<0)

{

printf("\n Negative Number");

getch();

exit(0);

}

while (max-min>0.000001)

{

mid=(min+max)/2;

pro=mid*mid;

if (pro==num)

break;

else if (pro>num)

max=mid;

else if (pro<num)

min=mid;

}

printf("\nsqaure root=%f",mid);

getch();

}

******************************************************************************

Output:

Run 1:

Enter the number : 49

Square root = 7.000000

Run 2:

Enter the number : 8

Square root = 2.828427

Run 2:

Enter the number : -5

Negative number

Page 14: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

14 Dept. of CSE

Page 15: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

15 Dept. of CSE

3b) Design and develop a C program to read a year as an input and find whether it is leap

year or not. Also consider end of the centuries.

****************************************************************************

#include<stdio.h>

void main()

{

int year;

clrscr();

printf("Enter any year: ");

scanf("%d",&year);

if(((year%4==0)&&(year%100!=0))||(year%400==0))

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

else

printf("\n%d is not a leap year",year);

getch();

}

---------------------------------------------------------------------------------------------------------------------

Output:

Run 1:

Enter any year: 1972

1972 is a leap year

Run 2:

Enter any year: 2014

2014 is not a leap year

To know about Leap Year

A normal year has 365 days.

A Leap Year has 366 days (the extra day is the 29th of February).

How to know if a year will be a Leap Year:

Leap Years are any year that can be evenly divided by 4 (such as 2012, 2016, etc)

except if it can can be evenly divided by 100, then it isn't (such as 2100, 2200, etc)

except if it can be evenly divided by 400, then it is (such as 2000, 2400)

Why?

Because the Earth rotates about 365.242375 times a year ...

... but a normal year is 365 days, ...

... so something has to be done to "catch up" the extra 0.242375 days a year.

So every 4th year we add an extra day (the 29th of February), which makes 365.25 days a year. This is fairly close,

but is wrong by about 1 day every 100 years.

So every 100 years we don't have a leap year, and that results in 365.24 days per year (1 day less in 100 year = -

0.01 days per year). Closer, but still not accurate enough!

Page 16: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

16 Dept. of CSE

So another rule says that every 400 years is a leap year again. This gets us 365.2425 days per year (1 day regained

every 400 years = 0.0025 days per year), which is close enough to 365.242375 not to matter much.

Program 4: Design and develop an algorithm for evaluating the polynomial f(x) = a4x4

+a3x3 + a2x

2 + a1x + a0, for a given value of x and its coefficients using Horner’s method.

Implement a C program for the developed algorithm and execute for different sets of

values of coefficients and x.

******************************************************************

/*Algorithm evaluating a polynomial by using Horner’s method*/ Step1 : Start

Step 2: Read n

Step 2: read n coefficients

Step 3: for i=0 to n

read a[i]

Step 4: read x

Step 5: for(i=n;i>=1;i--)

sum = (sum + a[i]) * x;

Step 6: sum=sum+a[0];

Step 7: print “Result of polynomial is”

Step 8: print sum

Step 9: Stop

Know about Horner’s method to solve polynomial

Evaluation of a polynomial in a straight forward method.

Given polynomial : 10+5x+4x2+3x

3+8x

4+9x

5

No. of multiplications : 15

No. of additions : 5

Horner’s method of solving polynomial equation

Given polynomial : 10+5x+4x2+3x

3+8x

4+9x

5

The above polynomial can be written as

10+x(5+x(4+x(3+x(8+9x))))

No. of multiplications : 5

No. of additions : 5

Page 17: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

17 Dept. of CSE

/* Program to evaluate a given polynomial using Horner’s method*/

#include<stdio.h>

void main()

{

int n, x, i, sum=0, a[25];

clrscr();

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

scanf("%d", &n);

printf("\n Enter the coefficients:");

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

scanf("%d",&a[i]);

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

scanf("%d",&x);

for(i=n;i>=1;i--)

sum = (sum + a[i]) * x;

sum=sum+a[0];

printf("\n The polynomial evaluates to = %d",sum);

getch();

}

OUTPUT:

RUN1

Enter the value of n: 3

Enter the coefficients: 1 2 3 4

Enter the value of x: 2

The polynomial evaluates to = 49

RUN 2

Enter the value of n: 5

Enter the coefficients: 1 2 3 4 5 6

Enter the value of x: 1

The polynomial evaluates to = 21

Page 18: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

18 Dept. of CSE

Page 19: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

19 Dept. of CSE

Program 5: Write C Program to compute Sin(x) using Taylor series approximation given

by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare the result with the built- in

Library function and print both the results. ******************************************************************

/*Program to Implement Taylor’s Series*/ #include<stdio.h>

#include<math.h>

void main()

{

float x,sin_val;

int n,i,j,fact;

clrscr();

printf("\nenter the value of n & x");

scanf("%d%f",&n,&x);

sin_val=0.0;

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

{

fact=1;

j=2*i+1;

while(j>0)

{

fact=fact*j;

j=j-1;

}

sin_val=sin_val+(float)(pow(x, 2*i+1)*pow(-1,i))/(float)(fact);

}

printf("%f”,sin_val);

printf("\n value of built- in- function sin (%f)=%f",x,sin(x));

printf("\n comparision of result & built in function value=%f",(sin_val-sin(x)));

getch();

}

Output:

Run 1:

Enter the value of n & x : 5 0.5

0.479426

Comparison of result & built in function value= -0.000000

Run 2:

Enter the value of n & x : 3 0.2

0.198669

Value of built-in-function sin(0.200000 )=0.198669

Comparison of result & built in function value=0.000000

Page 20: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

20 Dept. of CSE

Program 6: Develop an algorithm, implement and execute a C program that reads N

integer numbers and arrange them in ascending order using Bubble Sort.

******************************************************************

//Algorithm to sort n numbers using Bubble sort technique

Step 1: read n

Step 2: read n array values

Step 3 : for i=0 to n

read a[i]

Step 5: for i=0 to n-1

for j=0 to n-i-1

if(a[j]>a[j+1])

temp=a[j], a[j]=a[j+1], a[j+1]=temp

Step 6: print “After sorting”

Step 7: for i=0 to n-1

print a[i]

Step 8: Stop

Page 21: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

21 Dept. of CSE

/* Program to sort n numbers using Bubble sort technique */ #include<stdio.h>

void main()

{

int temp, a[25], n, i, j;

clrscr();

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

scanf("%d", &n);

printf("\n Enter %d integer numbers ", n);

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

scanf("%d", &a[i] );

for(i=0; i<n; i++) /* Bubble sort implementation */

{

for(j=0; j<n-i-1 ;j++)

{

if (a[ j ]> a[ j+1 ])

{

temp=a[j];

a[j]=a[ j+1];

a [j+1]= temp;

}

}

}

printf(" \n \n After Sorting \n");

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

printf("\t %d", a[ i ]);

getch();

} ----------------------------------------------------------------------------------------------------

OUTPUT: RUN 1

Enter the value of n 5

Enter 5 integer numbers 5 4 3 2 1

After sorting

1 2 3 4 5

RUN 2

Enter the value of n 4

Enter 5 integer numbers 50 10 20 15

After Sorting

10 15 20 50

Page 22: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

22 Dept. of CSE

Program 7: Develop, implement and execute a C program that reads two matrices

A (m x n) and B (p x q ) and Compute the product A and B. Read matrix A in row major

order and matrix B in column major order. Print both the input matrices and resultant

matrix with suitable headings and in matrix format. Program must check the compatibility

of orders of the matrices for multiplication. Report an appropriate message in case of

incompatibility.

****************************************************************************

/* Program to find product of two matrices A and B */

# include <stdio.h>

void main()

{

int i, a[10][10], b[10][10], c[10][10], p, q, n, m, k, j;

clrscr();

printf("\n Enter the order of matrix A: ");

scanf("%d %d", &m, &n);

printf("\n Enter the order of matrix B: ");

scanf ("%d %d", &p, &q);

if(n!=p)

printf("\n\n Multiplication of A and B is not possible");

else

{

printf("\n\n Multiplication of A and B is possible");

printf("\n\n Enter the elements of A");

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

{

for (j=0; j<n; j++)

scanf("%d", &a[i][j]);

}

printf("\n\n The elements of A are ");

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

{

printf("\n\t");

for (j=0; j<n; j++)

printf("\t%d", a[i][j]);

}

printf("\n\n Enter the elements B");

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

{

for (j=0; j<q; j++)

scanf("%d", &b[i][j]);

}

Page 23: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

23 Dept. of CSE

printf("\n\n The elements of B are ");

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

{

printf("\n\t");

for (j=0; j<q; j++)

printf("\t%d", b[i][j]);

}

//MULTIPLICATION PROCESS

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("\n\n The elements of resultant matrix are");

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

{

printf ("\n\t" );

for(j=0; j<q; j++)

printf("\t %d ", c[i][j]);

}

}

getch();

}

---------------------------------------------------------------------------------------------------------------------

Output:

Run 1:

enter the order of matrix A: 2 3

enter the order of matrix B:3 2

enter the elements of A 1 2 3 4 5 6

the elements of A are

1 2 3

4 5 6

enter the elements B 1 2 3 4 5 6

the elements of B are

1 2

3 4

5 6

multiplication of A and B is possible

the elements of resultant matrix are

22 28

49 64

Page 24: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

24 Dept. of CSE

Run 2:

enter the order of matrix A:3 2

enter the order of matrix B:3 2

multiplication of A and B is not possible

Run 3:

enter the order of matrix A:2 2

enter the order of matrix B:2 2

enter the elements of A 1 2 3 4

the elements of A are

1 2

3 4

enter the elements B1 2 3 4

the elements of B are

1 2

3 4

multiplication of A and B is possible

the elements of resultant matrix are

7 10

15 22

Page 25: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

25 Dept. of CSE

Program 8: Develop, implement and execute a C program to search a Name in a list of

names using Binary searching Technique.

****************************************************************************

#include<stdio.h>

void main()

{

char arr[25][20], key[25];

int i, n, low, mid, high;

clrscr();

printf("Enter the number of students name you want to enter \n");

scanf("%d", &n);

printf("Enter %d names in ascending order \n", n);

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

scanf("%s", arr[i]);

low = 0;

high = n-1;

printf("Enter name to be searched : \n");

scanf("%s", key);

while(low <= high)

{

mid = (low+high) / 2;

if((strcmp(arr[mid], key)) == 0)

{

printf("Name is found at %d position", mid+1);

getch();

exit(0);

}

else if((strcmp(arr[mid], key))< 0)

low = mid + 1;

else if ((strcmp(arr[mid], key))>0)

high = mid - 1;

}

printf("Name is not found \n");

getch();

}

---------------------------------------------------------------------------------------------------------------------

Page 26: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

26 Dept. of CSE

Output:

Run 1:

Enter the number of students name you want to enter 3

Enter 3 names in ascending order

Anil

Sunil

Tom

Enter name to be search

Sunil

Name is found at 2 position

Run 2:

Enter the number of students name you want to enter 3

Enter 3 names in ascending order

Anil

Sunil

Tom

Enter name to be search

John

Name is not found

Page 27: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

27 Dept. of CSE

Program 9a: Write and execute a C program that Implements string copy operation

STRCOPY (str1, str2) that copies a string str1 to another string str2 without using library

function.

****************************************************************************

#include<stdio.h>

void STRCOPY(char str1[], char str2[])

{

int i;

for(i=0;str1[i]!= '\0';i++)

{

str2[i]=str1[i];

}

str2[i] = '\0';

printf("\n Copied String is => %s", str2);

}

void main()

{

char str1[100], str2[100];

clrscr();

printf("Enter the String Which is to be copied\n");

gets(str1);

STRCOPY(str1,str2);

getch();

}

---------------------------------------------------------------------------------------------------------------------

Output:

Run 1:

Enter the String Which is to be copied

Engineering

Copied String is => Engineering

Run 2:

Enter the String Which is to be copied

Technology

Copied String is => Technology

Run 3:

Enter the String Which is to be copied

Engineering and Technology

Copied String is => Engineering and Technology

Page 28: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

28 Dept. of CSE

9b: Reads a sentence and prints frequency of each of the vowels and total count of

consonants.

****************************************************************************

#include<stdio.h>

void main()

{

char str[100];

int i, acnt=0, ecnt=0, icnt=0, ocnt=0, ucnt=0, ccnt=0;

clrscr();

printf("Enter the sentence\n");

gets(str);

for(i=0;str[i]!='\0';i++)

{

switch(str[i])

{

case 'A':

case 'a': acnt++;

break;

case 'E':

case 'e': ecnt++;

break;

case 'I':

case 'i': icnt++;

break;

case 'O':

case 'o': ocnt++;

break;

case 'U':

case 'u': ucnt++;

break;

default : if (str[i]!=’ ‘)

ccnt++;

}

}

printf("A vowel count is %d\n", acnt);

printf("E vowel count is %d\n", ecnt);

printf("I vowel count is %d\n", icnt);

printf("O vowel count is %d\n", ocnt);

printf("U vowel count is %d\n", ucnt);

printf("Constants count is %d\n", ccnt);

getch();

}

Page 29: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

29 Dept. of CSE

Output:

Run 1:

Enter the sentence: INDIA IS GREAT

A vowel count is 2

E vowel count is 1

I vowel count is 3

O vowel count is 0

U vowel count is 0

Constants count is 6

Run 2:

Enter the sentence C is structured language

A vowel count is 2

E vowel count is 2

I vowel count is 1

O vowel count is 0

U vowel count is 3

Constants count is 13

Page 30: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

30 Dept. of CSE

10. C function RightShift(x ,n) that takes two integers x and n as input and returns value of

the integer x rotated to the right by n positions. Assume the integers are unsigned. Write a

C program that invokes this function with different values for x and n and tabulate the

results with suitable headings.

****************************************************************************

/*C Program to Implement Right Shift operator */

#include<stdio.h>

unsigned int rightshift(unsigned int x, unsigned int n)

{

int y;

if(sizeof(y)==2)

{

printf("16 bits machine\n");

return((x>>n)|(x<<(16-n)));

}

else

{

printf("32 bits machine\n");

return((x>>n)|(x<<(32-n)));

}

}

void main()

{

int x, n;

clrscr();

printf("Enter the values of x and n\n");

scanf("%u %u", &x, &n);

printf("value of %u after performing %u right shift operations is %u\n", x, n, rightshift(x, n));

getch();

}

Output:

Run 1:

Enter the values of x and n 2 3

16 bits machine

Value of 2 after performing 3 right shift operations is 16384

Run 2:

Enter the values of x and n 200 3

16 bits machine

Value of 200 after performing 3 right shift operations is 25

Page 31: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

31 Dept. of CSE

Output:

Run 1:

Enter the values of x and n 2 3

16 bits machine

Value of 2 after performing 3 right shift operations is 16384

Run 2:

Enter the values of x and n 200 3

16 bits machine

Value of 200 after performing 3 right shift operations is 25

Run 3:

Enter the values of x and n 10 2

16 bits machine

Value of 10 after performing 2 right shift operations is 32770

Page 32: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

32 Dept. of CSE

10b. Design and develop a C function isprime(num) that accepts an integer argument and

returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this

function to generate prime numbers between the given ranges.

******************************************************************************

/*C Program to Generate a series of Prime numbers in a given range from low to high */

#include<stdio.h>

int flag = 1;

int isprime(int x)

{

int i;

for(i=2; i<=x/2; i++)

{

if(x%i == 0)

{

flag = 0;

break;

}

}

return flag;

}

void main()

{

int low, high, i, r;

clrscr();

printf("enter a number greater than 0\n");

scanf("%d%d", &low, &high);

if(low>1)

{

for(i=low; i<=high; i++)

{

r = isprime(i);

if(r = = 1)

printf("%d is a Prime Number", i);

else

flag=1;

}

}

else

printf(“\nLow value should be greater than 1”);

getch();

}

Page 33: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

33 Dept. of CSE

Output:

Run 1:

Enter the range of numbers (low & high) greater than 0: 2 20

2 is a Prime Number

3 is a Prime Number

5 is a Prime Number

7 is a Prime Number

11 is a Prime Number

13 is a Prime Number

17 is a Prime Number

19 is a Prime Number

Run 2:

Enter a number greater than 0: 10 25

11 is a Prime Number

13 is a Prime Number

17 is a Prime Number

19 is a Prime Number

23 is a Prime Number

Page 34: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

34 Dept. of CSE

11. Draw the flowchart and write a recursive C function to find the factorial of a number,

n!, defined by fact(n)=1, if n=0. Otherwise fact (n) =n*fact (n-1). Using this function, write a

C program to compute the binomial coefficient nCr . Tabulate the results for different

values of n and r with suitable messages.

****************************************************************************

ncr = fact(n)/((fact(r) * fact(n-r));

Read n & r values

Start

Display ncr

Stop

Yes

No

int fact(n)

n==1||

n ==0

Return (1)

Return (n*fact(n-1)

Page 35: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

35 Dept. of CSE

#include<stdio.h>

long int fact(int n)

{

if(n==0 || n==1)

return 1;

return(n*fact(n-1));

}

void main()

{

long int n, r, ncr;

clrscr();

printf("Enter the values of n and r\n");

scanf("%ld %ld", &n, &r);

ncr = fact(n)/(fact(r)*fact(n-r));

printf("The %d and %d of ncr is %ld\n", n, r, ncr);

getch();

}

---------------------------------------------------------------------------------------------------------------------

Output:

Run 1:

Enter the values of n and r: 10 5

The 10 and 5 of ncr is 252

Run 2:

Enter the values of n and r: 5 3

The 5 and 3 of ncr is 10

Run 3:

Enter the values of n and r: 10 3

The 10 and 3 of ncr is 120

Page 36: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

36 Dept. of CSE

Program 12: Given two university information files “studentname.txt” and “usn.txt” that

contains students Name and USN respectively. Write a C program to create a new file

called “output.txt” and copy the content of files “studentname.txt” and “usn.txt” into

output file in the sequence shown below. Display the contents of output file “output.txt” on

to the screen.

Student Name USN

Name 1 USN1

Name 2 USN2

…. ….

…. ….

****************************************************************************

#include<stdio.h>

void main()

{

FILE *fp1,*fp2,*fp3;

char name[25],usn[25];

fp1=fopen("students.txt","r");

fp2=fopen("usn.txt","r");

fp3=fopen("output.txt","w");

if(fp1==NULL||fp2==NULL||fp3==NULL)

{

printf("Error in opening files!");

getch();

exit(0);

}

clrscr();

while(!feof(fp1) && !feof(fp2))

{

fscanf(fp1,"%s",name);

fscanf(fp2,"%s",usn);

fprintf(fp3,"\n%s\t%s",name,usn);

}

fclose(fp1);

fclose(fp2);

fclose(fp3);

fp3=fopen("output.txt","r");

printf("\nSTUDENTS NAME\tUSN");

while(!feof(fp3))

{

fscanf(fp3,"%s\t%s",name,usn);

printf("\n%s\t\t%s",name,usn);

}

fclose(fp3);

getch();

}

---------------------------------------------------------------------------------------------------------------------

Heading

Page 37: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

37 Dept. of CSE

Output:

Run 1:

Student Name USN

Amar 2BL14CS001

Akbar 2BL14CS002

Anthony 2BL14CS003

Run 2:

Error in opening files

Page 38: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

38 Dept. of CSE

Program 13: Write a C program to maintain a record of “n” student details using an array

of structures with four fields (Roll number, Name, Marks, and Grade). Each field is of an

appropriate data type. Print the marks of the student given student name as input.

**************************************************************************** #include<stdio.h>

#include<conio.h>

#include<string.h>

struct student

{

int roll;

char name[50];

int marks;

char grade;

}s[50];

void main()

{

char name[50];

int i, n;

clrscr();

printf("Enter the number of Students:\n");

scanf("%d", &n);

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

{

printf("\nEnter Student details:\n");

printf("Roll number:");

scanf("%d",&s[i].roll);

printf("\nName:");

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

printf("\nMarks:");

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

printf("\nGrade:");

fflush(stdin);

scanf(" %c",&s[i].grade);

}

printf("\nEnter student name to be searched:");

scanf("%s",name);

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

{

if(strcmp(s[i].name, name) == 0)

{

printf("\n Record Found!");

printf("\n Marks= %d",s[i].marks);

getch();

exit(0);

}

}

printf("\n Record Not Found!!!\n");

Page 39: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

39 Dept. of CSE

getch();

}

Output:

Run1:

Enter the number of Students: 3

Enter Student details:

Roll number: 1

Name: Tom

Marks: 600

Grade: A

Enter Student details:

Roll number: 2

Name: Wilson

Marks: 700

Grade: H

Enter Student details:

Roll number: 3

Name: Bob

Marks: 800

Grade: H

Enter student name to be searched: Wilson

Record Found!

Marks= 700

Run2:

Enter the number of Students: 2

Enter Student details:

Roll number: 1

Name: Tom

Marks: 600

Grade: A

Enter Student details:

Roll number: 2

Name: Wilson

Marks: 700

Grade: H

Enter student name to be searched: Bob

Record Not Found!!!

Page 40: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

40 Dept. of CSE

Page 41: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

41 Dept. of CSE

Program 14: Write a C program using pointers to compute the sum, mean and standard

deviation of all elements stored in an array of n real numbers.

****************************************************************************

#include<stdio.h>

#include<alloc.h>

#include<math.h>

void main()

{

int i, n ;

float *arr;

float deviation, sum=0, sumsqr=0, mean, variance, stddeviation;

clrscr();

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

scanf("%d", &n);

printf("Enter n elements\n");

arr = (float*)malloc(n*sizeof(float));

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

scanf("%f", (arr+i));

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

sum = sum + (*(arr+i));

mean = sum/(float)n;

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

{

deviation = *(arr+i) - mean;

sumsqr += deviation * deviation;

}

variance = sumsqr/(float)n;

stddeviation = sqrt(variance);

printf("\n Number of items : %d\n", n);

printf("Sum : %f \n", sum);

printf("Mean : %f \n", mean);

printf("Variance : %f \n", variance);

printf("Standard deviation : %f \n", stddeviation);

free(arr);

getch();

}

Page 42: Lab Manual - laxmishabadi.files.wordpress.com€¦ · COMPUTER PROGRAMMING LABORATORY [As per Choice Based Credit System (CBCS) scheme] (Effective from the academic year 2015 -2016)

42 Dept. of CSE

Output:

Run 1:

Enter the value of n 5

Enter n elements 1 5 3 4 2

Number of items : 5

Sum : 15.000000

Mean : 3.000000

Variance = 2.000000

Standard deviation: 1.414214

Run 1:

Enter the value of n 5

Enter n elements 10 20 30 40 50

Number of items : 5

Sum : 150.000000

Mean : 30.000000

Variance = 200.000000

Standard deviation: 14.142136