Sei sulla pagina 1di 74

-COMPUTER

PRACTICE LABORATORY-I

GE6161 -COMPUTER PRACTICES LABORATORY

LIST OF EXPERIMENTS:

1. Search, generate, and manipulate data using MS office / Open Office

2. Presentation and Visualization graphs, charts, 2D, 3D

3. Problem formulation, Problem Solving and Flowcharts

4. C Programming using Simple statements and expressions

5. Scientific problem solving using decision making and looping.

6. Simple programming for one dimensional and two dimensional arrays.

7. Solving problems using String functions

8. Programs with user defined functions Includes Parameter Passing

9. Program using Recursive Function and conversion from given program to flow

chart.

10. Program using structures and unions.

TOTAL: 45 PERIODS

1 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex.no:1 Implement the following operations using MS office :

Search, generate, and manipulate data , Presentation and Visualization of

graphs, charts, 2D, 3D and Problem formulation, Problem Solving using

Flowcharts by

A) Word Processing

B) Spread Sheet

C) Power point presentation

A. Word Processing

A.1 Document Creation, Text manipulation With

Scientific Notation

Procedure:

Step 1: Open a new document and perform the following tasks:


Open a new document using File New option and type the text.

For saving the document, press Ctrl +S or click on save button on


Standard tool bar or select the Save option from the File menu. Choose a
suitable file name in the File menu. Then it shows the Save As dialog box.
Choose a suitable file name in File Name Text Box , then click Save button
on Save As dialog Box. Now the document is saved under a given name.

Step 2: Alignment

Select the text for alignment.

For Left Alignment press Ctrl+L or press Align Left Button on the
formatting toolbar.

For Justification press Justify button on the formatting tool bar.


2 R. M. K COLLEGE OF ENGINEERING &T
ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 3: For Adding Bullets and numberings

Select the text where you want to add Bullets and Numberings.

Then click Bullets on the Formatting Tool Bar or choose Bullets and
Numbering option from the Format menu. It shows the Bullets and
Numbering dialog box .

Step 4: To add box on the entire text, do the following steps

Choose Select All option from Edit menu or by pressing Ctrl +A.

Click Border icon in the Formatting Tool Bar.

Step 4: Inserting Symbols

Type the text, then put the cursor where you want to add the symbol.

Choose Symbols Option from the Insert menu.

Step 5: Equations

Enter the Equation in the document.

For Subscript, Choose the Font option from the Format menu, then enable
the superscript check box from the Font dialog box.

Step 6: For Typing the Formulas

Click on Insert Object.

Now the object dialog box will be opened.

Choose Microsoft Equation 3.0, then click on OK.

Step 7: Letter head of the Company

Open a new document.

To enter a header that repeats on all pages in your document choose Header
and footer.

3 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 8: Curriculum Vitae

Select Table option on the menu bar, choose Insert->Table option.

Set Number of Columns, Number of Rows and Column Width in the


Insert Table dialog box and then click ok. The table will be created.

A. 2 Create Table with Contents

Procedure:
Step 1: For Table Insertion

Chose Table->insert->table option


It shows insert table dialog box
Click OK to add table in your document
Step 2: Table
Selectable menu bar, then choose Insert table option.
Shows Insert table dialog box
Emp name Basic DA HRA Total salary
kamala 6000 600 110)0 7700
Raja 3000 300 500 3800
Grand salary 11500
Maximum 7700
Minimum 3800

Step 3: Splitting cell

Select a cell in a table by clicking on it.


Click table menu from the menu bar.
select split cell option from the table menu, the split cells dialog box
appears
Enter your choice to split cells and click ok.

Step 4: Sorting table contents

4 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Insert the table and type the information


Select the entire table and then chose Sort option from table menu.

Step 5: Total salary calculation

Insert the table and type information.


Place the cursor in the cell at which total salary to be displayed.
Choose Formula option from the table menu.
Now it shows the formula dialog box with the formula [i.e. Sum (ABOVE)]

Step 6: Convert table to text

Select the table in the document.


Chose the convert table to text options from the table menu.
Now it shows convert table to text dialog box.
Step 7: Convert Text to table
Select the text in the document.
Choose the convert text to table options from the table menu.

5 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

A.3 Mail Merge

Procedure:
Step1: Mail Merge
Select File option in the menu bar and click New, to open a new document.
From the Tools menu select the mail merge
It shows the mail merge helper dialog box.
In the Main Document area of mail merge helper dialog box, click on
create button.
Step 2: Creating a data source
In the mail merge helper dialog box, click Get data button
select create Data source... to get create Data Source dialog box.
Step 3: Creating the merged document
Click on the Insert Merged Field on the mail merge toolbar.
Selected the desired results from the drop down menu of the insert merge
field.
After inserting the merge fields, choose Tools->mail merge
Step 4: Printing labels
Choose tools->Envelopes and labels option
It shows the Envelopes and labels dialog box.
Step 5: Printing envelope letters
Choose Tools->Envelope and Labels option.
It shows Envelope and Labels dialog box.

6 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

A.4 Drawing And Flow Chart creation

Procedure:
Step 1: Drawing Picture
Choose the Up Ribbon option in the Stars and Banners of the Auto
Shapes menu in the Drawing Toolbar.

Draw the required size of Up Ribbon in the document.

Click on the Insert Word Art icon in the drawing tool.

Step 2: Inserting Picture


Choose Insert-> Picture -> From File option.
It shows the Insert Picture dialog box is opened.
Then click on Insert button. It inserts the selected picture into the document.
Then it automatically shows the Picture toolbar.
Step 3: Adding Shadows and 3 d effects to the Word art
Click on the Insert WordArt icon in the drawing toolbar.
Now the WordArt gallery dialog box is opened with different style.
Choose the required style, then click Ok.
Now the Edit WordArt Text box dialog box is shown.
Step 4: Flow Chart
Choose Flowchart option in the Auto shapes menu of the Drawing Toolbar.
Choose the appropriate flowchart symbol, then drag the symbol in the
document.

