0% found this document useful (0 votes)
15 views50 pages

PCD manual_FINAL (1) (1)

The document outlines the course details for the Computer Programming Lab at ACU-BGS Institute of Technology, including objectives, outcomes, and practical sessions. Students will learn to write flowcharts, algorithms, and C programs, covering topics such as control structures, arrays, functions, and debugging. The lab includes various programming exercises and theoretical concepts designed to bridge the gap between theory and practice.

Uploaded by

Moulya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views50 pages

PCD manual_FINAL (1) (1)

The document outlines the course details for the Computer Programming Lab at ACU-BGS Institute of Technology, including objectives, outcomes, and practical sessions. Students will learn to write flowcharts, algorithms, and C programs, covering topics such as control structures, arrays, functions, and debugging. The lab includes various programming exercises and theoretical concepts designed to bridge the gap between theory and practice.

Uploaded by

Moulya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

ACU-BGS Institute of Technology

Course Details
Course Name: Computer Programming Lab.
Course Code: 18CPL16/26.
Course prerequisite: Basic Mathematics knowledge and Logical thinking.

Course Objectives:
Upon completion of this course, students are expected to:

1. To practice writing flowcharts, algorithms and programs.


2. To implement basics of C programming language.
3. To provide solutions to the laboratory programs.
4. To familiarize the processes of debugging and execution.

Course outcomes
Upon successful completion of this course, students should be able to:

Subject code: 18CPL16/26 Subject Name: Computer Programming Lab


Cos COURSE OUTCOME
CO1 Execute C Program on control structures.
CO2 Execute C programs using arrays and functions.
CO3 Execute C programs on string handling functions.
CO4
Execute C programs on pointers, structures and files.

COMPUTER PROGRAMMING LAB


ACU-BGS Institute of Technology

Subject Code: 18CPL17/27 IA Marks: 40


No. of Practical Hrs. / Week: 03 Exam Hours: 03
Total No. of Practical Hrs: 42 Exam Marks: 60

PART A
Demonstration of Personal Computer and its Accessories:

Demonstration and Explanation on Disassembly and Assembly of a Personal Computer by the


faculty-in-charge. Students have to prepare a write-up on the same and include it in the Lab
record and evaluated.

Laboratory Session-1: Write-up on Functional block diagram of Computer, CPU, Buses,


Mother Board, Chip sets, Operating System & types of OS, Basics of Networking &
Topology and NIC.

Laboratory Session-2: Write-up on RAM, SDRAM, FLASH memory, Hard disks, Optical
media, CDROM/R/RW, DVDs, Flash drives, Keyboard, Mouse, Printers and Plotters.
Introduction to flowchart, algorithm and pseudo code.

Note: These TWO Laboratory sessions are used to fill the gap between theory classes and
practical sessions. Both sessions are to be evaluated as lab experiments.

PART B
Laboratory Experiments:
Implement the programs with WINDOWS / LINUX platform using appropriate
C compiler.

1. Develop an algorithm and implement simple C program to solve simple computational


problems using arithmetic expressions, demonstrate familiarization with programming
environment, concept of naming the program files, storing, compilation and debugging,
using different data types.
2. Draw the flowchart and implement C program to solve problems involving if-then-else
structures to find the largest of given three positive integers.
3. 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(Using switch
statements).

4. 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
ACU-BGS Institute of Technology

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

5. Draw the flowchart and Write a C Program to compute Sin(x) using Taylor series
approximation given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/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. Program must
check the compatibility of orders of the matrices for multiplication. Report appropriate
message in case of incompatibility.

8. Design and algorithm and write a C program to do Newton-Raphson method to find the
square root of a given positive integer. Also cross-check with implementation of long-
division method.

9. 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
n
program to compute the binomial coefficient Cr. Tabulate the results for different values
of n and r with suitable messages.
10. Implement structures to read, write, and compute average-marks and the students scoring
above and below the average marks for a class of 60 students.

11. 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.
12. Develop, implement and execute a C program to search a Name in a list of names using
Binary searching Technique.
13. Implement a C program to find the sum and average of N floating point numbers using
pointers.
 Fedora Commands:
 Esc+shift+:: To save the program
ACU-BGS Institute of Technology

 vi filename.c: To open a source file (.c file) using dialogue box.


 cc filename.c: To compile the program.
 ./a.out: To run the program.

