Template For Front Page Lab Manual
Template For Front Page Lab Manual
17
Course Learning Objectives
Course Outcomes
SI. Page
No. Programs List: CO PO RBT No.
1 Familiarization with programming environment, concept of naming
the program files, storing, compilation, execution and debugging. CO1 PO3 L2 2
Taking any simple C- code.
PART – A
2. Develop a program to solve simple computational problems using
arithmetic expressions and use of each operator leading to CO2 PO3 L2 7
simulation of a Commercial calculator. (No built-in math function).
3 Develop a program to compute the roots of a quadratic equation by CO2 PO3 L2 11
accepting the coefficients. Print appropriate messages.
4. Develop a program to find the reverse of a positive integer and
check for palindrome or not. Display appropriate messages. CO2 PO3 L2 15
PART - B
11. Develop a program to sort the given set of N numbers using Bubble
sort. CO4 PO3 L2 51
12. 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). CO2,
CO4 PO3 L2 55
Page
SI.
Programs List: CO PO RBT No.
No.
13. Implement structures to read, write, and compute average- marks and
the students scoring above and below the average marks for a class of CO5 PO3 L3 60
N students.
14. Develop a program using pointers to compute the sum, mean and
standard deviation of all elements stored in an array of n real numbers. CO4 PO3 L3 66
Viva Questions 74
Computer Programming Laboratory Manual 18CPL27
PROGRAMS
Input Unit
This unit contains devices with the help of which we enter data into the computer. This unit
creates a link between the user and the computer. The input devices translate the information
into a form understandable by the computer.
This unit can store instructions, data, and intermediate results. This unit supplies information to
other units of the computer when needed. It is also known as internal storage unit or the main
memory or the primary storage or Random Access Memory (RAM).
Its size affects speed, power, and capability. Primary memory and secondary memory are two
types of memories in
the computer. Functions of the memory
unit are −
o It stores all the data and the instructions required for processing.
o It stores intermediate results of processing.
o It stores the final results of processing before these results are released to an
output device.
o All inputs and outputs are transmitted through the main memory.
Control Unit
This unit controls the operations of all parts of the computer but does not carry out any
actual data processing operations.
Functions of this unit are −
o It is responsible for controlling the transfer of data and instructions among other units
of a computer.
o It manages and coordinates all the units of the computer.
o It obtains the instructions from the memory, interprets them, and directs the operation
of the computer.
o It communicates with Input/Output devices for transfer of data or results from storage.
o It does not process or store data.
Logic Section
Function of logic section is to perform logic operations such as comparing, selecting,
matching, and merging of data.
Output Unit
The output unit consists of devices with the help of which we get the information from the
computer. This unit is a link between the computer and the users. Output devices translate the
computer's output into a form understandable by the users.
Following are some of the important input devices which are used in
a computer −
Keyboard
Mouse
Joy Stick
Light pen
Track Ball
Scanner
Graphic Tablet
Microphone
Magnetic Ink Card
Reader(MICR) Optical Character
Reader(OCR) Bar Code Reader
Optical Mark Reader(OMR)
Following are some of the important output devices used in a
computer Monitors
Graphic Plotter
Printer
Programming Environment
If we want to set up environment for C programming language, we need the following
two software tools available on computer, (a) Text Editor and (b) The C Compiler.
An integrated development environment (IDE) is a software application that provides
comprehensive facilities to computer programmers for software development. An IDE
normally consists of a source code editor, build automation tools, and a debugger. Most
modern IDEs have intelligent code completion. Some IDEs, such as NetBeans and
Eclipse, contain a compiler, interpreter, or both;
Naming the program files : A filename is a name used to uniquely identify a computer file
stored in a file system. The filename can consist of letters, digits and special characters. The
files that we create with editor are called the source files and they contain the program source
codes. The source files for C programs are typically named with the extension ".c".
Syntax: filename.ext ic, Eg: hello.c
$ gedit hello.c
Storing, Compilation, Execution and Debugging : The program is created and named and
stored in the disk. It can be referenced any time later by its filename.
Compilation is a process of converting source code in high level language to low level.
$gcc hello.c
The Translation is done after examine each instruction for correctness. When there are no errors,
the translated program is stored on another file with the name filename.o. This program is the
object code. Eg- hello.o.
Execution is the process of loading the executable object code into computer memory and
execute the instructions.
$ ./a.out
Debugging is a systematic process of spotting and fixing the number of bugs, or defects, in a
piece of software so that the software is behaving as expected.
Let us look at a simple code that would print the words "Hello World" −
#include<stdio.h>
int main() {
/* my first program in
C */ printf("Hello,
World! \n"); return 0;
}
Let us take a look at the various parts of the above program −
The first line of the program #include <stdio.h> is a preprocessor command, which tells a C
compiler to include stdio.h file before going to actual compilation.
The next line int main() is the main function where the program execution begins.
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments
in the program. So such lines are called comments in the program.
The next line printf(...) is another function available in C which causes the message "Hello, World!"
to be displayed on the screen.
The next line return 0; terminates the main() function and returns the value 0.
Compile and Execute C Program :Let us see how to save the source code in a file, and how to compile
and run it.
Open a text editor and add the above-mentioned code.
Save the file as hello.c
Open a command prompt and go to the directory where you have saved the file.
Type gcc hello.c and press enter to compile your code
If there are no errors in your code, the command prompt will take you to the next line and
would generate a.out executable file.
Now, type a.out to execute your program.
You will see the output "Hello World" printed on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!
PART A
2. 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)
Algorithm:
Step1: [Initialize]
start
Step2: [Input a simple arithmetic expression]
Read a,op,c
Step3: [Check for arithmetic operators using switch case with operator op]
switch(op)
3.1 case '+': res=a+b; goto step 4
3.2 case '-' : res=a-b; goto step 4
3.3 case '*': res=a*b; goto step 4
3.4 case '/':
if(b!=0)
res=a/b;
else
Flowchart:
TEST CASE:
Algorithm:
Step1: [Initialize]
start
Step2: [Input the coefficients of quadratic equation]
Read a,b,c.
Step3: [Check for valid coefficients]
If (a==0)
Then invalid coefficients. Go to step 6
Step4:[Compute discriminate value as d]
d=b*b-4*a*c
Step5: [Check for d value to compute roots]
5.1:if (d>0)
then
print real and distinct roots
root1=(-b+√d)/(2*a)
root2=(-b-√d)/(2*a)
print
root1,root2 go
to step 6
5.2 :if (d==0) then
print real and equal roots root1 = root2= -b/(2*a)
print root1,root2
go to step 6
Step 5.3:if (d<0) then
print real and imaginary real=-b/(2*a)
imag=(√fabs(d))/(2*a) print root1 as real+iimag print
root2 as real-I imag
goto step 6
Step 6: [Finished] Stop
Flowchart:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float a, b, c, d,root1,root2,real,imag;
printf("enter the coefficients a, b, c:\n");
scanf("%f%f%f",&a,&b,&c); if(a==0||
b==0||c==0)
{
printf("Invalid coefficients\n");
exit(0);
}
d=b*b-4*a*c;
if(d>0)
{
root1=(-b+sqrt(d))/(2.0*a);
root2=(-b-sqrt(d))/(2.0*a);
printf("the roots are real and distinct...\n");
printf("root1=%.2f\n”,root1);
printf(“root2=%.2f\n",root2);
}
else if(d==0)
{
root1=root2=-b/(2.0*a);
printf("the roots are real and equal..\n");
printf("root1=%.2f\n”,root1);
printf(“root2=%.2f\n",root2);
}
else
{
real= -b/(2.0*a);
imag=sqrt(fabs(d))/(2.0*a);
printf("the roots are complex and imaginary..\n");
printf("root1=%.2f+i%.2f\n",real,imag);
printf(“root2=%.2f-i%.2f\n",real,imag);
}
return 0;
}
TEST CASE:
While (n!=0)
rem=n%10
rev=rev*10+rem
n=n/10
End while
Step 5: [Print reverse number]
Print rev
Step 6: [Check if original number and reverse number are the same. If it is, number is palindrome.
otherwise, not palindrome]
If rev==num then
print "The number is palindrome"
Else
print “The number is not palindrome"
End IF
Step 7:[Finished] Stop
Flowchart:
Program
#include<stdio.h>
int main()
{
int n, rev=0, rem,num;
printf("enter a number:\n");
scanf("%d",&num);
n = num;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“\n reversed num is %d”,rev);
if(num==rev)
printf("\n %d is palindrome",num);
else
printf("\n%d is not palindrome",num);
return 0;
}
TEST CASE:
Algorithm
Step 1: [Initialize]
Start
Step 2:[Read the name of the user and number of units of electricity consumed]
Read name,units
Step 3: [Use else if ladder, to satisfy the given condition: 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(units<=200)
rupees=100+units*0.80;
else if(units<=300 && units>200)
rupees=100+200*0.80+(units-200)*0.90;
else
rupees=100+200*0.80+100*0.90+(units-300)*1.00;
end if
Step 4:[Use simple if to satisfy, the total amount is more than Rs 400, then an additional surcharge
of 15% of total amount is charged]
if(rupees>400)
rupees=rupees+0.15*rupees;
end if
Step 5:[Print the name of user and amount to be paid]
Print name, rupees
Step 6:[Finished]
Stop
Flowchart:
Program
#include<stdio.h>
int main()
{
char name[20];
int units;
float rupees=0;
printf("\n enter the name of the user :");
gets(name);
printf("\n enter number of units consumed :");
scanf("%d",&units);
if(units<=200)
rupees=100+units*0.80;
else if(units<=300 && units>200)
rupees=100+200*0.80+(units-200)*0.90;
else
rupees=100+200*0.80+100*0.90+(units-300)*1.00;
if(rupees>400)
rupees=rupees+0.15*rupees;
printf("%s has to pay rupees %.2f",name,rupees);
return 0;
}
Test Cases:
Algorithm
Step 1: [Initialize]
Start
Step 2:[Read number of numbers and the numbers:n and n numbers in ascending order in an
array]
Read n, a[]
Step 3: [Read the number to be searched as key]
Read key
Step 4:[Set low and high]
low=0
high=n-1
Step 5:[Iterate using while loop until low<=high and !found]
while(low<=high && !found)
mid=(low+high)/2
if(a[mid]==key)
found=1
else if(a[mid]<key)
low=mid+1
else
high=mid-1
end if
end while
Step 6:[Finished]
Stop
Flowchart:
Program:
#include<stdio.h>
#include<string.h>
int main()
{
int a[10],key;
int n,i,low,high,mid,found=0;
printf("enter the number of numbers to read\n");
scanf("%d",&n);
printf("enter the numbers in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the number to search\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(a[mid]==key)
found=1;
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(found==1)
printf("\n number found at position:%d",mid+1);
else
printf("\n number not found");
return 0;
}
Test Cases:
Test cases:
Algorithm
Step 1: [Initialize]
start
Step 2:[Read the order and the matrices (2D array) from the
user] Read order of matrices a[ ] [ ] and b[ ][ ] i.e m*n and
p*q Read a[][] and b[][] arrays.
Step 3: [check for possibility of multiplication]
if n!=p
Print Matrix multiplication not possible.
goto step 6
else
goto Step 4
end if
Step 4:[Multiply the two matrices a*b(nested for is used)]
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];
end for
end for
end for
Step 5:[Display the resultant C Matrix]
Print the input matrices a[ ][ ],b[ ][ ] and resultant matrix c[ ][ ]
Step 6:[Finished]
Stop
Flowchart:
Program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],c[10][10], m,n,p,q,i,j,k;
printf("Enter the order of the matrix A and order of the matrix B\n");
scanf("%d%d%d%d",&m,&n,&p,&q);
if(n!=p)
{
printf("\n multiplication not possible");
exit(0);
}
else
{
printf("Enter the elements of matrix A...\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
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]);
}
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 Product of A and B matrices : MATRIX C\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
}
}
return 0;
}
Test cases:
Test Input Parameters Expected Output Obtained Output
No
1 Enter the order of the matrix A 2 2 MATRIX A MATRIX A
Enter the order of the matrix B 2 2 12 12
Enter the elements of matrix A... 34 34
1234 MATRIX B MATRIX B
Enter the elements of matrix B... 56 56
5678 78 78
Product of A and B Product of A and B
matrices : MATRIX C matrices : MATRIX C
19 22 19 22
43 50 43 50
2 Enter the order of the matrix A 2 3 multiplication not multiplication not
Enter the order of the matrix B 2 2 possible possible
Enter the elements of matrix A...
123456
Enter the elements of matrix B...
5678
OUTPUT
Algorithm
Step 1: [Initialize]
Start
Step 2: [Set PI=3.142]
PI=3.142
Step 3: [Reading degree]
read degree
Step 4:[Calculate x to convert degree to radians]
x=degree*(PI/180)
Step 5:[Set the intial values of nume,deno,i]
nume=x;
deno=1;
i=2;
Step 6:[Iterate using do while loop till term>=0.000001]
do
term=nume/deno;
nume=-nume*x*x;
deno=deno*i*(i+1);
sum=sum+term;
i=i+2;
while(fabs(term)>=0.000001);
Step 7: [Display the result]
Print degree and sum.
Also, print using built in function sin(x).
Step 8: [Finished]
Stop
FlowChart:
Program
#include<stdio.h>
#include<math.h>
#define PI 3.142
int main()
{
int i,degree;
float x,sum=0,term,nume,deno;
printf("enter the value of degree");
scanf("%d",°ree);
x=degree * (PI/180);
nume=x;
deno=1;
i=2;
do
{
term=nume/deno;
nume=-nume*x*x;
deno=deno*i*(i+1);
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.00001);
printf("\nThe sine of %d is %.2f",degree,sum);
printf("\nThe sine function of %d is %.2f using library function",degree,sin(x));
return 0;
}
Test cases:
Algorithm
Step 1: [Initialize]
start
Step 2: [Input two source strings]
read source1,source2
Step 3: [Caculate length1 of source1 by calling the user defined function,strlength(); Repeat the
same for length2 of source2 ]
length1=strlength(source1);
length2=strlength(source2);
Step 4: [Ouput length1,length2]
Print length1,length2
Step 5: [Compare the two strings by calling the user defined function,strcompare()]
k=strcompare(source1,source2);
Step 6: [check k, to find the whether the strings are same or not]
if(k==0)
print Both strings are same
else
print strings are different
end if
Step 7: [Concatenate two strings by calling the user defined function,strconcat() and the
concatenated string is stored in source1]
strconcat(source1,source2);
print source1
Step 8: [Finished]
Stop
Algorithm
Step 1: [Initialize]
start
Step 2: [Input number of elements]
read n
Step 3: [Input unsorted elements in an array]
Read elements in array a[]
for(i=0;i<n;i++)
read a[i]
end for
Step 4: [output unsorted elements in array]
Print elements in array a[]
for(i=0;i<n;i++)
print a[i]
end for
Step 5:[Iterate array a[] in two loops. Outer loop gives number of passes. Inner loop does n-I
comparisions and swap task. In each pass, compare each pair of adjacent items. If former
element is greater than latter one, swap them.]
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
end if
end for
end for
Step 6: [Display sorted elements in array]
Print elements in array a[]
for(i=0;i<n;i++)
print a[i]
end for
Step 7: [Finished]
Stop
Flowchart:
Program
#include<stdio.h>
int main()
{
int n,i,j,a[10],temp;
printf("\n enter the number of elements");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\nThe sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
Test cases:
Algorithm:
#include<stdio.h>
#include<math.h>
int main()
{
float n,s;
int i;
printf(“\n Enter the number to find the square root\n”):
scanf(“%f”,&n);
if(n>0)
{
s=n/2;
for(i=0;i<n;i++)
s=(s+(n/s))/2;
printf(“Square root without using Library function is %.2f\n”,s);
printf(“Square root using Library function is %.2f\n”,sqrt(n));
}
else
printf(“\n Not possible to find square root”);
return 0;
}
Test Cases:
Algorithm
Step 1: [Initialize]
Start
Step 2:[Create a structure student]
Create a structure called student with fields rollno,name,marks,grade with suitable datatypes.
struct student
{
int rollno;char name[20]; float marks;
};
Step 3:[Set found=0, sum=0]
found=0
Step 4:[Read the number of student details]
Read n
Step 5: [Read n student details. Iterate using a for loop for inputting rollno, name, marks of each student]
for(i=0;i<n;i++)
printf(“\nenter the %d student details”,i+1);
printf(“\n enter roll number:”);
scanf(“%d”,&s[i].rollno);
printf(“\n enter student name:”);
scanf(“%s”,s[i].name);
printf(“\n enter the marks:”);
scanf(“%d”,&s[i].marks);
end for
Step 6:[Display the n student details]
Print the rollno, name, marks of all students by iterating using a for loop.
Step 7:[Calculate sum of the marks of all students]
for(i=0;i<n;i++)
sum=sum+s[i].marks
end for
Step 8:[calculate average]
average=sum/n;
Step 9:[Display the names of students scoring above average]
for(i=0;i<n;i++)
if(s[i].marks>=average)
printf("%s\t",s[i].name)
end if
end for
Step 10:[Display the names of students scoring below average]
for(i=0;i<n;i++)
if(s[i].marks<average)
printf("%s\t",s[i].name)
end if
end for
Step 11:[Display no students scored below average, if found==0]
if(found==0)
printf("\n no students scored below average")
end if
Step 12:Stop
Flowchart:
Program
#include<stdio.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
float marks;
};
int main()
{
int i,n,found=0;
struct student s[10];
float sum=0,average;
printf("\nEnter the number of student details");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the %d student details",i+1);
printf("\n enter roll number:");
scanf("%d",&s[i].rollno);
printf("\n enter student name");
scanf("%s",s[i].name);
printf("\n enter the marks:");
scanf("%f",&s[i].marks);
}
printf("\nStudent details are\n");
printf("\nRollno\t\tName\t\tMarks\n");
for(i=0;i<n;i++) printf("%d\t\t%s\t\t
%f\n",s[i].rollno,s[i].name,s[i].marks);
for(i=0;i<n;i++)
sum=sum+s[i].marks;
average=sum/n; printf("\nAVERAGE=
%,2f",average); printf("\n students scoring
above average\n"); for(i=0;i<n;i++)
{
if(s[i].marks>=average)
{
printf("%s\t",s[i].name);
}
}
printf("\n students scoring below average\n");
for(i=0;i<n;i++)
{
if(s[i].marks<average)
{
printf("%s\t",s[i].name);
found=1;
}
}
if(found==0)
printf("\n no students scored below average");
return 0;
}
Test cases Test No Input Parameters Expected Output Obtained Output
1 Enter the number of Student details are Student details are
student details 4 Rollno Name Marks Rollno Name Marks
enter the 1 student 100 austin 56.000 100 austin 56.000
details 200 banu 67.000 200 banu 67.000
enter roll number: 100 300 charan 90.000 300 charan 90.000
enter student 400 dina 69.000 400 dina 69.000
name:austin Average=70.500000 Average=70.500000
enter the marks: 56 Students scoring above Students scoring above
enter the 2 student average charan average charan
details Students scoring below Students scoring below
enter roll number: 200 average average
enter student austin banu dina austin banu dina
name:banu
enter the marks: 67
enter the 3 student
details
enter roll number: 300
enter student
name:charan
enter the marks: 90
enter roll number: 400
enter student name:dina
enter the marks: 69
2 Enter the number of Student details are Student details are
student details 4 Rollno Name Marks Rollno Name Marks
enter the 1 student 100 austin 100.000 100 austin 100.000
details 200 banu 100.000 200 banu 100.000
enter roll number: 100 300 charan 100.000 300 charan 100.000
enter student 400 dina 100.000 400 dina 100.000
name:austin Average=100.000000 Average=100.000000
enter the marks: 100 Students scoring above Students scoring above
enter the 2 student average
details
enter roll number: 200
enter student
name:banu
enter the marks: 100 Austin banu charan dina averaavgeerage
enter the 3 student details Students scoring below AusAtinusbtainnubacnhua
ina
enter roll number: 300 average cahnaran d dinaStudents
enter student name:charanNo students scored belo scoring below
enter the marks: 100 average wStudaevnetrsasgceoring below
low
enter roll number: 400 averNagoestudents scored
enter student name:dina be No satvuedreangtes scored
enter the marks: 100 below average
OUTPUT
Algorithm
Step 1: [Initialize]
Start
Step 2:[Read the no of elements and array elements]
Read n
Read a[]
Step 3: [Set starting address of array to a pointer variable]
ptr=a
Step 4:[Iterate using a for loop to find sum using pointers]
for(i=0;i<n;i++)
sum=sum+*ptr
ptr++
end for
Step 5:[Calculate mean]
mean=sum/n
Step 6: [Set starting address of array to a pointer variable]
ptr=a
Step 7:[Iterate using a for loop to find sumstd using pointers]
for(i=0;i<n;i++ )
sumstd=sumstd+pow((*ptr-mean),2)
ptr++
end for
Step 8:[Calculate standard deviation]
std=sqrt(sumstd/n)
Step 9:[Display the result]
Print sum,mean,std
Step 10:[Finished]
Stop
Flowchart:
Program
#include<stdio.h>
#include<math.h>
int main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
intn,i;
printf(“\n Enter the number of elements”);
scanf(“%d”,&n);
printf(‘\n Enter the array elements”);
for(i=0;i<n;i++)
{
scanf(‘%f”,&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2); ptr+
+;
}
std=sqrt(sumstd/n); printf(“Sum=
%.2f\n”,sum); printf(“Mean=
%.2f\n”,mean); printf(“Standard
Deviation=%f\n”,std); return 0;
}
Test cases:
Test No Input Parameters Expected Output Obtained Output
1 Enter the number of Sum= 28 Sum= 28
elements 5 Mean= 5.6 Mean= 5.6
Enter the array Standard Deviation= Standard Deviation=
elements 1 5 9 6 7 2.09 2.09
2 Enter the number of Sum= 10.68 Sum= 10.68
elements 4 Mean= 2.67 Mean= 2.67
Enter the array Standard Deviation= Standard Deviation=
elements 2.3 1.1 4.5 0.863 0.863
2.78
OUTPUT
Algorithm
Step 1: [Initialize]
Start
Step 2:[Set decimal=0]
Set decimal=0
Step 3:[Enter the binary input]
Read binary
Step 4: [Find the length of binary using strlen() and store in len]
len=strlen(binary)
Step 5:[call the user defined function recursion to find the decimal value for binary]
recursion(binary, &decimal, &len);
Step 6:[Display decimal value]
Print decimal
Step 7:[Finished]
Stop
Algorithm (user defined function - recursion())
Step 1: [Initialize]
Start
Step 2:[set static int num, i=0]
static int num, i = 0
Step 3: [Until len becomes zero,do the following steps and call recursively, recursion again and again]
if (*len > 0)
*len = *len - 1;
num = *(binary + *len) - '0';
*decimal = *decimal + (num * pow(2, i++));
recursion(binary, decimal, len);
end if
Step 4: [return to the calling function main()]
Return
Flowchart:
Program
#include <stdio.h>
#include <string.h>
#include <math.h>
void recursion(char *binary, int *decimal, int *len)
{
static int num, i = 0;
if (*len > 0)
{
*len = *len - 1;
num = *(binary + *len) - '0';
*decimal = *decimal + (num * pow(2, i++));
recursion(binary, decimal, len);
}
return;
}
int main()
{
char binary[256];
int decimal = 0, len;
printf("Enter the binary input:");
gets(binary);
len = strlen(binary);
recursion(binary, &decimal, &len);
printf("Decimal Value: %d\n", decimal);
return 0;
}
Test cases:
3. What is a compiler?
Compile is a software program that transfer program developed in high level language intoexecutable
object code.
5. What is a program?
A computer program is a collection of the instructions necessary to solve a specific problem.
6. What is an algorithm?
The approach or method that is used to solve the problem is known as algorithm.
9. What is a Keyword?
Keywords are building blocks for program statements and have fixed meanings and these meanings cannot
be changed.
13. . What are the Back Slash character constants or Escape sequence characters available in C?
Back Slash character constant are \t, \n, \0
18. What are integer variable, floating-point variable and character variable?
A variable which stores integer constants are called integer variable. A variable which stores real values
are called floating-point variable. A variable which stores character constants are called character
variables.
Structure
1-It is a collection of data items of different data
type. 2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.
29.Out of fgets() and gets() which function is safe to use and why?
fgets() is safer than gets(), because we can specify a maximum input length. Neither one is
completely safe, because the compiler can‘t prove that programmer won‘t overflow the buffer
he pass to fgets ().