Computer Programming Lab
Computer Programming Lab
LAB MANUAL
(CBCS 2022 SCHEME)
Represents
Looping
Hexagon structures
Subroutine
Process function
Advantages of Flowcharts:
A flowchart is a diagrammatic representation that illustrates the sequence of steps that must be
performed to solve a problem. They are usually drawn in the early stages of formulating computer
solutions to facilitate communication between programmers and business people.
Flowcharts help programmers to understand the logic of complicated and lengthy problems. They
help to analyze the problem in a more effective manner
Flowchart can be used to debug programs that have error(s).
E.g.: To compute the Area of Rectangle
Limitations of using Flowcharts:
Drawing flowcharts is a laborious and a time consuming activity. Flowchart of a complex program
becomes, complex and clumsy. At times, a little bit of alteration in the solution may require complete re-
drawing of the flowchart Essentials of what is done may get lost in the technical details of how it is done.
There are no well-defined standards that limits the details that must be incorporated in a flowchart
start
Print Area
Stop
Pseudo code: It is a form of structured English that describes algorithms. It facilitates the designers to
focus on the logic of the algorithm without getting bogged down by the details of language syntax.
Pseudocode is a compact and informal high-level description of an algorithm that uses the structural
conventions of a programming language. It is meant for human reading rather than machine reading, so it
omits the details that are not essential for humans. Such details include keywords, variable declarations,
system-specific code and subroutines. There are no standards defined for writing a pseudocode because it
is not an executable program. Flowcharts can be considered as a graphical alternative to pseudocode, but
are more spacious on paper.
Begin
Input length, breadth
Area=length*breadth
Print Area
End
Computer Programming Laboratory
Environment on Linux
Familiarization with computer hardware and programming environment, concept of naming
the program files, storing, compilation, execution and debugging. Taking any simple C- code.
Right click on the folder and click in properties and note down the path
On the command promt enter the following command which will take
you the folder created
cd /home/sgbit/foldername
Page 1
Computer Programming Laboratory
gcc hello.c
or
This command will invoke the GNU C compiler to compile the file hello.c
./a.out
Page 2
Computer Programming Laboratory
Environment on Windows
Familiarization with computer hardware and programming environment, concept of naming
the program files, storing, compilation, execution and debugging. Taking any simple C- code.
Step 1: Locate the TC.exe file and open it. You will find it at location C:\TC\BIN\.
Step 2: File > New (as shown in the below picture) and then write your C program
Page 2
Computer Programming Laboratory
Step 3: Save the program using F2 (OR file > Save), remember the extension should be “.c”. In the
below screenshot I have given the name as summ.c.
Page 3
Computer Programming Laboratory
Step 4: Compile the program using Alt + F9 OR Compile > Compile (as shown in the below screenshot).
Step 5: Press Ctrl + F9 to Run (or select Run > Run in menu bar ) the C program.
Page 4
Computer Programming Laboratory
Step 6: Alt+F5 to view the output of the program at the output screen.
PART-A
Laboratory Program 1
Develop a program to solve simple computational problems using arithmetic expressions and use of
each operator leading to simulation of a commercial calculator. (No built-in math function).
1.1 ALGORITHM
PURPOSE : To simulate a calculator using arithmetic expressions
INPUT: Enter num1 and num2
OUTPUT: Print the result of addition or subtraction or multiplication or division or modulus.
START
Step 1: [Enter first number]
read num1
Step 2: [Enter Second number]
read num2
Step 3:[Enter Choice]
read choice
Step 4:[To perform addition]
if choice is equal to plus
add num1 and num2
print result
Step 5: [To perform subtraction]
if choice is equal to minus
subtract num2 from num1
print result
Step 6: [To perform multiplication]
if choice is equal to multiplication
multiply num1 and num2
print result
Step 7: [To perform division]
if choice is equal to division
Page 6
Computer Programming Laboratory
1.2 FLOWCHART
START
Read choice
if choice=?
case '+'
result=num1+num2;
case '-'
result=num1-num2;
case '*'
result=num1*num2;
Result
STOP
Page 7
Computer Programming Laboratory
1.3 PROGRAM
#include <stdio.h>
void main()
{
int num1,num2;
float result;
char choice; //to store operator choice
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,choice,num2,result);
}
***************************************************************************
OUTPUT:
First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000
******************************************************************************
VIVA QUESTIONS:
Page 9
Computer Programming Laboratory
Laboratory Program 2
Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
2.1 ALGORITHM
Purpose : To find roots of a given quadratic equation.
Input: Coefficients of quadratic equation a, b, c
Output: Root1 and Root2
START
STEP 1: [Input the values of a, b, c]
read a, b, c
STEP 2: [Calculate the determinant]
determinant = b*b-4*a*c
STEP 3: [Check for validity]
If a is equal to 0 and b is equal to 0
print “Invalid Inputs”
STEP 4: [Check for different roots]
If a is equal to 0
print “Linear equation”
Root1=-c/b
Print “Root1”
STEP 5: [Check for real and equal roots]
If determinant is equal to 0
print “Roots are real and equal”
Root1= -b/(2*a)
Root2 = -b/(2*a)
Print “Root1 & Root2”
STEP 6: [Check for real and distinct roots]
If determinant is greater than 0
Then print “Roots are real and distinct”
Root1= (-b+ (sqrt (fabs (determinant))))/(2*a)
Root2= (-b-(sqrt (fabs (determinant))))/(2*a)
Page 10
Computer Programming Laboratory
STOP
2.2 FLOWCHART
Page 11
Computer Programming Laboratory
2.3 PROGRAM
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c;
float root1,root2;
float determinant,real,imaginary;
printf("INVALID INPUTS\n");
}
else if(a==0)
{
printf("LINEAR EQUATION\n");
root1=-c/b;
printf("ROOT=%f\n",root1);
}
else if(determinant == 0)
{
printf("ROOTS ARE REAL AND EQUAL\n");
root1=-b/(2*a);
root2=-b/(2*a);
printf("Root1=%f\n Root2=%f",root1,root2);
}
else if(determinant>0)
Page 12
Computer Programming Laboratory
{
printf("ROOTS ARE REAL AND DISTINCT\n");
root1=(-b+(sqrt(fabs(determinant))))/(2*a);
root2=(-b-(sqrt(fabs(determinant))))/(2*a);
printf("ROOT1=%f\n ROOT2=%f",root1,root2);
}
else
{
printf("ROOTS ARE IMAGINARY\n");
real=-b/(2*a);
imaginary=sqrt(fabs(determinant))/(2*a);
printf("ROOT1=%f+i%f\n",real,imaginary);
printf("ROOT2=%f-i%f\n",real,imaginary);
}
}
******************************************************************************
Output 1:
Enter the Co-efficient of Quadratic Equation
000
Invalid Coefficients
Output 2:
Enter the Co-efficient of Quadratic Equation
021
Root1=-0.5
Output 3:
Enter the Co-efficient of Quadratic Equation
121
Roots are Real and Equal
Root1=-1.0000
Root2=-1.0000
Page 13
Computer Programming Laboratory
Output 4:
Enter the Co-efficient of Quadratic Equation
189
Roots are Real and Distinct
Root1=-1.354
Root2=-6.646
Output 5:
Enter the Co-efficient of Quadratic Equation
123
ROOTS ARE IMAGINARY
ROOT1=-1.000+i1.414
ROOT2=-1.000-i1.414
******************************************************************************
Viva Questions:
1) What is quadratic Equation?
2) What is math.h?
3) What are decision making capabilities of C language?
4) Write the syntax of “ if ”statement?
5) Difference between if and switch statements?
6) Write an unconditional control statement in C.
7) Write a flowchart for „ if‟ conditional construct?
Page 14
Computer Programming Laboratory
Practice Program
Develop a program to find the reverse of a positive integer and check for palindrome or not. Display
appropriate messages.
0.1 ALGORITHM
Purpose : To check the given integer is palindrome or not
Input: Num
Output : Reverse and palindrome or not
START
STEP 1: [Input a number]
read num
STEP 2: [To check the given number is 4 digits or not]
If the number less than 999 or greater than 9999
print “it is not a four digit number”
Goto step 5
STEP 3: [Calculate the reverse of given number]
Temp=num
Loop while temp not equal to 0
Remainder=temp%10
Temp=temp/10
Reverse=reverse*10+remainder
print “reverse number”
STEP 4: [Check number is a palindrome or not]
If Num is equal to reverse
print “Num is palindrome “
Else
print “Num is not palindrome”
STEP 5: [Finished]
STOP
Page 15
Computer Programming Laboratory
0.2 FLOWCHART
Page 16
Computer Programming Laboratory
0.3 PROGRAM
#include<stdio.h>
void main()
{
int NUM,reverse=0,temp,remainder;
temp=NUM;
while (temp!=0)
{
remainder=temp%10;
temp=temp/10;
reverse=reverse*10+remainder;
}
printf("Reverse of %d is %d \n",NUM,reverse);
if(NUM==reverse)
Page 17
Computer Programming Laboratory
Output 1:
Enter a number to check if it is a palindrome or not
123
Enter Four digit number
Output 2:
Enter a number to check if it is a palindrome or not
1234
1234 is not a palindrome number
Output 3:
Enter a number to check if it is a palindrome or not
1221
1221 is a palindrome number
******************************************************************************
Viva Questions:
1. What are Looping control statements?
2. Explain while loop.
3. What is the difference between while and for loops?
4. What are the Entry controlled and Exit controlled loops in C?
5. Write the flowchart for „while‟ loop.
Page 18
Computer Programming Laboratory
Laboratory Program 3
An electricity board charges the following rates for the use of electricity: for the first 200 units 80
paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are
charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400, then an
additional surcharge of 15% of total amount is charged. Write a program to read the name of the
user, number of units consumed and print out the charges.
3.1 ALGORITHM
PURPOSE: Read The Name Of The User, Number Of Units Consumed And Print
INPUT: name [10], unit
OUTPUT: Print the charges for total number of units consumed
START
STEP 1: [Input a name and units]
read name and unit
STEP 2: [Initialization]
Metercharge=100
STEP3: [To check electricity unit is less than or equal to 200 and calculate
metercharge] If unit less than or equal to 200
metercharge= metercharge+(unit*.80)
STEP 4: [Else check unit is greater than 200 and greater than 300 and calculate
metercharge] If unit greater than 200 and unit greater than or equal to 300
metercharge= metercharge+(200*0.80)+(unit-200)*0.90)
STEP 5: [Else check unit is greater than 300 and calculate metercharge]
If unit is greater than 300
metercharge= metercharge+(200*0.80)+(100*0.90)+(unit-300)*1)
STEP 6: [To check and calculate if meter charge is greater than 400 ]
If metercharge greater than or equal to 400
metercharge=metercharge+(metercharge*0.15);
STEP 7: [Finished]
STOP
Page 19
Computer Programming Laboratory
5.3 PROGRAM
#include <stdio.h>
void main()
{
char name[10];
float unit, metercharge=100;
printf("Enter your name and unit Consumed:");
scanf("%s%f",name,&unit);
if(unit<=200)
Page 20
Computer Programming Laboratory
metercharge= metercharge+(unit*.80);
else if(unit > 200 && unit <= 300)
metercharge= metercharge+(200*0.80)+(unit-200)*0.90;
else if(unit>300)
metercharge= metercharge+(200*0.80)+(100*0.90)+(unit-300)*1;
if(metercharge>=400)
metercharge=metercharge+(metercharge*0.15);
printf("Name: %s\n Number of unit consumed: %f \n MeterCharge: %f",name,unit,metercharge);
}
*****************************************
Output 1:
Enter your name and unit Consumed:Suresh 200
Name: Suresh
Number of unit consumed: 200
MeterCharge : 260.000
Output 2:
Enter your name and unit Consumed:Ramesh 400
Name: Ramesh
Number of unit consumed: 400
MeterCharge : 724.5000
******************************************************************************
Viva Questions:
Page 21
Computer Programming Laboratory
Lab Program 5
5.1 ALGORITHM
Read n
For i=0 to n
Read a[i]
Read key
STEP 4: [Initiliazation]
low = 0;
high = n-1;
STEP 5: [ Check whearther low is less than of equal to high and calculate mid)
while low <=high
mid= (low+ high)/2;
Page 22
Computer Programming Laboratory
found=1
high = mid-1;
low = mid+1;
If not found
STEP 12:[Finished]
STOP
Page 23
Computer Programming Laboratory
5.2 FLOWCHART
Page 24
Computer Programming Laboratory
5.3 PROGRAM
#include <stdio.h>
void main()
{
int a[50], key, i, n , low, high,mid, found =0;
printf("\n Enter the number of elements in the array: ");
scanf ("%d", &n);
printf (" \n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the key to be searched: \n" );
scanf ("%d", &key);
low = 0;
high = n-1;
while (low <= high)
{
mid= (low+ high)/2;
if (a[mid] == key)
{
printf("\n %d is present at the position = %d", key, mid+1);
found=1;
break;
}
if (a[mid] > key)
high = mid-1;
else
low = mid+1;
}
if(!found)
printf(“key not found”);
Page 25
Computer Programming Laboratory
}
***************************************************************
Output 1:
Enter the number of elements in the array: 5
Enter the elements of the array:
10 20 30 40 50
Enter the key to be searched: 30
30 is present at the position = 3
Output 2:
Enter the number of elements in the array: 5
Enter the elements of the array:
10 20 30 40 50
Enter the key to be searched: 60
key not found
******************************************************************************
Viva Questions:
What is searching?
What are the types of searching?
Explain with an example for binary search.
Explain with an example for linear search.
Which is better searching technique?
Page 26
Computer Programming Laboratory
Practice Program 3
Implement using functions to check whether the given number is prime and display appropriate
messages. (No built-in math function)
0.1 ALGORITHM
Input:A number
START
read n
for i=2 to m/2
if m% i is equal to 0
return 0
for i=2 to m/2
if m% i is equal to 1
return 1
Page 27
Computer Programming Laboratory
0.2 FLOWCHART
0.3 PROGRAM
#include<stdio.h>
int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i == 0)
return 0;
}
return 1;
}
Page 28
Computer Programming Laboratory
void main()
{
int isprime(int);
int n;
printf("Enter a +ve integer greater than 1:");
scanf("%d",&n);
if (isprime(n))
OUTPUT1:
5 is prime number
OUTPUT 2:
******************************************************************************
VIVA QUESTION:
Page 29
Computer Programming Laboratory
Laboratory Program 6
Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and
ensure the rules of multiplication are checked.
6.1 ALGORITHM
Page 30
Computer Programming Laboratory
For i 0 to m-1 do
For j 0 to q-1 do
C[i][j] 0
For k 0 to n-1 do
C[i][j] c[i][j]+a[i][k]*b[k][j]
End kth for loop
End jth for loop
End ith for loop
STEP 7: [Display matrix A]
For i 0 to m-1 do
For j 0 to n-1 do
Print a[i][j]
End jth for loop
End ith for loop
STEP 8: [Display matrix B]
For i 0 to p-1 do
For j 0 to q-1 do
Print b[i][j]
End jth for loop
End ith for loop
STEP 9: [Display matrix C]
For i 0 to m-1 do
For j 0 to n-1 do
Print c[i][j]
End jth for loop
End ith for loop
STEP 10: [Finished]
STOP
6.2 FLOWCHART
Page 31
Computer Programming Laboratory
Page 32
Computer Programming Laboratory
6.3 PROGRAM
#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("Enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix \n");
scanf("%d %d",&p,&q);
if(n == p)
{
}
printf("Enter the elements of the second matrix \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
Page 33
Computer Programming Laboratory
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
} // end of if
printf("The product of two matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf(“Multiplication is not possible”);
}
************************************************************************
Output1:
Enter the size of first matrix:2 2
Enter the size of second matrix:2 2
Enter the elements of first matrix :1 2 3 4
Enter the elements of second matrix :5 6 7 8
The product of two matrix
19 22
43 50
************************************************************************
VIVA QUESTIONS:
1.What is a multi-dimensional array?
Page 34
Computer Programming Laboratory
Page 35
Computer Programming Laboratory
Laboratory Program 7
Develop a Program to compute Sin(x) using Taylor series approximation .Compare your result with the
built- in Library function. Print both the results with appropriate messages.
7.1 ALGORITHM
Page 36
Computer Programming Laboratory
7.3 PROGRAM
#include<stdio.h>
#include<math.h>
void main()
{
int i, j, n, degree;
float x, sum=0,term;
printf("Enter the value of degree");
scanf("%d",°ree);
Page 37
Computer Programming Laboratory
******************************************************************************
VIVA QUESTIONS:
1. What is pre-processor directive?
2. What is difference between const and #define.
3. What is use of fabs().
4. What is variable initialization and why is it important?
5. What is the difference between the = symbol and == symbol?
6. Can the curly brackets { } be used to enclose a single line of code?
Page 38
Computer Programming Laboratory
Laboratory Program 9
Write functions to implement string operations such as compare, concatenate, string length. Convince
the parameter passing techniques.
9.1 ALGORITHM
Purpose: To find String length, Compare two Strings, Concatenate two strings
Concatenated String
START
initialize i to 0
increment i by 1
initialize i to 0
break;
increment i by 1
return str1[i]-str2[i];
Page 39
Computer Programming Laboratory
initialize i to 0 and j to 0
increment i by 1
str1[i++]=str2[j++];
STOP
9.3 PROGRAM
#include<stdio.h>
int my_strlen(char str[])
{
int i=0;
while (str[i]!='\0') i++;
return i;
}
Page 40
Computer Programming Laboratory
}
str1[i++]='\0';
Page 41
Computer Programming Laboratory
9.2 FLOWCHART
Page 42
Computer Programming Laboratory
OUTPUT
concatinated string=RAMAKRISHNA
****************************************************************
VIVA QUESTIONS
1) What is string?
2) What are the built-in functions of string?
3) Difference between user defined functions and built in functions.
4) What is null character?
5) Explain the flow of program with example.
Page 43
Computer Programming Laboratory
Laboratory Program 8
Develop a program to sort the given set of N numbers using Bubble sort.
8.1 ALGORITHM
ALGORITHM : Bubble sort
PURPOSE : Arranging the numbers in ascending order using bubble sort technique
INPUT : N, interger numbers in arrary a[i]
OUTPUT : Numbers are arranged in ascending order
START
STEP 1: [Input number of elements]
Read n
STEP 2: [Input the elements/numbers into array]
For i 0 to n
Read a[i]
Endfor
STEP 3 : [Sorting the elements in ascending order]
For i o to n-1
For j o to n-i-1
[Compare the adjacent elements]
If(a[j]>a[j+1]) then
[Swap these elements]
Temp a[j]
a[j] a[j+1]
a[j+1] temp
endif
endfor
endfor
STEP 4: [Display the sorted elements of array]
For i 0 to n
Print a[i]
Endfor
STEP 5: [Finished]
Page 44
Computer Programming Laboratory
STOP
8.2 FLOWCHART
Page 45
Computer Programming Laboratory
8.3PROGRAM
#include<stdio.h>
void main()
{
int n,i,j,a[10],temp;
printf("%d ",a[i]);
for(i= 0 ; i < n-1 ; i++) // Number of Passes
{
for(j= 0; j< n-i-1; j++) // Comparisons
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("%d\t ",a[i]);
}
******************************************************************************
Page 46
Computer Programming Laboratory
Output 1:
Enter the no. of elements:5
54321
The Sorted elements are 1 2 3 4 5
******************************************************************************
VIVA QUESTIONS:
1. Why the name bubble sort?
2. Mention the different types of sorting techniques?
3. Explain the logic of bubble sort with an example.
4. What is nested for loop?
Page 47
Computer Programming Laboratory
Practice Program 4
Develop a program to find the square root of a given number N and execute for all possible inputs with
appropriate messages. Note: Don’t use library function sqrt(n).
0.1 ALGORITHM
temp 0
STEP 2: [Enter a number]
Read num
STEP 3: [Divide the num by 2]
square_root=num/2
STEP 4: [Calculate a square root of a number]
While (square_root not equal to temp)
temp square_root
square_root (num/square_root+square_root)/2
STEP 5: [Display results]
print “square root”
STEP 6: [Display result using the library function]
print” square root of a number is”, sqrt (num)
STEP 7: [Finished]
STOP
0.2 FLOWCHART
Page 48
Computer Programming Laboratory
Program
#include<stdio.h>
#include<math.h>
void main()
{
float num,square_root,temp=0;
printf("Enter the number to find square root\n");
scanf("%f",&num);
square_root=num/2;
while(square_root!=temp)
Page 49
Computer Programming Laboratory
{
temp=square_root;
square_root=(num/square_root+square_root)/2;
}
printf("Square Root of %f is %f\n",num,square_root);
printf("Square Root of %f using inbuilt Function is %f\n",num,sqrt(num));
}
******************************************************************************
Output 1:
Enter a number
49
Square root using program is 7
Square Root using Library Function is
7
Output2:
enter a number -12
Can't Find for Negative Numbers
******************************************************************************
VIVA QUESTIONS:
1. What is typecasting? Explain with examples.
2. Difference between float and double data types.
3. Explain for loop?
4. What is a use of break statement?
5. Difference between continue and break statement?
Page 50
Computer Programming Laboratory
Laboratory Program 10
Implement structures to read, write, compute average- marks and the students scoring above and
below the average marks for a class of N students.
10.1 ALGORITHM
PURPOSE: To Implement structures to read, write, compute average- marks and the
students scoring above and below the average marks for a class of N students.
Input: number of Students, name ,marks1,marks2,marks3
Output: Average marks and printing the students scoring above and below the
average marks
START
STEP 1: [input number of students]
Read n
STEP 2: [input details of students ie.name and marks]
Read name,m1,m2,m3
STEP 3: [ Calculate total and average]
For i=0 to n
s[i].total=(s[i].m1+s[i].m2+s[i].m3);
T=T+s[i].total;
AVG=T/N;
STEP 4: [Find students above and below average]
for i=0 to n
aboveavg[j]=i;
j++;
else
belowavg[k]=i;
k++;
STEP 5:[Finished]
STOP
Page 51
Computer Programming Laboratory
10.2 FLOWCHART
Page 52
Computer Programming Laboratory
10.3 PROGRAM
#include<stdio.h>
struct student
{
char name[20];
float m1,m2,m3,total;
};
void main()
{
int i,j,k,m,n,aboveavg[10],belowavg[10];
float N,T=0,AVG=0;
struct student s[10];
printf("Enter number of students\n");
scanf("%d",&n);
N=(float)n;
printf("Enter the details of students\n");
for (i=0;i<n;i++)
{
printf("Enter the details of student %d \n",i+1);
printf("Enter name \n");
scanf("%s",s[i].name);
printf("Enter m1,m2,m3 \n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=(s[i].m1+s[i].m2+s[i].m3);
T=T+s[i].total;
}
printf("Details of Students.\n");
printf("Name\t m1\t m2\t m3\t total\n");
for (i=0;i<n;i++)
printf("%s\t%f\t%f\t%f\t%f\t\n",s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total);
AVG=T/N;
Page 53
Computer Programming Laboratory
printf("AVG = %f \n",AVG);
j=0;
k=0;
for (i=0;i<n;i++)
{
if (s[i].total > AVG)
{
aboveavg[j]=i;
j++;
}
else
{
belowavg[k]=i;
k++;
}
}
printf("Students scoring above avg. \n");
for (i=0;i<j;i++)
{
printf("%s %f\n",s[aboveavg[i]].name,s[aboveavg[i]].total);
}
printf("Students scoring below avg. \n");
for (i=0;i<k;i++)
{
printf("%s %f\n",s[belowavg[i]].name,s[belowavg[i]].total);
}
}//end of main
*****************************************************************************
Page 54
Computer Programming Laboratory
OUTPUT
Enter number of students 3
Enter m1,m2,m3
67 84 72
Enter m1,m2,m3
76 55 68
Enter m1,m2,m3
58 79 92
Details of Students.
Name m1 m2 m3 total
AVG = 217.000000
Page 55
Computer Programming Laboratory
Anil 223.000000
Suresh 229.000000
Sudhir 199.000000
******************************************************************************
Page 56
Computer Programming Laboratory
Laboratory Program 11
Develop a program using pointers to compute the sum, mean and standard deviation of all elements
stored in an array of n real numbers.
11.1 ALGORITHM
Algorithm
STOP
Page 57
Computer Programming Laboratory
11.3 PROGRAM
#include<stdio.h>
#include<math.h>
void main()
{
Page 58
Computer Programming Laboratory
******************************************************************************
Viva Questions:
1. Define pointer?
2. How do you declare a pointer variable?
3. What is * and & in pointer concept.
4. What are the advantages and disadvantages of using pointer?
5. Give the difference between static allocation and dynamic allocation of memory space.
6. What is the effect of the ++ and -- operators on pointer variable
7. Explain the pointers to arrays concept?
Page 60
Computer Programming Laboratory
Laboratory Program 12
12.1 ALGORITHM
Algorithm
Purpose: To convert binary number to decimal number
START
remainder = n%10
n=n/10
decimalnumber=decimalnumber+ remainder*pow(2,i)
STOP
12.2 FLOWCHART
Page 61
Computer Programming Laboratory
12.3 PROGRAM
#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long n);
void main()
{
long n;
printf("Enter a binary number: ");
scanf("%ld", &n);
printf("%ld in binary = %d in decimal", n, convertBinaryToDecimal(n));
}
int convertBinaryToDecimal(long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
*******************************************************************************
OUTPUT 1:
OUTPUT 2:
Viva Question:
1) What is recursion?
2) What are binary numbers?
3) What is recursive function?
4) Explain the flow of program with example
5) Explain mathematical functions.
Page 63