TABLE OF CONTENTS
Program No. Program Title Page No.
1 To compute simple arithmetic programs. 1
2 To find Largest of given three positive integers using if-then-else. 4
3 To find roots of a Quadratic Equation using switch statement. 7
4 To check whether a number is Palindrome or Not. 11
5 To compute Sin(x) using Taylor Series. 13
6 To sort numbers using Bubble Sort. 17
7 To multiply two matrices. 21
8 To find Square root of a number using Newton-Raphson method. 25
9 To find Factorial of a given number. 27
10 To find the students average marks. 29
11 i) To implement String Copy 34
ii) Read a sentence and count number of vowels and Consonants. 38
12 To search for a name using Binary Search. 40
13 To compute sum and average of N floating point numbers using 43
pointers.
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

1. Develop an algorithm and implement C program to solve simple computational


problems using arithmetic expressions, demonstrate familiarization with programming
environment, concept of naming the program files, storing, compilation and debugging,
using different data types.

Program 1:

Addition of two numbers using integer data type:

#include<stdio.h>
int main()
{
int a, b, sum;
printf(“Enter the value of a and b:\n”);
scanf(“%d %d”, &a, &b);
sum=a + b;
printf(“sum of two numbers =%d”, sum);
return 0;
}

Addition of two numbers using float data type:

#include<stdio.h>
int main()
{
float a, b, sum;
printf(“Enter the value of a and b:\n”);
scanf(“%f %f”, &a, &b);
sum=a + b;
printf(“sum of two numbers =%f”, sum);
return 0;
}
Subtraction of two numbers using integer data type:

#include<stdio.h>
int main()
{
int a, b, sub;
printf(“Enter the value of a and b:\n”);
scanf(“%d%d”, &a, &b);
sub=a - b;
printf(“subtraction of two numbers =%d”, sub);
return 0;
}
1
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Subtraction of two numbers using float data type:

#include<stdio.h>
int main()
{
float a, b, sub;
printf(“Enter the value of a and b:\n”);
scanf(“%f %f”, &a, &b);
sub=a - b;
printf(“subtraction of two numbers =%f”, sub);
return 0;
}

Multiplication of two numbers using integer data type:

#include<stdio.h>
int main()
{
int a, b, product;
printf(“Enter the value of a and b:\n”);
scanf(“%d %d”, &a, &b);
product=a * b;
printf(“product of two numbers =%d”, product);
return 0;
}

Multiplication of two numbers using float data type:

#include<stdio.h>
int main()
{
float a, b, product;
printf(“Enter the value of a and b:\n”);
scanf(“%f %f”, &a, &b);
product=a * b;
printf(“product of two numbers =%f”, product);
return 0;
}

2
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Division of two numbers using integer data type:

#include<stdio.h>
int main()
{
int a, b, div;
printf(“Enter the value of a and b:\n”);
scanf(“%d%d”, &a, &b);
div=a / b;
printf(“division of two numbers =%d”, div);
return 0;
}

Division of two numbers using float data type:

#include<stdio.h>
int main()
{
float a, b, div;
printf(“Enter the value of a and b:\n”);
scanf(“%f %f”, &a, &b);
div=a / b;
printf(“division of two numbers =%f”, div);
return 0;
}

3
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

2. Draw the flowchart and implement a simple C program to solve problems involving if-
then-else structures to find the largest of given three positive integers.

Program 2:

#include<stdio.h>
int main()
{
int n1,n2,n3;
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&n1,&n2,&n3);
if(n1>=n2)
{
if(n1>=n3)
printf(“%d is the largest number”,n1);
else
printf(“%d is the largest number”,n3);

}
else
{
if(n2>=n3)
printf(“%d is the largest number”,n2);
else
printf(“%d is the largest number”,n3);
}
return 0;
}

4
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Flowchart:

Start

Read n1,n2,n3

True False
If
n1>n2

True If False True If False


n1>n3 n2>n3

Print n1 Print n3 Print n2 Print n3

Stop

5
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

OUTPUT:

SAMPLE INPUT / OUTPUT:

Run 1:
Enter the three numbers
20
10
5

20 is the largest number

Run 2:
Enter the three numbers
90
101
53

101 is the largest number

6
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

3. 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.

Theory:
A quadratic equation is a polynomial equation of the second degree. The general form is ax2 +
bx + c=0, where a, b, and c are constants, with a ≠ 0. (If a = 0, the equation becomes a Linear
Equation.)
Solving for ‘x’ then gives

Algorithm :
step 1: read a, b, c

step 2: if (a == 0&&b==0&&c==0)
write “invalid data”
exit
end if
disc = b*b-4*a*c
deno = 2*a

