PCD manual_FINAL (1) (1)
PCD manual_FINAL (1) (1)
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:
Course outcomes
Upon successful completion of this course, students should be able to:
PART A
Demonstration of Personal Computer and its Accessories:
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.
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.
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
Program 1:
#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;
}
#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
#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;
}
#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;
}
#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
#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;
}
#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
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
Flowchart:
Start
Read n1,n2,n3
True False
If
n1>n2
Stop
5
Computer Programming Laboratory 2nd Semester
OUTPUT:
Run 1:
Enter the three numbers
20
10
5
Run 2:
Enter the three numbers
90
101
53
6
Computer Programming Laboratory 2nd Semester
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
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
T
Print “Roots are complex”
Print “roots are real
& equal”
root1← rp + i ip
root2← rp – i ip
A
8
Computer Programming Laboratory 2nd Semester
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
}
}
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
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 2: temp=num
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
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);
if (num= = rev)
printf ("\n %d is a palindrome",num);
else
printf ("\n %d is not a palindrome",num);
getch();
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
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:
13
Computer Programming Laboratory 2nd Semester
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
print sum
print sin(x)
stop
14
Computer Programming Laboratory 2nd Semester
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”, °ree);
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();
}
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
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
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
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]
7. [Finished] End
Flowchart:
17
Computer Programming Laboratory 2nd Semester
Start
Read n
Read n elements
F
i←0
i←i + 1 i<n
T
Read a[i]
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
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);
19
Computer Programming Laboratory 2nd Semester
Run 1:
Enter the number of elements
3
Enter the elements to sort
26
51
8
Run 2:
Enter the number of elements
5
Enter the elements to sort
88
36
5
99
2
20
Computer Programming Laboratory 2nd Semester
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 5: for(k=0 to n)
c[i][j] =c[i][j]+a[i][k]*b[k][j]
end for
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
}
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();
}
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
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
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
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
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
Program 9:
#include<stdio.h>
#include<conio.h>
}
else
{
Printf(“n should be >=r\n”);
}
getch();
}
Run 2:
Enter n and r values: 12 4
12C4(nCr) : 495
28
Computer Programming Laboratory 2nd Semester
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;
};
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
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();
}
30
Computer Programming Laboratory 2nd Semester
Run 2:
31
Computer Programming Laboratory 2nd Semester
32
Computer Programming Laboratory 2nd Semester
name usn sub1 sub2 sub3 sub4 total avg result(above/below avg)
33
Computer Programming Laboratory 2nd Semester
11 i:
Algorithm:
Flowchart:
strcopy(str1,str2)
Start
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
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();
}
35
Computer Programming Laboratory 2nd Semester
11 ii:
Algorithm:
36
Computer Programming Laboratory 2nd Semester
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
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();
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
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
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
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);
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;
}
41
Computer Programming Laboratory 2nd Semester
Run 1:
Enter the number of names:
5
Run 2:
Enter the number of names:
4
42
Computer Programming Laboratory 2nd Semester
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();
}
Run 2:
Enter the number:5
Enter data to find sum and avg:
10
20
30
40
43
Computer Programming Laboratory 2nd Semester
50
sum = 150.000000
average = 30.000000
44
Computer Programming Laboratory 2nd Semester
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
46