7 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Type the text in the Symbol, but right click then choose Add Text.
Using the Arrow button Line Style Using the Arrow button Line Style Arrow
Style in the drawing Toolbar draw the lines connecting to the different
flowchart symbols.

B. Spreadsheet

B.1 Charts and graphs In Spreadsheet

Procedure:
Step1: chart in excel
Enter the data in the Worksheet as follows
Choose Chart option from the Insert menu, or choose the chart wizard
button from the Insert menu, or choose the Chart Wizard button from the
Standard toolbar.

It shows the chart Wizard Step 1 of 4 Chart type dialog box.

Choose the appropriate Chart type from the Chart type list box then click
Next.

Shows the Chart Wizard Step 2 of 4 Chart source Data dialog box, then
click Next.

Shows the Chart Wizard Step 3 of 4 Chart options dialog box, here we can
give the Chart Titles ie X and Y-Axis Titles etc then click Next.

Shows the Chart Wizard step 3 of 4 Chart Locations dialog box the click
Finish.

8 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 2 : Graphs in excel

Type the data as given in the worksheet.


Click at cell address A1.Click Insert-> Chart.
Excel will display the first dialog box of Chart Wizard.
Select Bar graph and click on Next.
Click on Data Range from the Chart Wizard dialog box.
In the same way, repeat the steps for creating 3D and PIE chart.
Step 3: Inserting ClipArt to the chart
We can insert an image, Eg: a bitmap for certain types of data markers, the
chart area,
or the legend in 2-D and 3-D charts, or the walls and the floor in 3-D
charts.
To change the chart type of the e4ntire chart, click the chart.

B.2 Formula,Formula Editor

Procedure:
Step 1:For calculation
ex: Employee pay
Enter the employee details as shown in the table above
click in cell D2, type =IF(C2<8000,C2*56/100,C2*44/100)
click in cell F2, type =C2*0.1
click in cell G2, type =C2+D2+E2
Click in cell H2 type=G2-F2
copy the formulae to the remaining records
Place the cursor in H8,type=MAX(H2:H4)
The maximum salary will be displayed.) Place the cursor in
H9,type=MIN(H2:H4)
The minimum salary will be displayed.
ex: To find the sum of the marks
Enter the student name, marks in three subjects.
Place the cursor in E2, type=SUM (B2...D3)

9 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Place the cursor in F2, type=AVERAGE (B2...D3)


Click on G2, type
=IF(OR(B2<35,C2<35,D2<35)FAIL,IF(F2>=75,DISTINCTION,
IF(F2>=60,FIRSTCLASS, IF(F2>=50,SECOND CLASS,THIRD
CLASS))))
Copy the formulae to the remaining students

Step 2:Conversions
Click Tools->Add ins dialog box
In the Add-ins dialog box, click the checkbox of Analysis toolpak,Analysis
toolpak-VBA
Click on A2, enter the decimal number in rows. Click on B2,
type=dec2bin(A2), copy the formula to the remaining, as it converts decimal
number to binary.

B.3 Inclusion Of Objects, Pictures And Graphics


Protecting the Document And Sheet
Procedure:

Step1 : Inserting Objects in the Worksheet

There are so many objects that can be inserted into the worksheets.

Choose Objects option in the Insert menu.

Choose Picture option from the Insert Menu.

Choose appropriate object from the Auto shape menu of the Drawing
toolbar.

Step 2: Inserting a picture:

Choose picture option in the Insert Menu.


Then choose From file option.
Then click on Insert button.

10 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

.Step 4: Organization Chart


Choose Organization chart option in the picture option of the Insert Menu.
Step 3 : Inserting a Function
Click on Paste Function button in the Standard Toolbar or choose
Function Option
in the Insert Menu.
Step 4: Establish link between two or more worksheets.
Open a New Workbook, from File -> New option.
Click on any cell and then choose Data->Consolidate option.
It shows the Consolidation dialog box.
Click on Browse button to browse other worksheet.
Step 5: Show the worksheet in 3D effect
Click on the 3D Effects auto format.
Now the worksheet is shown in 3D format style.
Step 6: Protect a sheet in MS-Excel
Click on cell, which you want to protect.
Choose Tools->Protection, then choose Protect Sheet, it shows the Protect
Sheet
dialog box asking the Password to protect the sheet.
To remove protection, Choose Tools->Protection->Unprotect sheet then
type the

password to unprotect.
.
Step 7: Protecting the Workbook
Open the Workbook to protect.
Choose Tools->protection, then choose Protect Workbook, it shows the
Protect
Workbook dialog box asking the Password to protect sheet.
To remove protection, choose Tools->Protection->Unprotect sheet, then
type the same
Password to unprotect.

11 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

B.4 Sorting And Import/ Export Features

Procedure:
Step 1 : Enter the data into the Worksheet
Enter the Name, Department, Salary in different columns.

Click Data-> Form option, it shows the form.

Click New button in the Form to add the rows to the list.

After adding the rows, place the cursor in the list in the worksheet.

Click Data-> Sort, it shows the Sort dialog box.

In the Sort by drop down list select Dept ,click Ok.

Click Data->Filter->Auto filter, it shows an arrow at each field.

On the Dept click the icon select Accounts.

The list of rows which belongs o Accounts is displayed.

Step 2: Subtotals

The Subtotals are helpful to analyze the data in the list and quickly
create reports and
Charts.

12 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Type the list in a worksheet with column as Salesman, Product, Sale.


Select all, then Sort the sales Column to add subtotals.
Click Data->Subtotals
The Subtotal dialog box appears.
Select the column name from At each change in drop down list box to add
subtotals.
Use the Sum function from the Use function drop down list box.
Step 3: Saving workbook for use in another program