step 3: if (disc>0)
write “roots are real and distinct”
root1=(-b+√disc)/deno
root2=(-b-√disc)/deno
write “root1,root2”

else if (disc = = 0)
write “roots are real and equal”
root1=root2=-b/deno
write “root1,root2”

else
write “roots are complex”
root1 = -b/deno
root2 = √|disc|/deno
write “root1+iroot2”
write “root1-iroot2”

step 4: stop.
7
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Flowchart :

Start

Read a,b,c

F T
Is a == 0?
disc ← (b * b) – 4 * a * c
deno ← 2 * a

F
Is disc>0?
Print Invalid
data
T

Print “roots are real Stop


and distinct” A

root1← ( - b + √disc)/ deno


root2← (- b - √disc)/ deno F
Is
disc==0?

T
Print “Roots are complex”
Print “roots are real
& equal”

ip ←√fabs (disc) / deno


root1←root2← - b/deno rp ← - b/ deno

root1← rp + i ip
root2← rp – i ip

Print root1, root2

A
8
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 3:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()

{
float a,b,c,d,real,img,root1,root2;
int flag;
clrscr();
printf (" Enter three values of co-efficients \n" );
scanf ("%f%f%f", &a,&b,&c);
if (a == 0&&b!=0&&c!=0)
{
printf("\n invalid co-efficients");
}
else
{
d =(b*b) - (4*a*c);
printf(“d=%f\n”,d);
if (d>0) // real distinct or unequal roots
flag=1;
else if(d== 0) // real equal roots
flag=2;
else
flag=3;

switch(flag)
{
case 1: printf ( "\n Roots are real and distinct: " ) ;
rootl = (-b + sqrt(d)) /(2*a);
root2 = (-b - sqrt(d)) /(2*a);
printf("\n\t Rootl = %f and Root2 = %f", root1, root2);
break;
case 2: printf ( " \nRoots are real and equal: " ) ;
root1=root2=-b/(2*a);
printf("\n\t Rootl = Root2 = %f",root1,root2);
break;
case 3: printf("\nRoots are complex:\n");
img=sqrt(fabs(d))/(2*a);
real=-b/(2*a);
printf("\n\t Rootl = %f + i %f \n\n\t”, real, img);
printf(“\n\t Root2= %f – i %f \n\n\t”, real, img);
9
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

}
}
getch();

}
SAMPLE INPUT / OUTPUT:

Run 1:
Enter values of co-efficient
2
4
2
Roots are real and equal:
Rootl=Root2 =-1.000000

Run 2:
Enter values of co-efficient
2
8
1
Roots are real and distinct:
Rootl = -0.129171 and Root2 = -3.870829

Run 3:
Enter values of co-efficient
4
5
6
Roots are complex:
Rootl = -0.625000 + i1.053269
Root2 = -0.625000 - i1.053269

Run 4:
Enter values of co-efficient
0
5
6
Invalid co-efficients

10
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

4. 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.

Theory:
A palindrome number or numeral palindrome is a 'symmetrical' number like 16461, which
remains the same when its digits are reversed. The term palindromic is derived from palindrome,
which refers to a word like rotor that remains unchanged under reversal of its letters. The
palindromic numbers (in decimal) are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161,
171, 181, 191, …

Algorithm:

Step 1: read num

Step 2: temp=num

Step 3: while (temp!=0)


digit=temp%10
rev=rev*10+digit
temp= temp/10
end while

Step 4: write “reverse number.”

Step 5: if(num==rev)
write “ num is a Palindrome”
else
write “ num is not a Palindrome”
end if

Step 6 : stop.

11
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 4:

#include<stdio.h>
#include<conio.h>
void main ( )
{
int num , temp , digit , rev=0;
clrscr();
printf ("\n Enter any number \n");
scanf ("%d", &num);

temp=num; //copy of num to a temp variable

while (temp!=0) /* to determine reverse of a number*/


{
digit=temp%10;
temp=temp/10;
rev=rev*10+digit;
}

printf("\n Reversed number is = %d\n",rev);

if (num= = rev)
printf ("\n %d is a palindrome",num);
else
printf ("\n %d is not a palindrome",num);
getch();

SAMPLE INPUT / OUTPUT:

Run 1:
Enter any number 2332
Reversed number is = 2332
2332 is a palindrome

Run 2:
Enter any number 4324
Reversed number is=4234
4324 is not a palindrome

12
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

5. Draw the flowchart and Write a C Program to compute Sin(x) using Taylor series
approximation given by
Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + …….Compare the results with built-in Library
function and print both the results with appropriate messages.

Algorithm:

Step 1: Read x in degrees


Step 2: Convert x to radians
x=x*3.14/180
Step 3: for ( i = 1 to 100)
term = (-term*x*x)/((2*i)*(2*i+1))
sum = sum + term
Step 4: Print sum as sin(x) using Taylor Series
Step 5: stop

13
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Flowchart :

Start

Read degree

PI 3.142

X degree*(PI/180)

numr X
denr 1
sum 0
i 2

term numr/denr
numr -numr*X*X
denr denr*(i*(i+1))
sum sum+term
i i+2

True is(fabs(term)>=0.0001) False

print sum
print sin(x)

stop

14
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 5:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i, degree;
float x, sum=0,term,numr,denr;
clrscr();
printf(“Enter the value of degree:”);
scanf(“%d”, &degree);
x= degree * (PI/180);
numr=x;
denr=1;
i=2;
do
{
term=numr / denr;
numr=-numr *x *x;
denr= denr *(i *(i+1));
sum=sum +term;
i=i+2;
}
while(fabs(term)>=0.00001);
printf(“the sine of %d is %.3f\n”, degree, sum);
printf(“the sine of %d using the built in function sin() is %.3f\n”,degree,sin(x));
getch();
}

SAMPLE INPUT / OUTPUT:

Run 1:
Enter the value of degree
0
the sine of 0 is 0.00
the sine of 0 using the built-in function sin( ) is 0.00

Run 2:
Enter the value of degree
60
the sine of 60 is 0.866
15
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

the sine of 60 using the built-in function sin( ) is 0.866

Run 3:
Enter the value of degree
-10
the sine of -10 is -0.174
the sine of -10 using the built-in function sin( ) is -0.174.

16
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

6. Develop an algorithm, implement and execute a C program that reads N integer numbers
and arrange them in ascending order using Bubble Sort.
Theory:
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be
sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates that the list is
sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
Because it only uses comparisons to operate on elements, it is a comparison sort.
Algorithm :
Steps :

1. [Initialization] start

2. [Input number of elements]


Read n

3. [Input unsorted array elements in array]


Read a[]

4. Print elements of array a[]


Print a[]

5. [Iterate array a[] in two loops. Outer loop gives number of passes and inner loop does swap
task . in each pass, compare each pair of adjacent items. I former element is greater than
later one, swap them]

for each value i in array a[i] to n do


for each value j in array a[j] to n-1 do
[compare each pair of adjacent elements]
if(a[j]>a[j+1]) then
[swap these elements using temp variable]
temp a[j]
a[j] a[j+1]
a[j+1] temp
end if
end for
end for

6. [print array with sorted elements]


Print a[]

7. [Finished] End

Flowchart:
17
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Start

Read n

Read n elements

F
i←0
i←i + 1 i<n
T

Read a[i]

Print n Print sorted


elements array

i←0
F i←i + 1 i<n i ←0
i<n i
T ←i+1
Print a[i] i ←i+1
Print a[i]

F j←1
j←j+ 1 j<n

T
F Stop
i←0
i←i+ 1 i < n-j

T
F
is
a[j]>a[j+1] ?

T
temp ←a[j]
a[j] ←a[j+1]
A a[j+1] ←temp

18
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 6:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp,a[10];
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);

printf("Enter the elements to sort\n"); /* read the array to be sorted */


for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Entered elements are:\n"); /* printing the array accepted as input */
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
/* Function to sort the elements */
for(i=1;i<=n-1;i++) /* n-1 passes */
{
for(j=0;j<=n-i-1;j++) /* comparisons */
{
if(a[j]>a[j+1])
{
temp=a[j]; /* exchange of elements if a[i] is greater than a[i+1]*/
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("Sorted array elements are:\n");


for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

19
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

SAMPLE INPUT / OUTPUT:

Run 1:
Enter the number of elements
3
Enter the elements to sort
26
51
8

Entered array elements are:


26
51
8

Sorted array elements are:


8
26
51

Run 2:
Enter the number of elements
5
Enter the elements to sort
88
36
5
99
2

Entered array elements are:


88
36
5
99
2
Sorted array elements are:
2
5
36
88
99

20
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

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. Program must
check the compatibility of orders of the matrices for multiplication. Report appropriate
message in case of incompatibility.

Algorithm:
Step 1: Read m,n
Read p,q
Step 2: if (n!=p)
print “Aborting!!!!!! Multiplication Of The Above Matrices Not Possible.”
goto step 7
else
print “multiplication possible”

Step 3: for(i=0 to m)
for(j=0 to n)
Read a[i][j]
end for
end for
for (j=0 to q)
for (i=0 to p)
Read b[j][i]
end for
end for

Step 4: for (i=0 to m)


for(j=0 to q)
c[i][j]=0
end for
end for

Step 5: for(k=0 to n)
c[i][j] =c[i][j]+a[i][k]*b[k][j]
end for

Step 6: print ”multiplication of a[i][j] & b[i][j] is”


for (i=0 to m)
for (j=0 to q)
print (c[i][j])
end for
end for
Step 7: Stop
21
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 7:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
clrscr();
printf("Enter the value of m and n:");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix a:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“ the matrix a is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“Enter the value of p and q:\n”);
scanf(“%d%d”,&p,&q);
printf(Enter the elements of matrix b:\n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,&b[i][j]);
}
}
printf(“the matrix b is\n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,b[i][j]);
22
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

}
printf(“\n”);
}
if(n!=p)
{
printf("Aborting!!!!!!\nMultiplication of the Above Matrices Not Possible.");

} else{
printf("\n Matrix Multiplication Is Possible\n");
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]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\n The resultant matrix c is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}}
getch();
}