Open the workbook we want to save for use in another program.


On the File menu, click Save As
In the File name box ,type a new name for the workbook.
In the Save as type list, click a file format that we can open in the other
program. Click on Save button.

C. Power Point Presentation

Procedure:

Step 1 : To open a new powerpoint start All Programs Microsoft


Office
Microsoft Office Power point.
In a new document perform the following tasks:
Open a new document using File New option and type the text.

For saving the document, press Ctrl +S or click on save button on


Standard tool bar or select the Save option from the File menu. Choose a
suitable file name in the File menu. Then it shows the Save As dialog box.
Choose a suitable file name in File Name Text Box , then click Save button
on Save As dialog Box. Now the document is saved under a given name.

Open the presentation you want to view as a slide show.

Do one of the following:

13 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Click Slide Show at the lower left of the PowerPoint window.

On the Slide Show menu, click View Show.

Press F5.

Usage of powerpoint:

Using Power point to Create slides and add text.


Using slide layout to change the slide layout, insert a picture, and
apply a theme.
Change the size and position of slide elements.
Format pictures, shapes, and other slide content.

Using Animation tab we give animation for the slides. In Design tab
it have various templates for slides

Using insert menu insert the pictures, shapes, cliparts, chart, textbox,
wordart and so on.

Ex.no:2 C Programming using simple statements and expressions

Ex.no: 2a Area and Circumference of the circle

Aim: To write a program to find the area and circumference of the circle.

Algorithm:

Step 1: Start the program.


Step 2: Input the radius of the circle.
Step 3: Find the area and circumference of the circle using the formulae
Area=3.14 * r *r
Circumference= 2 * 3.14 * r
Step 4 : Print the area and circumference of the circle.
Step 5: Stop the program.

Flow Chart:

14 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Start

Read radius

Area=3.14 * r *r

circumference= 2 * 3.14 * r

Print
area,circumference

Stop

Source Code:

#include<stdio.h>
#include<conio.h>
void main()
{
float radius,area,circum;

printf("Enter the radius");


scanf("%f",&radius);
area=3.14*radius*radius;
circum=2*3.14*radius;
printf("\nArea=%f",area);
printf("\nCircumfrerence=%f",circum);
getch();

15 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Output:

Enter the radius 4


Area=50.240002
Circumfrerence=25.120001

Ex No: 2b Convert Celsius into Fahrenheit

Aim: To write a program to convert Celsius into Fahrenheit

Algorithm:

Step 1: Start the program.


Step 2: Read the Celsius value.
Step 3: Calculate the Fahrenheit value using the formulae
Fahrenheit=(1.8*Celsius)+32
Step 4 : Print the Fahrenheit vlaue.
Step 5: Stop the program.

16 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Flow Chart:
Start

Read Celsius

Fahrenheit=(1.8*Celsius)+32

Print Fahrenheit

Stop

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
float cels,fah;
clrscr();
printf("Enter the temperature in Celsius:");
scanf("%f",&cels);
fah=(1.8*cels)+32;
printf("\nConverted Temperature is %.2f%cF",fah,248);
getch();
}
17 R. M. K COLLEGE OF ENGINEERING &T
ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Output

Enter the temperature in Celsius:37


Converted Temperature is 98.60C

Ex.No:2c Display the size of each data type

Aim: To write a program to display the size of each data type in C

Algorithm:

Step 1: Start the program.


Step 2: Use the sizeof() operator to display the size of each datatype
Step 3 : Print the Fahrenheit vlaue.
Step 4: Stop the program.

Flow Chart

18 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Start

Compute the size of datatypes


i=sizeof(int)
l=sizeof(long)
f=sizeof(float)
d=sizeof(double)
c=sizeof(char)

Print the sizes

Stop

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
double a;
clrscr();
printf("\nSize of different datatypes in C");
printf("\n--------------------------------");
printf("\nSize of Integer : %d bytes",sizeof(int));
printf("\nSize of Long : %d bytes",sizeof(long));
printf("\nSize of Float : %d bytes",sizeof(float));

19 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

printf("\nSize of Double : %d bytes",sizeof(a));


printf("\nSize of Character : %d byte",sizeof(char));
printf("\nSize of Unsigned Integer : %d bytes",sizeof(unsigned int));
getch();
}

Output

Size of different datatypes in C


--------------------------------
Size of Integer : 2 bytes
Size of Long : 4 bytes
Size of Float : 4 bytes
Size of Double : 8 bytes
Size of Character : 1 byte
Size of Unsigned Integer : 2 bytes

Ex no: 2d Evaluate the expression

Aim: To write a C program to evaluate


x = 8t2-t+4
y=sin 30
z=e3t+5
Algorithm:
Step 1: Start the program.
Step 2: Read t
Step 3 : Calculate
x = 8t2-t3+4

20 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

y=sin 30
z=e3t+5
Step 4: Print the value of x,y and z.
Step 5: Stop the program

Flow Chart

Start

Read t

X=8*pow(t,2)-t+4
Y=sin
Z= exp(3*t+5)

Print X,Y and Z

Stop

Source Code

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,y,z,t;
clrscr();
printf("Enter the value of t :");
scanf("%f",&t);
x=8*pow(t,2)-t*t*t+4;

21 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

y=sin(3.1415*30/180);
z=exp(3.0*t +5.0);
printf("\nx = %.3f, y = %.3f, z= %.3f", x,y,z);
getch();
}

Output

Enter the value of t : 2


x = 34.000, y = 0.5000, z = 2270.811

Ex no: 2e Area of a triangle

Aim: To write a C program to find the area of a triangle given the length of three sides

Algorithm:
Step 1: Start the program.
Step 2: Read the side lengths a, b and c
Step 3 : Calculate area using the formula
area =
where s = (a + b + c)/ 2
Step 4: Print the area
Step 5: Stop the program