SAMPLE INPUT / OUTPUT:

Run 1:
Enter the value of m and n
2 2
Enter the elements for matrix a
5 2
1 2
The matrix a is
5 2
1 2
Enter the value of p and q
23
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

2 2
Enter the elements for matrix b
3 1
2 4

The matrix b is
3 1
2 4
Matrix multiplication is possible
Resultant matrix c is
19 13
7 9

Run 2:
Enter the value of m and n
2 3
Enter the elements for matrix a
1 2 3
4 5 6
The matrix a is
1 2 3
4 5 6
Enter the value of p and q
2 3
Enter the elements for matrix b
1 2 3
4 5 6
The matrix b is
1 2 3
4 5 6
Matrix multiplication is not possible

24
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

8. Design an algorithm and write a C program to do Newton-Raphson method to find the


square root of a given positive integer. Also cross-check with implementation of long-division
method.

Program 8:

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, int n)
{
return(x*x-n);
}
float df(float x)
{
return 2*x;
}
void main()
{
float b=1,m,e=0.0001,num;
int i;
clrscr();
printf(“Newton Raphson Method to find Square root:\n”);
printf(“Enter number whose square root to be found:\n”);
scanf(“%f”, &num);
m=(b)-((f(b, num))/df(b));
i=1;
while(fabs(f(m, num))>e)
{
b=m;
m=(b)-((f(b, num))/df(b));
i=i+1;
printf(“Square root of %.2f is %.2f\n”, num, m);
}
getch();
}

25
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

SAMPLE INPUT / OUTPUT:

Run 1:
Newton Raphson Method to find Square root:
Enter number whose square root to be found:
4
Square root of 4.00 is 2.139655
Square root of 4.00 is 2.004558
Square root of 4.00 is 2.000005

Run 2:
Newton Raphson Method to find Square root:
Enter number whose square root to be found:
16
Square root of 16.00 is 4.001219
Square root of 16.00 is 4.000000

Run 3:
Newton Raphson Method to find Square root:
Enter number whose square root to be found:
9
Square root of 9.00 is 3.02530
Square root of 9.00 is 3.00000

26
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

9. 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.
Algorithm:
Step 1: Read n and r
Step 2: ncr =fact(n)/(fact(n-r)*fact(r))
Step 3: fact(n) : if (n==0 or n==1) return 1 else return(n*fact(n-1));
Step 4: Print Binomial Coefficient nCr
Step 5: stop
Flowchart:
Start

Read n,r

Res=fact(n)/(fact(n-r)*fact(r))

print nCr=res

Stop

fact(n)

Is n=0 Return 1

Return (n*fact(n-1))

27
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 9:

#include<stdio.h>
#include<conio.h>

int fact( int n) //function definition, recursive nature