22 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Flow Chart

Start

Read a,b,c

s=(a+b+c)/2

area =

Print area

Stop

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c, s, area;
clrscr();
printf("\nEnter the length of the three sides of the triangle:");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;

23 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of the Triangle :%.3f sq.meters",area);
getch();
}

Output

Enter the length of the three sides of the triangle:


12 16 20
Area of the Triangle : 96.000

Ex no: 2f Find the slope and midpoint of a given line segment

Aim: To write a C program to find the slope and midpoint of a given line segment.

Algorithm:

Step 1: Start the program.


Step 2: Read the endpoints of the line
Step 3 : Calculate slope and midpoint using the formula
Slope=

Midpoint=

24 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 4: Print the Slope and Midpoint


Step 5: Stop the program

Flow Chart

Start

Read the
endpoints (x1,y1)
and (x2,y2)

Slope=
Midpoint=

Print area

Stop

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
float x1,x2,y1,y2,slope,midX,midY;
clrscr();
printf("\nEnter the endpoints of a line");
printf("\nEndpoint 1 (x1,y1):");
scanf("%f%f",&x1,&y1);
printf("\nEndpoint 2 (x2,y2):");
scanf("%f%f",&x2,&y2);
slope=(float)(y2-y1)/(float)(x2-x1);

25 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

midX=(x1+x2)/2;
midY=(y1+y2)/2;
printf("\nSlope : %.2f",slope);
printf("\nMidpoint : (%.2f,%.2f)",midX,midY);
getch();
}

Output

Enter the endpoints of a line


Endpoint 1 (x1,y1): 1 1
Endpoint 2 (x2,y2): 3 3
Slope : 1.00
Midpoint : (2.00,2.00)

Ex.no.3 Scientific problem solving using decision making and looping.

Ex no.3a Fibonacci series

Aim: To write a C program print the Fibonacci series of n

Algorithm

Step 1: Start the program


Step 2: Read n
Step 3: Set first =0, second=1
Step 4: Loop from i=1 to i=n do
next first + second

26 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

first second
second next
Step 5: Print next in each iteration
Step 6: Stop the program

Flowchart

start

Read n

Initialize i=0

no
27 R. M. K COLLEGE OF ENGINEERING &T
ECHNOLOGY If
i<n
-COMPUTER
PRACTICE LABORATORY-I

yes

yes
If
i<=
1
no

next=i
next = first + second;
first = second;
second = next

i=i +1

Print next

stop

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are \n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{

28 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

next = first + second;


first = second;
second = next;
}
printf("%d\n",next);
}
}
Output
Enter the number of terms
5
First 5 terms of Fibonacci series are
0
1
1
2

Ex no: 3b Conversion Of Binary To Decimal

Aim: To write a program to convert the binary number to a decimal number.

Algorithm:

Step 1 : Start the program.


Step 2 : Enter the binary value.
Step 3 : Set a loop
Step 4 : Convert the binary number into decimal by using statement given:
digit binary number % 10
decimal decimal + (digit< < base)
base base + 1
binary number binary number/10
Step 5 : After the e4xecutioon of the loop print the decimal value equivalent to the entered
binary number.
29 R. M. K COLLEGE OF ENGINEERING &T
ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 6 : Stop

FlowChart:

30 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Start

Read bnum

Binbnum

Is
bnum=
0

bnum
Digital bnum % 10

Decimaldecimal+(digital< base)

Bnum bnum/10

Print
bin,decimal

Stop

Source Code:

31 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

#include<stdio.h.
main()
{
int bnum,digit,decimal=0,bin,base=0;
printf(\nEnter the binary number);
scanf(%d,&bnum);
printf(%d,bnum);
bin=bnum;
while(bnum!=0)
{
digit=bnum%10;
decimal=decimal+(digit< < base);
base=base+1;
bnum=bnum/10;
}
printf(\n The binary equivalent of %d in decimal = %d,bin,decimal);
}

Output:
Enter the binary number 100
The binary equivalent of 100 in decimal =4

Ex No:3c Sine And Cosine Series

32 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Aim: To write a program to print sine and cosine series

Alogrithm:

Step 1 : Start the program.


Step 2 :Read x and n
Step 3:Conert x values into radian using formula x=x*3.1412/180
Step 4:t x;
Step 5:sum=x;
Step 6:setup for loop i=1 until (i<n+1) increment 1
Step 7:t (t*(pow(double)(-1),(double(2*i-1))*x*x)/(2*i*(2*i+1)
Step 8: t1 (t1*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i-1));
Step 9:sum sum+t;
Step 10:print sum
Step 11:stop

Flow Chart:

33 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

start

Read
x,n

x=x*3.1412/180

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

T=(t*(pow(double)(1),(double

(2*i-1))*x*x)/(2*i*(2*i+1)

T1=(t1*pow((double)(1),
(double)

(2*i-1))*x*x)/(2*i*(2*i-1));

Print sum

stop

Source Code:
#include<stdio.h>

34 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

#include<conio.h>
#include<math.h>
void main()
{
float x,t,sum,t1,sum1;
int i,n;
clrscr();
printf("Enter the value of x,n : ");
scanf("%f%d",&x,&n);
printf("%4.2f %d\n",x,n);
x=x*3.1412/180;
t=x;
sum=x;
t1=x;
sum1=x;
for(i=1;i<n;i++)
{
t=(t*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
for(i=1;i<n;i++)
{
t1=(t1*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i-1));
sum1=sum1+t1;
}
printf("The value of SINE(%f)=%6.4f\n",x,sum);
printf("the value of COSINE(%f)=%6.4f\n",x,sum1);
getch();
}

Output

Enter the value of x,n : 45 10


45.00 10
The value of SINE(0.785300)= 0.7070
the value of COSINE(0.785300)= 0.5553*/

ExNo:3d Program To Find The Exponent Of The Given Number

35 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Aim: To write a program to find the exponent of the given number.

Algorithm:

Step1: Start
Step 2: Enter the X and n Value.
Step 3: Set a loop upto n
Step 4: Find the exponent value of x
temp= temp* x / i
Sum= sum+ temp
Step 5: After the execution of the loop print the exponent value of x
Step 6: Stop
Flowchart

Start

read x,no

i=1

is Print
i<no Sum

i=i+
1
temp=temp * x /i Stop
Sum = sum + temp

Source Code:

36 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

// Exponential Series
#include<stdio.h>
main()
[
float x,temp=1,sum=1;
int i,no;
printf(Enter the numbers);
scanf(%f%d,&x,&no);
for(i=1;i<no;i++)
{
temp=temp * x / I;
sum=sum + temp;
}
printf(Exponent of x is %f\n,sum);
}

Output:
Enter the numbers
5
Exponent of x is 65.375000

Ex No:3e Triangle Of Numbers

37 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Aim: To write a program to print triangle of numbers

Algorithm:

Step 1:Start the program


Step 2:Declare the variables I,j,k,l and n as int datatype
Step 3:Read N numbers of lines in a triangle
Step 4:Initialize l=1;
Step 5: for i=1,j=n-I to less than or equal to n
Step 5.1: for k=1, to less than or equal to j print
Step 5.2: for k=1, to less than or equal to 1 print i
Step 5.3: I is incremented by 2
Step 6:Stop the program

Flow Chart:

38 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

start

Read N no of
triangle

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

for(k=1;k<=l;k++)

Print

for(k=1;k<=l;k++)

Print i

Increment I by2

stop

Source Code:

39 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

#include<stdio.h>
main()
{
int i,j,k,l,n;
printf(type n-no of lines in triangle \n);
scanf(%d,&n);
l=1;
for(i=1;j=n-i;i<=n,i++,j--)
{
for(k=1;k<=j;k++)
printf( );
for(k=1;k<=l;k++)
printf( %d,i);
printf((\n);
l+=2;
}
}
OUTPUT
Type N-no of lines in triangle
4
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4

Ex no.3f Illustrate menu-driven Programming

40 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Aim : To write a C program to illustrate menu-driven Programming.

Algorithm:

Step 1: Start the program

Step 2: Read choice and num

Step 3: Repeat till a valid choice

Step 3a: if choice is 1

Check if num is Armstrong or not

Step 3b: If choice is 2

Check if num is Palindrome or not

Step 3c: If choice is 3

Check if num is Prime or not

Step 4: Stop the prooram

Flowchart:

star
t

Read
choice,num
41 R. M. K COLLEGE OF ENGINEERING &T
ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Original = num

yes
If(ch=
=1)

Compute Armstrong of
num
NO

If(ch= yes
=2)
Compute Palindrome of
num

NO

yes

If(ch=
=3)
Compute primality of
NO num

stop

Program

42 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

#include<stdio.h>
#include<conio.h>
void main()
{
int choice,num,original,ans,r,i,flag;
clrscr();
do
{
printf("\n MENU");
printf("\n 1.Check Armstrong or not");
printf("\n 2.Check Palindrome or not");
printf("\n 3.Check Prime or not");
printf("\n 4.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
printf("\nEnter the number:");
scanf("%d",&num);
original=num;
switch(choice)
{
case 1:
ans=0;
while(num>0)
{
r=num%10;
ans=ans+r*r*r;
num=num/10;
}
if(ans==original)
printf("%d is an armstrong number",original);
else
printf("%d is not an armstrong number",original);
break;
case 2:
ans=0;
while(num>0)
{
r=num%10;
ans=ans*10+r;
num=num/10;
}
if(ans==original)
printf("%d is palindrome",original);
else
printf("%d is not palindrome",original);
break;
case 3:
flag=0;
for(i=2;i<num/2;i++)
{

43 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

if(num%i==0)
{
flag=1;
break;
}
else
continue;
}
if(flag==1)
printf("%d is not a prime number",num);
else
printf("%d is a prime number",num);
break;
default:
exit(0);
}
}while(1);
}

Output

MENU

Check Armstrong or not


2.Check Palindrome or not
3.Check Prime or not
4.Exit"
Enter your choice:1
Enter the number: 153
153 is an Armstrong number

Enter your choice: 2


Enter the number: 12321
12321 is a Palindrome

Enter your choice: 3


Enter the number: 43
43 is a prime number

Ex no.3g Find the roots of a given Quadratic Equation

Aim : To find the roots of a given Quadratic Equation

Algorithm:

Step 1: Start the program

Step 2: Read coefficients a, b and c

44 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 3: Compute determinant d =b2-4ac

Step 4: If determinant > 0

Compute root1=(b+d )/2a and root2=(b--d)/2a

If determinant = 0

Compute root1 = root2 =b/2a

If determinant<0

Compute real = -b/2a and img = d / 2a

Step 5: Print the roots root1 and root2

Step 6: Stop the program

Flowchart:

start

Read
a,b, c

d =b2-4ac

45 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
r= -b/(2*a)Print r+is, r-is IfIf
s = sqrt(d)/(2*a) d==
d>0 r1= (-b+sqrt(determinant))/
Print
0 (2*a) r1,r2
-COMPUTER
PRACTICE LABORATORY-I

yes

NO

No yes

r1 = r2 = -b/
(2*a)

Print
r1,r2

stop
Program

#include <stdio.h>
#include <math.h>
void main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);

46 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

printf("Roots are: %.2f and %.2f",r1 , r2);


}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
getch();
}

Output

Enter coefficients a, b and c: 1 2 1


Roots are: -1.00 and -1.00

EX.NO.4 Simple programming for one dimensional and two dimensional arrays.
Ex. No: 4a linear search

Aim: To write a C program to perform linear search on an array of n


elements

Algorithm

Step 1: Start the program

Step 2: Read the array a

Step 3: Set flag 0

Step 4: Read element to be searched

47 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 5: Repeat for i 0 to size of array

If a[i] = element, set flag 1

Break

If a[i] ! =element, continue

Step 6: If flag = 0, print element not found

Else, print element position

Step 7: Stop the program

Program

#include<stdio.h>

#include<conio.h>

void main()

int a[10],item,size,pos,flag,i;

clrscr();

flag=1;

printf("\nEnter the size of array:");

scanf("%d",&size);

printf("\nEnter the elements:");

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

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

printf("\nEnter the element to be searched:");

scanf("%d",&item);

for(i=0;i<size;i++){

if(item==a[i])

pos=i;

48 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

flag=1;

break;

else

continue;

if(flag==1)

printf("%d is at position %d",item,pos+1);

else

printf("%d not found",item);

getch();

Output

Enter the size of array: 10 30 20 15 11

Enter the elements to be searched: 20

20 is at position 3

Ex. No : 4b Binary search

Aim: To write a C program to perform binary search on an array of n elements

Algorithm:

Step 1: Start the program

Step 2: Read the array a in ascending order

Step 3: Set flag 0, low 0 and high sizeOfArray-1

Step 4: Read element to be searched

49 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Step 5: Repeat till low<=high

Step 5a: mid (low+high)/2

Step 5b: if item = a[mid], set flag 1

Break

Step 5c: if item < a [mid], set high mid 1

Step 5 d: If item > a[mid], set low mid + 1

Step 6: If flag =1, print element is found at position mid+1

Otherwise, print element not found

Step 7: Stop the program

Program:

#include<stdio.h>

#include<conio.h>

void main(){

int a[10],i,n,item,flag=0,low,high,mid;

clrscr();

printf("Enter the size of an array: ");

scanf("%d",&n);

printf("Enter the elements in ascending order: ");

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

50 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

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

printf("Enter the number to be search: ");

scanf("%d",&item);

low=0,high=n-1;

while(low<=high){

mid=(low+high)/2;

if(item==a[mid]){

flag=1;

break;

else if(item<a[mid]){

high=mid-1;

else

low=mid+1;

if(flag==0)

printf("%d is not found",item);

else

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

getch();

Output

Enter the size of array: 5

51 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Enter the elements in ascending order: 10 20 30 40 50

Enter the element to be searched: 40

40 is at position 4

Ex. No: 4c Sorting

Aim: To write a C program to sort an array of n integers

Algorithm

Step 1: Start the program

Step 2: Read the array a

Step 3: Repeat for i 0 to ArraySize

Repeat for j i to ArraySize

If a[i] > a[j], swap a[i] and a[j]

Step 4: Print the sorted array a

Step 5: Stop the program

Program

#include<stdio.h>

52 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

#include<conio.h>

void main()

int a[10],temp,i,j,size;

clrscr();

printf("\nEnter the array size");

scanf("%d",&size);

printf("\nEnter the array to be sorted:");

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

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

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

for(j=i+1;j<size;j++)

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

printf("\nSorted Array");

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

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

getch();

Output

Enter the array size: 5

Enter the array to be sorted: 4 3 1 6 2

53 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Sorted Array 1 2 3 4 5

Ex. No : 4d Sum of two matrices

Aim: To write a C program to find the sum of two matrices

Algorithm

Step 1: Start the program

Step 2: Read the matrix size : rows and columns

Step 3: Read the two matrices a and b of size rows x columns each

Step 4: Add the two matrices element-by-element and store in matrix c

Step 5: Print matrix c

Step 6: Stop the program

Program

#include<stdio.h>

#include<conio.h>

void main()

54 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

int a[3][3],b[3][3],c[3][3],i,j,row,col;

clrscr();

printf("\nEnter the row and column size");

scanf("%d%d",&row,&col);

printf("\nEnter the matrix1 :");

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

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

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

printf("\nEnter the matrix2 :");

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

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

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

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

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

c[i][j]=a[i][j]+b[i][j];

printf(\nThe sum is\n);

for(i=0;i<row;i++){

for(j=0;j<col;j++){

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

printf("\n");

getch();

Output

Enter the row and column size: 2 2

55 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Enter the matrix 1:

2 3

4 5

Enter the matrix 2:

1 5

2 9

The sum is

3 8

6 14

Ex. No: 4e Product of two matrices

Aim: To write a C program to find the product of two matrices

Algorithm

Step 1: Start the program

Step 2: Read the size of two matrices : row1,col1, row2 and col2

Step 3: If col2 ! = row2,

print matrix multiplication is not possible

Exit the program

Step 4: Read the two matrices a and b

Step 4: Multiply the two matrices and store in matrix c

Step 5: Print matrix c

Step 6: Stop the program

Program

#include<stdio.h>

#include<conio.h>

void main()

56 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

int a[3][3],b[3][3],c[3][3],i,j,k,row1,col1,row2,col2;

clrscr();

printf("\nEnter the row and column size of matrix 1");

scanf("%d%d",&row1,&col1);

printf("\nEnter the row and column size of matrix 2");

scanf("%d%d",&row2,&col2);

if(col1!=row2){

printf("Multiplication is not possible");

getch();

exit(0);}

printf("\nEnter the matrix1 :");

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

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

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

printf("\nEnter the matrix2 :");

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

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

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

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

for(j=0;j<col2;j++){

c[i][j]=0;

for(k=0;k<col1;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

printf(\nThe product matrix is:\n);

for(i=0;i<row1;i++){

57 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

for(j=0;j<col2;j++){

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

printf("\n");

getch();

Output

Enter the row and column size of matrix 1: 2 3

Enter the row and column size of matrix 1: 3 2

Enter the matrix 1:

1 2

1 0

3 3

Enter the matrix 2:

1 1 2

2 0 -1

The product matrix is

5 1 0

1 1 1

9 3 3

58 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex. No: 4f Transpose of a given matrix

Aim: To write a C program to find the transpose of a given matrix

Algorithm

Step 1: Start the program

Step 2: Read the matrix A of rowA x colB size

Step 3: Find the transpose of A

Let matrix B be of size rowB=colA and colB=rowA

For i0 to rowB do

For j 0 to colB do

B[i][j] <- A[j][i]

Step 4: Print matrix B

Step 6: Stop the program

Program

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3],b[3][3],i,j,row,col,trow,tcol;

clrscr();

59 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

printf("\nEnter the row and column size");

scanf("%d%d",&row,&col);

printf("\nEnter the matrix :");

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

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

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

trow=col;

tcol=row;

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

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

b[i][j]=a[j][i];

printf("The transpose of matrix A is\n");

for(i=0;i<trow;i++){

for(j=0;j<tcol;j++){

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

printf("\n");

getch();

Output

Enter the row and column size:

2 3

Enter the matrix A:

1 2 3

4 5 6

60 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

The Transpose of Matrix A is

1 2

3 4

5 6

Ex.No: 5 solving problems using String functions


Aim: To write a C program to illustrate any five string functions

Algorithm

Step 1: Start the program

Step 2: Read the string str1 and choice

Step 3: If choice is 1

Print length of str1 using strlen()

If choice is 2

Print reverse of str1 using strrev()

If choice is 3

Get another string str2

Compare str1 and str2 using strcmp()

If choice is 4

Copy the first half of str1 to another str2 using strcpy()

Print str2

If choice is 5

Get another string str2

Concatenate str1 and str2 using strcat()

Step 4: Stop the program

Program

#include<stdio.h>

#include<conio.h>

61 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

void main()

char str1[10],str2[10],original[10];

int choice,half;

clrscr();

printf("\nEnter the string:");

scanf("%s",&str1);

strcpy(original,str1);

while(1)

strcpy(str1,original);

printf("\nMENU");

printf("\n1. String Length");

printf("\n2. String Reverse");

printf("\n3. String Compare");

printf("\n4. String Copy");

printf("\n5. String Concate");

printf("\n6. Exit");

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Length of %s is %d ",str1,strlen(str1));

break;

case 2:

62 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

printf("Reverse of %s is %s",original,strrev(str1));

break;

case 3:

printf("\nEnter another string for comparison");

scanf("%s",&str2);

if(strcmp(str1,str2)==0)

printf("Strings are equal");

else

printf("Strings are not same");

break;

case 4:

half=strlen(str1)/2;

strncpy(str2,str1,half);

str2[half]='\0';

printf("The first half of %s is %s",str1,str2);

break;

case 5:

printf("\nEnter the string");

scanf("%s",str2);

strcat(str1,str2);

printf("After Concatenation:%s",str1);

break;

default:

exit(0);

63 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Output

Enter the string: Hello

MENU

1. String Length

2. String Reverse

3. String Compare

4. String Copy

5. String Concate

6. Exit

Enter your choice: 1

Length of Hello is 5

Enter your choice: 2

Reverse of Hello is olleH

Enter your choice: 3

Enter another string for comparison: Hell

Strings are not same

Enter your choice: 4

The first half of Hello is He

Enter your choice: 5

Enter the string: Guru

After Concatenation: Hello Guru

64 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex.no:6 Programs with user defined functions Includes Parameter Passing


Ex No:6a FUNCTIONS WITH ARGUMENTS AND WITH RETURN VALUES

Aim: To write a program to illustrate a function with arguments and with return values.
Algorithm:
Step 1: Start the program
Step 2: Enter the two numbers
Step 3: Call the function with two arguments passed to it
Step 4: Find GCD of two numbers in the calling function
Step 5:Return answer to the called function from the calling function.
Step 6: Print the gcd value in the main function
Step 7; Stop
Source Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int ans,a,b;
clrscr();
printf("\nEnter value of a and b");
scanf("%d%d",&a,&b);
ans=gcd(a,b);
printf("\nGCD of %d, %d is %d",a,b,ans);
getch();
}

int gcd(int x, int y)


{
int t;
while(x!=y){
if(x>y)
x=x-y;
else if(x<y)
y=y-x;
}
return x;
}
Output:
Enter value of a and b 39 130
GCD of 39 and 130 is 13

65 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex No: 6b Swapping Two Numbers using call by value

Aim: To write a C program to swap two numbers using call by value

Algorithm
Step 1 : Start the program
Step 2 : Set a 10 and b 20
Step 3 : Call the function swap(a,b)
Step 3a: Start fuction
Step 3b: Assign t x
Step 3c: Assign x y
Step 3d: Assign y t
Step 3e: Print x and y
Step 3f: End function
Step 4: Stop the program

Program
#include<stdio.h>
#include<conio.h>
void swap(int , int); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
printf(\nBefore swapping);
printf ( "\na = %d b = %d", a, b ) ;
swap(a,b);// call by value : a and b are actual parameters
getch();
}
void swap( int x, int y ) // x and y are formal parameters
{
int t ;
t=x;
x=y;
y=t;
printf(\nAfter swapping);
printf ( "\nx = %d y = %d", x, y ) ;
}
Output
Before swapping
a = 10 b=20
After swapping
x = 20 y = 10

66 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex No: 6c Swap two numbers using call by reference

Aim: To write a C program to swap two numbers using call by reference

Algorithm:

Step 1 : Start the program


Step 2 : Set a 10 and b 20
Step 3 : Call the function swap(&a,&b)
Step 3a: Start fuction
Step 3b: Assign t *x
Step 3c: Assign *x *y
Step 3d: Assign *y t
Step 3e: End function
Step 4: Print x and y
Step 5: Stop the program

Program

#include<stdio.h>
#include<conio.h>
void swap(int* , int*); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
printf("\nBefore swapping\n");
printf ( "\na = %d b = %d", a, b ) ;
swap(&a,&b);// call by reference
printf("\nAfter swapping\n");
printf ( "\na = %d b = %d", a, b ) ;
getch();
}
void swap( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;

67 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

EX.NO:7 Program using Recursive Function and conversion from given


program to flow

Ex No: 7a Recursion Using Function

Aim: To write a program to find the factorial of the given number using recursion.

Algorithm:

Step 1: Start
Step 2: read num
Step 3: afact(num)
Step 4: print a
Step 5: Stop
Step 1: fact start
Step 2: Is no=1
T : return
F : return n * fact(n-1)
Program

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a;
printf("\nEnter the number");
scanf("%d",&num);
a=fact(num);
printf("\nThe factorial of the number %d is %d",num,a);
getch();
}
int fact(int n)
{
int f;
if(n==1)
return(1);
else
return n * fact(n-1);
}
Output
Enter the number 5
The factorial of the number 5 is 120

68 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex No: 7b Fibonacci number Using Recursion


Aim: To write a program to find the ith Fibonacci number, given F(0)=0, F(1)=1
Algorithm
Step 1: Start the program
Step 2: read num
Step 3: ffibo(num)
Step 4: print f
Step 5: Stop
Step 1:fibo start
Step 2:If n<=1
Return 1
Else
Return fibo(n-1)+fibo(n-2)
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num,a;
printf("\nEnter the number");
scanf("%d",&num);
a=fibo(num);
printf("\nThe fibonacci of the number %d is %d",num,a);
getch();
}
int fibo(int n)
{
if(n==0)
return(0);
if(n==1)
return(1);
else
return fibo(n-1)+fibo(n-2);
}
Result

Enter the number 8

The Fibonacci of the number 8 is 21

69 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex.no:8 Program using structures and unions.

Ex No:8a Student Detail using structure

Aim: To write a program to assigning values to the structures variables and retrieving values.
Algorithm:
Step 1: Start the program
Step 2: Struct Std
Rno, Marks : integer
Name[10] :character
End
Step 3: Read S.rno, S.name,S.marks
Step 4: Print S.no,S.name,S.marks
Step 5: Stop
Source Code:

#include<stdio.h>

#include<conio.h>
struct std
{
int rno,marks;
char name[10];
}s;
void main()
{
clrscr();
printf(Enter Student Details:\n);
printf(Enter the Register No,Name and Mark\n);
scanf(%d%s%d,&s.rno,s.name,&s.marks);
printf(\n);
printf(Rollno\t\t Name\t\tMarks\n);
printf(%d\t\t%s\t\t%d,s.rno,s.name,s.marks);
getch();
}
Output:
Enter the student Details:
Enter the regNo, Name,Marks:
111 Ram 98
RollNo Name Marks
111 Ram 98

70 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex No:8b Book Details using structure

Aim To write a C program to read and display the details of n books using structure
Algorithm
Step 1: Start
Step 2: Declare a structure with variables : name,author,price and Quantity
Step 3: Read n number of books
for i1 to n
Read book[i].name, book[i].author, book[i].price and book[i].Quantity
Step 4: Display the details of n books
Step 5: Stop

#include<stdio.h>
#include<conio.h>
struct book
{
char name[25];
char author[25];
double price;
int Quantity;
}b[10];
void main()
{
int n,i;
printf("\nEnter the number of books\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter details of Book %d[Name,Author,Price,Quantity] ",i+1);
scanf("%s%s%ld%d",b[i].name,b[i].author,&b[i].price,&b[i].Quantity);
}
clrscr();
printf("\n-----------Book Details------------");
printf("\nBook Id\tName\tAuthor\Price\Quantity\n");
for(i=0;i<n;i++)
{
printf("\nBook %d \t%s\t%s\t%ld\t%d",i+1,b[i].name,b[i].author,b[i].price,b[i].Quantity);

}
getch();

71 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

}
Output
Enter the number of books
2
Enter details of Book 1 [Name,Author,Price,Quantity]
Computer
Babu
250.00
6
Enter details of Book 2 [Name,Author,Price,Quantity]
C++
Bala
450.00
15
------------Book Details--------------
Book Id Name Author Price Quantity
1 Computer Babu 250.0000 6
2 C++ Bala 450.0000 15

72 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Ex No:8C Implementation of Union

Aim To write a C program to illustrate union

Algorithm
Step 1: Start
Step 2: Declare union with variables: metre & cm
Step 3: Check if choice is m
Read u1.metre and u2.metre value
Print sum : u1.metre + u2.metre
Step 4: Check if choice is c
Read u1.cm and u2.cm value
Print sum : u1.cm + u2.cm
Step 5: Stop the program

Algorithm
#include<stdio.h>
#include<conio.h>
union distance
{
float metre;
int cm;
}u1,u2;
void main()
{
int choice;
printf(\nThe size of union is:%d bytes,sizeof(u1));
printf("\nSelect your distance metric m/c?");
scanf("%c",&choice);
if(choice=='m'){
printf("\nEnter the distance:");
scanf("%f%f",&u1.metre,&u2.metre);
printf("Sum of distances:%f metres",u1.metre+u2.metre);
}
else
{
printf("\nEnter the distance:");
scanf("%d%d",&u1.cm,&u2.cm);
printf("Sum of distances:%d cm",u1.cm+u2.cm);
}
getch();
}
Output
The size of union is 4 bytes
Select your distance metric m/c?
c

73 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY
-COMPUTER
PRACTICE LABORATORY-I

Enter the distance: 20 25


Sum of distances: 45 cm

74 R. M. K COLLEGE OF ENGINEERING &T


ECHNOLOGY

Potrebbero piacerti anche