{
if(n==0)
{
return 1;
}
return(n*fact(n-1));
} // end of function factorial
void main()
{
int n,r,res;
clrscr();
printf("\n Enter n and r values:");
scanf("%d%d",&n,&r);
if(n>=r)
{
res=fact(n)/(fact(n-r)*fact(r)); //function call
printf("%dC%d(nCr)=%d\n”,n,r,res);

}
else
{
Printf(“n should be >=r\n”);
}
getch();
}

SAMPLE INPUT / OUTPUT:


Run 1:
Enter n and r values: 5 2
5C2(nCr) :10

Run 2:
Enter n and r values: 12 4
12C4(nCr) : 495

28
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

10. Implement structures to read, write, and compute average- marks and the students
scoring above and below the average marks for a class of 60 students.

Program 10:

#include <stdio.h>
#include<conio.h>

void main()
{
struct student{
char name[20];
char usn[20];
int sub1;
int sub2;
int sub3;
int sub4;
int avg;
};

struct student students[60];

int n,i;
clrscr();
printf("enter number of students:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nenter the details of student %d",i+1);
printf("\nenter the name of student:");
scanf("%s",&students[i].name);
printf("enter the USN of student:");
scanf("%s",&students[i].usn);
printf("marks of sub 1:");
scanf("%d",&students[i].sub1);
printf("marks of sub 2:");
scanf("%d",&students[i].sub2);
printf("marks of sub 3:");
scanf("%d",&students[i].sub3);
printf("marks of sub 4:");
scanf("%d",&students[i].sub4);
}
printf("\nname\t\tusn\t\tsub1\tsub2\tsub3\tsub4\ttotal\tavg\tresult(above/below avg)");

29
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

for(i=0;i<n;i++)
{
int avg = 0,total=0;
total = ( students[i].sub1 + students[i].sub2 + students[i].sub3 + students[i].sub4 );
avg = total / 4;
if(avg < 50)
printf("\n%s\t\t%s\t\t%d\t%d\t%d\t%d\t%d\t%d\tbelow",students[i].name,
students[i].usn, students[i].sub1, students[i].sub2, students[i].sub3, students[i].sub4, total, avg);
else
printf("\n%s\t\t%s\t\t%d\t%d\t%d\t%d\t%d\t%d\tabove",students[i].name,
students[i].usn, students[i].sub1, students[i].sub2, students[i].sub3, students[i].sub4, total, avg);
}

getch();
}

SAMPLE INPUT / OUTPUT:


Run 1:
enter number of students:3

enter the details of student 1

enter the name of student:asha

enter the USN of student:4bw15cs005

marks of sub 1:67

marks of sub 2:78

marks of sub 3:98

marks of sub 4:65

enter the details of student 2

30
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

enter the name of student:preethi

enter the USN of student:4bw15cs006

marks of sub 1:76

marks of sub 2:98

marks of sub 3:78

marks of sub 4:90

enter the details of student 3

enter the name of student:ramya

enter the USN of student:4bw15cs008

marks of sub 1:68

marks of sub 2:87

marks of sub 3:94

marks of sub 4:69

name usn sub1 sub2 sub3 sub4 total avg result(above/belowavg)

asha 4bw15cs005 67 78 98 65 308 77 above

preethi 4bw15cs006 76 98 78 90 342 85 above

ramya 4bw15cs008 68 87 94 69 318 79 above

Run 2:

enter number of students:5

enter the details of student 1

enter the name of student:shruthi

enter the USN of student:4bw18cs004

31
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

marks of sub 1:75

marks of sub 2:74

marks of sub 3:65

marks of sub 4:56

enter the details of student 2

enter the name of student:spoorthi

enter the USN of student:4bw18csoo8

marks of sub 1:65

marks of sub 2:89

marks of sub 3:70

marks of sub 4:69

enter the details of student 3

enter the name of student:sumathi

enter the USN of student:4bw18is006

marks of sub 1:78

marks of sub 2:89

marks of sub 3:96

marks of sub 4:80

enter the details of student 4

enter the name of student:swathi

enter the USN of student:4bw18cs005

marks of sub 1:76

32
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

marks of sub 2:78

marks of sub 3:90

marks of sub 4:65

enter the details of student 5

enter the name of student:kavitha

enter the USN of student:4bw18cs003

marks of sub 1:56

marks of sub 2:67

marks of sub 3:74

marks of sub 4:46

name usn sub1 sub2 sub3 sub4 total avg result(above/below avg)

shruthi 4bw18cs004 75 74 65 56 270 67 above

spoorthi 4bw18csoo8 65 89 70 69 293 73 above

sumathi 4bw18is006 78 89 96 80 343 85 above

swathi 4bw18cs005 76 78 90 65 309 77 above

kavitha 4bw18cs003 56 67 74 46 243 60 above

33
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

11. 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.

11 i:

Algorithm:

Step 1: Read string str1


Step 2: Print string str1
Step 3:strcopy(str1,str2)
for i=0 to str1[i]!=’\0’
str2[i]=str1[i]
i++
str2[i]=’\0’
Step 4: print str2
Step 5: Stop

Flowchart:
strcopy(str1,str2)
Start

read str1 F i← 0 str1[i]


!= '\0'

i ← i+1
strcopy(str1,str2)
T

str2[i] ←str1[i]
i←i+1
Print str2

str2[i] ←’\0’
Stop

return

34
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 11 i:

#include<stdio.h>
#include<conio.h>

void strcopy(char[],char[]);

void main()
{
char str1[100],str2[100];
clrscr();
printf("Enter any string: ");
gets(str1);
strcopy(str1,str2);
printf("After copying: %s",str2);
getch();
}

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


{
int i;
for(i = 0; str1[i] != '\0'; ++i)
str2[i] = str1[i];
str2[i] = '\0';
}

SAMPLE INPUT / OUTPUT:

Enter any string: global


After copying: global

35
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

11 ii:

Algorithm:

Step1: Read a line of string


Step2: for i=0 to end of line
Step3: check isalpha buit-in function and convert to lowercase using tolower() function.
Step4: check the condition for all the vowels(a,e,i,o,u) using if statement
Step5: Increment the count of vowels & consonants.
Step 3: print count of vowels.
Step 4: print count of consonants
Step 5: stop

36
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Flowchart:
Start

Read string

F i ←0 i<strlen(s)

i← i+1

B
T

is F
isalpha(s[i])?

T
ch=tolower(s[i])

is
ch == ‘a’ || ch == ‘e’ ||
ch ==’i’ ||
F ch ==’o’|| ch ==’u’?
T
T
Consonants++ vowels++
else

print count of
vowels and
consonants

Stop
37
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 11ii:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char s[100];
int ac=0,ec=0,ic=0,oc=0,uc=0,cc=0,i;
clrscr();

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


gets(s);

for(i=0;i < strlen(s);i++) /* to check the string with vowels and consonants */
{
if( isalpha(s[i]))
{
if(s[i]==’a’ || s[i]==’A’)
{
ac++;
}
else if(s[i]==’e’ || s[i]==’E’)
{
ec++;
}
else if(s[i]==’i’ || s[i]==’I’)
{
ic++;
}
else if(s[i]==’o’ || s[i]==’O’)
{
oc++;
}
else if(s[i]==’u’ || s[i]==’U’)
{
uc++;
}

else
cc++;
}
}
38
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

printf(" Number of vowel a is =%d\n”,ac);


printf(" Number of vowel e is =%d\n”,ec);
printf(" Number of vowel i is =%d\n”,ic);
printf(" Number of vowel o is =%d\n”,oc);
printf(" Number of vowel u is =%d\n”,uc);
printf(" Number of consonants is =%d\n”,cc);
getch();

SAMPLE INPUT / OUTPUT:

Run 1:
Enter a string:
Bgs institute of technology
Number of vowel a is=0
Number of vowel e is=2
Number of vowel i is=2
Number of vowel o is=3
Number of vowel u is=1
No of consonants is= 16

Run 2:
Enter a string:
C programming lab
Number of vowel a is=2
Number of vowel e is=0
Number of vowel i is=0
Number of vowel o is=1
Number of vowel u is=0
No of consonants in the given string is: 11

39
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

12.Develop, implement and execute a C program to search a Name in a list of names using
Binary searching Technique.

Theory:
In computer science, a binary search is an algorithm for locating the position of an item in a
sorted array. The idea is simple: compare the target to the middle item in the list. If the target
is the same as the middle item, you've found the target. If it's before the middle item, repeat
this procedure on the items before the middle. If it's after the middle item, repeat on the items
after the middle
Algorithm:
1. get the middle element;
2. if the middle element equals to the searched value, the algorithm stops;
3. otherwise, two cases are possible:
case1: searched value is less, than the middle element. In this case, go to the step 1 for the part
of the array, before middle element.
case 2: searched value is greater, than the middle element. In this case, go to the step 1 for the
part of the array, after middle element.

Algorithm:
step 1: read n

step 2: for (i=0 to n in steps of 1) do


read a[i]
end for

step 3: read key

step 4: low = 0,high = n-1


mid = (low+high)/2

step 5: while(key != a[mid] and low <= high)


if(key > a[mid])
low = mid + 1
if(key<a[mid])
high=mid-1;
mid=(low+high)/2;
end while

step 6: if (key == a[mid]


write “search is successful, key found”
else
print “search is unsuccessful, key not found”
end if else
step 7: stop
40
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Program 12:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,low,high,mid;
char a[20][20], key[20];
clrscr();
printf("Enter the number of names:\n");
scanf("%d",&n);

printf("Enter the names in lexicographic (alphabetical) order as in dictionary:\n");


for(i=0;i<n;i++)
scanf("%s",&a[i]);

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


scanf("%s",&key);

low=0; /* initial position of array */


high=n-1; /* end point or last position of array */
mid=(low+high)/2; /* finding the midpoint of the array */

while((strcmp(key,a[mid])!= 0)&&low<=high)
{
if(strcmp(key,a[mid]) > 0)
low=mid+1;
if(strcmp(key,a[mid]) < 0)
high=mid-1;
mid=(low+high)/2;
}

if(strcmp(key,a[mid]) == 0) /* comparing the string to check if they are same */


printf("Search is successful. Key name is found\n");
else
printf("Search is not successful. Key name is not found\n");
getch();

41
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

SAMPLE INPUT / OUTPUT:

Run 1:
Enter the number of names:
5

Enter the names in lexicographic (alphabetical) order as in dictionary:


arjun
bindu
chaitra
darshan
deepa

Enter the key name to be searched:


deepa

Search is successful. Key name is found.

Run 2:
Enter the number of names:
4

Enter the names in lexicographic (alphabetical) order as in dictionary:


dick
john
ruth
sam

Enter the key name to be searched:


rose

Search is not successful. Key name is not found

42
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

13. Write a C program to find sum and average of N floating point numbers using Pointers.

Program 13:

#include <stdio.h>
#include<conio.h>
void main()

{
float array[10],*ptr ,sum=0;
int n,i;
clrscr();
printf("enter the number:");
scanf("%d",&n);
printf("enter the data to find sum and avg:\n");
for(i=0;i<n;i++)
scanf("%f",&array[i]);
ptr = array;
for(i=0;i<n;i++)
{
sum = sum + *(ptr+i);
}
printf("sum = %f\n",sum);
printf("avrage= %f",(sum/n));
getch();
}

SAMPLE INPUT / OUTPUT:


Run 1:
Enter the number:3
Enter data to find sum and avg:
12
13
14
sum = 39.000000
average = 13.000000

Run 2:
Enter the number:5
Enter data to find sum and avg:
10
20
30
40
43
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

50
sum = 150.000000
average = 30.000000

44
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

Possible set of Viva Questions

1) What is a flowchart?
2) What is an Algorithm?
3) What is recursion?
4) What are advantages of flow charts?
5) What are different symbols in flowcharts?
6) What is a connector symbol?
7) Is the following assignment correct? A==10;
8) char a=”r”; Is the declaration correct?
9) What is the maximum length of variable?
10) Can a C keyword used as a variable name?
11) What does an expression contains?
12) How is an expression evaluated?
13) What does the modulus performs?
14) Name different logical operators.
15) What is syntax of conditional operator?
16) Is the scanf statement correct? scanf(“%s”,&a);
17) What is a variable?
18) How a variable is declared?
19) What is a constant?
20) List some valid logical expression.
21) What is the difference between post and pre increment/decrement?
a) when they are stand alone
b) when they are embed in an expression
22) What is a keyword?
23) What are Literals?
24) What are two categories of C constants?
25) What is the range of integer constant?
26) What is the output of printf("%d") ?
27) Is there a similarity between for and while?
28) Difference between if-else if ladder and switch
29) What is ternary operator? Write the syntax
30) Difference between while and do-while
31) In what scenario we have to apply for loop?
32) Does break exits from the two loops?
33) How does continue work?

45
Computer Programming Laboratory 2nd Semester

ACU-BGS Institute of Technology

34) What is string? Difference between string and char


35) Difference between array and linked-lists?
36) Difference between linked-lists and trees?
37) What do you mean by abstract data-type?
38) Name some dynamic memory allocation methods
39) What is the use of free()?
40) What is a pointer?
41) What is NULL pointer?
42) What is the use of return statement?
43) Explain the function signature.
44) How does a stack work?
45) Difference between stack and queue?
46) What is dangling pointer in c?
47) What will be output of following c program?
void main()
{
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
}
48) Write a c program without using any semicolon which output will : Hello word.
49) Swap two variables without using third variable.
50) Swap two variables using built in function.

46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy