Vous êtes sur la page 1sur 89

Global Institute of Engineering & Technology

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output

GLOBAL INSTITUTE
OF ENGINEERING & TECHNOLOGY
Melvisharam, Vellore

Courses
Offered

SALIENT FEATURES
Highly Qualified staffs
Well equipped labs

ECE

24 hrs internet facility

CSE

Good transport facility

EEE
Mech

Separate hostel for Boys &


Girls

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 01
Date :

MS WORD
Aim:
To create a Microsoft word document and perform the various text manipulation
operations.

Procedure:
1. Create a new word document using File-> New option, and justify the text by
using the justify button on the formatting tool bar.
2. For Bold, Italic and Underline press the CTRL+B, CTRL+I, CTRL+U
respectively.
3. For Left, Right and Center alignment press the CTRL+C, CTRL+R, CTRL+E
respectively.
4. Select the text were we want to add the Bullets and Numberings. Then select the
Bullets and Numbering option from the Formatting tool bar.
5. For line spacing select the text and click the Paragraph option from the Format
menu.
6. Select the 1.5 lines from the line spacing list box, and then click OK.
7. If you want to change the text style and size, select the text which you want to
change and check the Font option from Format menu.
8. Choose the required font type from the font drop down list box and the size from
the size list box. Then click OK.
9. To insert the Symbols put the cursor where you want to insert the symbol. Then
select the symbol from the Insert menu.
10. Then choose required symbol from the symbol dialog box, then click Insert.

Result:
Thus the word document is created and all the text manipulation operations are
tested successfully.

Global Institute of Engineering & Technology

Computer practice lab -1

Table
Name

Dhamu

Mark 1
60

Mark 2
55

Mark 3
50

Total
165

Average
55

Kumar

40

60

50

150

50

Priya

90

75

81

246

82

Raj

90

75

81

246

82

Ram

80

85

72

237

79

Ramya

80

65

74

219

73

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 02
Date :

TABLE CREATION
Aim:
To create a table in Microsoft Word and perform the various table formatting
operations.

Procedure:
1. Choose the Table option from the menu bar, and then choose the Insert -> Table
option from the table option.
2. Choose the required number of rows and columns, and then click OK.
3. Now the table is inserted in your document, enter the text in the columns.
4. After adding the text, place the cursor in the table and click Table -> Sort.
5. It shows the Sort Dialog Box, from that select the field which you want to sort.
Select the Ascending from the drop down list and click OK.
6. For calculating the sum, place the cursor in the appropriate and click Table ->
Formula, it will display a formula dialog box with=.
7. Click OK, now the sum is displayed in the column. Do the same for remaining
columns also.
8. If you want to add a row in your table means, place the cursor at the last row and
click Table -> Insert -> Rows this will creates the Blank row.
9. Like this we can insert the rows above are below to the current row.
10. For columns also we can follow the same as the rows.
11. If you want to give the various formats to your table, then select Table Auto
Format in the Table menu.
12. It shows the table auto Format Dialog Box, choose the required format then click
OK.

Result:
Thus the table is created and formatted in the Microsoft Word Document.

Global Institute of Engineering & Technology

Computer practice lab -1

Table
Name

Dham
u
Kuma
r
Priya
Raj
Ram
Ramy
a

Mark 1

Mark 2

Mark 3

Total

Average

60

55

50

165

55

THIRD

40

60

50

150

50

FAIL

90

75

81

246

82

First

90

75

81

246

82

First

80

85

72

237

79

First

80

65

74

219

73

SECOND

Chart

Picture

Class

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 03
Date :

SPREAD SHEET
Aim:
To create a Spread sheet with various fields of datas and charts. And also insert
the objects and pictures in the sheet.

Procedure:
1. Enter the name and three subjects names in the first row of the Spreadsheet.
2. Enter the marks of the each student.
3. Place the cursor in the column Sum and type = SUM(B2..D2).
4. Place the cursor in the column Average and type = AVERAGE(B2D2).
5. Place the cursor in the column Class and type the following Formula to find the
class of the student.
=IF(OR(B5<50,C5<50,E5<50),"FAIL",IF(F5>75,"FIRST",IF(F5>60,"SECO
ND","THIRD")))
6. To create the chart for the student marks, first select the name and marks column.
Then click the Chart Wizard from the Formatting Toolbar.
7. It shows the Chart Wizard Step 1 of 4. Choose the chart type and click Next.
8. It shows the Chart Wizard step 2 pf 4. Chart Source Data dialog Box, choose
the chart source and click on Next.
9. It shows the Chart Wizard- step 3 of 4, Chart Option Dialog box, choose the
Chart Title, X- Axis and Y- Axis labels and click on Next.
10. It shows the Chart Wizard- step 4 of 4, choose the Chart Location and click on
Next. Then click Finish.
11. Now the chart is shown in the sheet.
12. If you want to insert the picture select the Picture option from the Insert menu. If
opens a sub menu from that select the Clip Art.
13. Clip Art window will open. You can select any clip art from the list and insert in
your sheet by clicking Insert.

Result:
7

Global Institute of Engineering & Technology

Computer practice lab -1

Thus the Spreadsheet is created with various fields and operations, and also
various types of charts and pictures are inserted

Global Institute of Engineering & Technology

Computer practice lab -1

Global Institute of Engineering & Technology

Flow Chart

Start

Read A,B;

C = A + B;

Print C

C = A - B;

Print C

C = A B;

Print C

C = A / B;

Print C

Stop
10

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex no : 04
Date

ARITHMETIC OPERATIONS
Aim:
Write a C program to perform all the basic arithmetic operations.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.

Start the program.


Declare the integer type variables A, B and C.
Read the values for the variables A and B.
Find the sum of A and B and store it in C. ie C = A + B.
Print the C value.
Find the difference between A and B, ie C = A B, print the C value
Find the product of A and B by the expression C = A * B, then print the C value.
Divide the A by B and store the quotient in C as C = A / B, then print the C value.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int A,B,C;
clrscr();
printf("\n Enter the value for A
scanf("%d",&A);
printf("\n Enter the value for B
scanf("%d",&B);
C=A+B;
printf("\n Sum of A and B is
C=A-B;
printf("\n Difference of A and B is
C=A*B;
printf("\n Product of A and B is
C=A/B;
printf("\n Quotient of A by B is
getch();
}

: ");
: ");
: %d",C);
: %d",C);
: %d",C);
: %d",C);

11

Global Institute of Engineering & Technology

12

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the value of A : 10
Enter the value of B : 5
Sum of A and B is
Difference of A and B is
Product of A and B is
Quotient of A by B is

: 15
:5
: 50
:2

Result:
Thus the C program to perform the basic arithmetic operations is written and
executed successfully.

13

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
Start

Read a, b,
n;

No

Switc
h
(n)
Yes
Yes

No

Ca
se
1

C=a+b;

Print
c;

C=a+b;

Print
c;

C=a+b;

Print
c;

C=a+b;

Print
c;

Yes

Ca
se
2
No

Ca
se
3

Yes

No
Yes

Ca
se
4
No

Ca
se
0

Yes

exit ( );

No

Print default statement

Stop

14

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 05
Date :

MENU DRIVEN CALCULATOR


Aim:
Write a simple menu driven calculator program by using switch statement.

Algorithm:
1.
2.
3.
4.

Start the program


Read the values for the variables A and B.
Read the choice which you want.
match the choice value with the case constant,
4.1 Case = 1
C = A+ B
Print C
Case = 2
C = A B
Print C
Case = 3
C = A* B
Print C
Case = 4
C = A/ B
Print C
Case = 0
Exit
5. If all the cases are mismatched means, then execute the default statement.
6. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
clrscr();
printf("\n OUTPUT ");
printf("\n ~~~~~~ ");
printf("\n Enter the values for A and B : ");
scanf("%d%d",&a,&b);
printf("\n 1.Addition");
printf("\n 2.Subtraction");
printf("\n 3.Multiplication");
printf("\n 4.Division");
printf("\n 0.Exit");
15

Global Institute of Engineering & Technology

16

Computer practice lab -1

Global Institute of Engineering & Technology

printf("\n Enter your choice : ");


scanf("%d",&n);
switch(n)
{
case 1:
c=a+b;
printf("\n The sum is : %d ",c);
break;
case 2:
c=a-b;
printf("\n The difference is : %d ",c);
break;
case 3:
c=a*b;
printf("\n The product is : %d ",c);
break;
case 4:
c=a/b;
printf("\n The quotient is : %d ",c);
break;
case 0:
exit();
default:
printf("\n Enter the correct choice.");
}
getch();
}

Output 1 :
Enter the values for A and B : 10 2
1.Addition
2.Subtraction
3.Multiplication
4.Division
0.Exit
Enter your choice : 1
The sum is : 12

Output 2 :
Enter the values for A and B : 10 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
0.Exit
17

Computer practice lab -1

Global Institute of Engineering & Technology

18

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Enter your choice : 2


The difference is : 5

Output 3 :
Enter the values for A and B : 10 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
0.Exit
Enter your choice : 3
The product is :50

Output 4 :
Enter the values for A and B : 10 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
0.Exit
Enter your choice : 4
The quotient is : 2

Output 5 :
Enter the values for A and B : 10 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
0.Exit
Enter your choice : 5
Enter the correct choice.

Result:
Thus the menu driven calculator is executed successfully.

19

Global Institute of Engineering & Technology

Flow Chart
Start

Read x,n;

No

while
(i<=n
)
Yes

Stop

y=x*i;

Print i,x,y;

i++;

20

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 06
Date :

MULTIPLICATION TABLE
Aim:
Write a C program to generate a multiplication table by using the while statement.

Algorithm:
1.
2.
3.
4.
5.

Start the program.


Read the table which you want to print.
Read the number of terms.
set the incremental variable i = 1;
while( i <= n)
{
y = y * i;
i++;
print y;
}
6. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y=1,i=1,n;
clrscr();
printf("\n Enter the table you want : ");
scanf("%d",&x);
printf("\n Enter the no number of terms : ");
scanf("%d",&n);
while(i<=n)
{
y=x*i;
printf("\n %d * %d = %d",i,x,y);
i++;
}
getch();
}

21

Global Institute of Engineering & Technology

22

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the table you want : 5
Enter the no number of terms : 12
1*5=5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
11 * 5 = 55
12 * 5 = 60

Result:
Thus the coding for generating the multiplication table is written and executed
successfully.

23

Global Institute of Engineering & Technology

Flow Chart

Start

Read X;

x=x*3.14/180;
t=x;
sum=x;

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

t=(t*pow((double)(-1),(double)(2*i1))*x*x)/(2*i*(2*i+1));
sum=sum+t;

Print sum;

Stop

24

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 07
Date :

SIN SERIES
Aim:
Write a C program to print the sin series

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.

Start the program.


Read the X value.
Convert the X value into radian value by x = x * 3.14 / 180.
Assign the X value to t and sum.
Calculate t value as t = ( t * (pow(double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1)
Add the t and sum values. ie, sum = sum + t.
Print sum.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,t,sum;
int i,n=10;
clrscr();
printf("\n Enter the value of X : ");
scanf("%f",&x);
printf("%4.2f %d\n",x,n);
x=x*3.14/180;
t=x;
sum=x;
for(i=1;i<n+1;i++)
{
t=(t*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf("\n The value of SIN(X) = %6.2f\n",sum);
getch();
}

Output:
Enter the value of X: 90
The value of SIN(X) = 1.00

Result:
Thus sin series program is written and executed successfully.
25

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
Start

Read n;

yes

no

if
(n ==
0)

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

Print 0;

f=f+a;
a=b;
b=f;

Print f;

Stop

26

Global Institute of Engineering & Technology

Ex No : 08
Date :

FIBONACCI SERIES
Aim:
Write a C program to generate the Fibonacci series.

Algorithm:
1.
2.
3.
4.

Start the program.


Read the number of terms you want to print.
If the number of term is Zero, then print 0.
else
for(i=0;i<n;i++)
{
f=f+a;
a=b;
b=f;
}
5. Print the f value in every incrimination.
6. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int f=0,n,a=0,b=1,i;
clrscr();
printf("\n Enter the no of terms : ");
scanf("%d",&n);
printf("\n Fibonacci sequence upto %d terms : ",n);
if(n==0)
{
printf("\n 0");
}
else
{
for(i=0;i<n;i++)
{
f=f+a;
a=b;
b=f;
printf(" %d",f);
}
}
getch();
}
27

Computer practice lab -1

Global Institute of Engineering & Technology

28

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the no of terms : 5
Fibonacci sequence upto 5 terms : 0 1 1 2 3

Result:
Thus the Fibonacci series is generated successfully by using the C coding.

29

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart

Start

Read n;

false

whil
(i<=
n)
true

print f;
f = f*i;
i++;
Stop

30

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 09
Date :

FACTORIAL
Aim:
Write a C program to find the factorial of the given number.

Algorithm:
1. Start the program.
2. Read the number, initialize i = 1 and f = 1.
3. while(i<=n)
{
f=f*i;
i++;
}
4. Print the f value.
5. Stop the Program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int f=1,n,i=1;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
while(i <= n)
{
f = f*i;
i++;
}
printf("\n Factorial of %d is %d",n,f);
getch();
}

Output:
Enter the number : 5
Factorial of 5 is 120

Result:
Thus the factorial of the given number is calculated successfully.

31

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
Start

Read a,b,c;

d=b*b-4*a*c;

Yes

No

if
(d==
0)

No

Print roots
are real &
equal

if
(d >
0)

Yes

r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);

Print roots
are imaginsry

Stop

32

Print roots
are real &
unequal
Print r1,r2;

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 10
Date :

QUADRATIC EQUATION
Aim:
Write a C program to find the roots of the given quadratic equation.

Algorithm:
1.
2.
3.
4.
5.

Start the program.


Read the values for the variables A, B and C.
Calculate the D value by d= b*b-4*a*c.
If D value is equal to zero means, print roots are real and equal.
If D value is greater then zero means find the two roots as
r1 = (-b + sqrt(d)) / (2*a);
r2 = (-b sqrt(d)) / (2*a);
print r1 and r2.
6. If D value is less then zero means print, roots are imaginary.
7. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("\n Enter the values of A,B and C : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d = = 0)
printf("\n The roots are real and equal");
else if(d > 0)
{
printf("\n The roots are real and unequal");
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("\n Root1 : %f",r1);
printf("\n Root2 : %f",r2);
}
else
{
printf("\n The roots are imaginary");
}
getch();
}

33

Global Institute of Engineering & Technology

34

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output 1:
Enter the values of A, B and C : 1 2 1
The roots are real and equal

Output 2:
Enter the values of A, B and C : 1 0 -9
The roots are real and unequal
Root1 : 3.0
Root2 : -3.0

Output 3:
Enter the values of A, B and C : 1 2 3
The roots are imaginary

Result:
Thus the roots of a quadratic equation are found successfully.

35

Global Institute of Engineering & Technology

Flow Chart

Start

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

Read a[i];

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

Print a[i];

Stop

36

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 11
Date :

ONE DIMENSIONAL ARRAY Number


Aim:
Write a C program to store any five numbers in an one dimensional array.

Algorithm:
1.
2.
3.
4.

Start the program.


Read any five numbers for the array a..
Store the five numbers in their locations by using for loop.
Display the numbers as follows,
for(i=0;i<5;i++)
{
Printf(%d,a[i]);
}
5. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
printf("\n Enter any five numbers : ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n The numbers in the array are : ");
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
getch();
}

Output:
Enter any five numbers : 1 2 3 4 5
The numbers in the array are: 1 2 3 4 5

Result:
Thus the one dimensional integer array concept is tested successfully.

37

Global Institute of Engineering & Technology

Flow Chart

Start

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

Read a[i];

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

Print a[i];

Stop

38

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 12
Date :

ONE DIMENSIONAL ARRAY String


Aim:
Write a C program to store any five numbers in an one dimensional array.

Algorithm:
6.
7.
8.
9.

Start the program.


Read any five characters for the array a..
Store the five numbers in their locations by using for loop.
Display the numbers as follows,
for(i=0;i<5;i++)
{
Printf(%c,a[i]);
}
10. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char a[10];
clrscr();
printf("\n Enter any five characters : ");
for(i=0;i<5;i++)
{
scanf("%c",&a[i]);
}
printf("\n The characters in the array are : ");
for(i=0;i<5;i++)
{
printf("%c",a[i]);
}
getch();
}

Output:
Enter any five characters : KUMAR
The characters in the array are: KUMAR

Result:
Thus the one dimensional character array concept is tested successfully.

39

Global Institute of Engineering & Technology

Flow Chart

Start

Read m,n,p,q;

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

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

Read a[i][j];

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

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

Read b[i][j];

40

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 13
Date :

MATRIX ADDITION
Aim:
Write a C program to add two matrices.

Algorithm:
1.
2.
3.
4.
5.
6.

7.
8.

Start the program.


Declare the matrix A,B and C with maximum number of elements in their rows
and columns.
Read the number of rows and columns.
Read the elements to the matrix A.
Read the elements to the matrix B.
Add the two matrices A and B as
for(i=0;i<m;i++)
{
for(j=0.j<n;j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
Print the C[i][j] matrix.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,m,n,p,q;
clrscr();
printf("\n Enter the no of rows and columns of matrix A : ");
scanf("%d%d",&m,&n);
printf("\n Enter the no of rows and columns of matrix B : ");
scanf("%d%d",&p,&q);
printf("\n Enter the elements of matrix A : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf(" \n Enter the elements of matrix B : ");
for(i=0;i<p;i++)
41

Global Institute of Engineering & Technology

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

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

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

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

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

print c[i][j];

Stop

42

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",c[i][j]);
}
}
getch();
}

Output:
Enter the no of rows and columns of matrix A : 2 2
Enter the no of rows and columns of matrix B : 2 2
Enter the elements of matrix A : 1 2 3 4
Enter the elements of matrix B : 1 2 3 4
2 4
6 8

Result:
Thus the matrices A and B are added successfully by using the above C program.

43

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
Start

Read m,n,p,q;

if((m==p
) &&
(n==q))

Yes

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

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

Read a[i][j];

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

Print rows
& columns
are not
equal

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

Read b[i][j];

44

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 14
Date :

MATRIX MULTIPLICATION
Aim:
Write a C program to multiply two matrices.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.

Start the program.


Declare the A, B and C matrices.
Read the number of rows and columns of matrix A.
Read the number of rows and columns of matrix B.
If the number of rows and columns of matrices A and B are equal means, then
Read the elements of matrix A.
Read the elements of matrix B.
Multiply the matrices A and B and store it in C as
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
C[i][j]=o;
for(k=0;k<m;k++)
{
C[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
9. Print the c[i][j].
10. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,m,n,p,q;
clrscr();
printf("\n Enter the no of rows and columns of matrix A : ");
scanf("%d%d",&m,&n);
printf("\n Enter the no of rows and columns of matrix B : ");
scanf("%d%d",&p,&q);
if((m==p) && (n==q))
{
printf("\n Enter the elements of matrix A : ");

45

Global Institute of Engineering & Technology

Computer practice lab -1

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

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

c[i][j] = 0;

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

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

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

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

print c[i][j];

Stop

46

Global Institute of Engineering & Technology

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf(" \n Enter the elements of matrix B : ");
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<n;j++)
{
c[i][j]=1;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",c[i][j]);
}
}
}
else
{
printf("\n Enter the equal no of rows and columns");
}
getch();
}

47

Computer practice lab -1

Global Institute of Engineering & Technology

48

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Output 1:
Enter the no of rows and columns of matrix A : 2 2
Enter the no of rows and columns of matrix B : 2 2
Enter the elements of matrix A : 1 2 3 4
Enter the elements of matrix B : 1 2 3 4
8 11
16 23

Output 2:
Enter the no of rows and columns of matrix A : 2 2
Enter the no of rows and columns of matrix B : 3 3
Enter the equal no of rows and columns

Result:
Thus the matrix multiplication program is written successfully.

49

Global Institute of Engineering & Technology

Flow Chart

Start

Read name;

n=strlen(name)
;

Print n;

Stop

50

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 15
Date :

STRING LENGTH
Aim:
Write a C program to find the length of the string.

Algorithm:
1.
2.
3.
4.
5.
6.

Start the program.


Read the string to the character type array variable name[25].
Find the length of the string by using the function strlen().
Assign the length to the integer type variable n.
Print n.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[25];
int n;
clrscr();
printf("\n Enter the string : ");
scanf("%s",name);
n=strlen(name);
printf("\n Length of the string is %d",n);
getch();
}

Output:
Enter the string : RAJKUMAR
Length of the string is : 8

Result:
Thus we can find the length of the string successfully.

51

Global Institute of Engineering & Technology

Flow Chart

Start

Read name;

n=strrev(name);

Print n;

Stop

52

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No :16
Date :

STRING REVERSE
Aim:
Write a C program to reverse the given string.

Algorithm:
1.
2.
3.
4.
5.

Start the program.


Read the string which you want to reverse.
Reverse the string by using the string handling function strrev().
Print the reversed string.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[25];
clrscr();
printf("\n Enter the string : ");
scanf("%s",name);
printf("\n The reversed string is : %s",strrev(name));
getch();
}

Output:
Enter the string : RAJI
The reversed string is : IJAR

Result:
Thus the given string is reversed by using the string handling function.

53

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
Start

Read S1,S2;

n=strcmp(s1,s2);

no

yes
if(n==
0)

Print The
given strings
same

Print The
strings are
different

Stop

54

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 17
Date :

STRING COMPARISON
Aim:
Write a C program to compare the two given strings.

Algorithm:
1. Start the program.
2. Read the two strings to the variables say S1 and S2.
3. Compare the two strings by the function strcmp(), and assign the value to variable
n.
4. If n == 0 then, print, The given strings are same.
5. If n != 0 then, print the given strings are different.
6. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int n;
clrscr();
printf("\n Enter the first string : ");
scanf("%s",s1);
printf("\n Enter the second string : ");
scanf("%s",s2);
n=strcmp(s1,s2);
if(n==0)
{
printf("\n The given strings same");
}
else
{
printf("\n The strings are different");
}
getch();
}

55

Global Institute of Engineering & Technology

56

Computer practice lab -1

Global Institute of Engineering & Technology

Output 1:
Enter the first string : ram
Enter the second string : ram
The given strings same.

Output 2:
Enter the first string : raj
Enter the second string : ram
The strings are different.

Result:
Thus the two strings are compared successfully.

57

Computer practice lab -1

Global Institute of Engineering & Technology

Flow Chart

Start

Read s1,s2;

strcpy(s1,s2);

Print s1,s2;

Stop

58

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 18
Date :

STRING COPY
Aim:
Write a C program to copy one string to another string.

Algorithm:
1.
2.
3.
4.
5.

Start the program.


Read the two strings S1 and S2.
Copy the string S2 to S1 by using the string function strcpy().
Print both the strings S1 and S2.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int n;
clrscr();
printf("\n Enter the first string : ");
scanf("%s",s1);
printf("\n Enter the second string : ");
scanf("%s",s2);
strcpy(s1,s2);
printf("\n After string copy");
printf("\n First string is : %s",s1);
printf("\n Second string is : %s",s2);
getch();
}

Output:
Enter the first string : raj
Enter the second string : kumar
After string copy
First string is : kumar
Second string is : kumar

Result:
Thus second string is copied with first string successfully
59

Global Institute of Engineering & Technology

Flow Chart

Start

Read s1,s2;

strcat(s1,s2);

Print s1;

Stop

60

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 19
Date :

STRING CONCATENATION
Aim:
Write a C program to concatenate two strings together.

Algorithm:
1.
2.
3.
4.
5.

Start the program.


Read the two strings to the variables
Concatenate both the string by the function strcat().
Print the concatenated string.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int n;
clrscr();
printf("\n Enter the first string : ");
scanf("%s",s1);
printf("\n Enter the second string : ");
scanf("%s",s2);
printf("\n The concatenated string is : %s",strcat(s1,s2));
getch();
}

Output:
Enter the first string : raj
Enter the second string : kumar
The concatenated string is : rajkumar

Result:
Thus the two strings are joined successfully.

61

Global Institute of Engineering & Technology

Computer practice lab -1

Flow Chart
main ( ) function

Start

Read n;

f = fact(n);

Print f;

Stop

fact( ) function
Start

Yes

No
if(n==0
|| n==1)

return(1);

return(n*fact(n-1));

Stop
62

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 20
Date :

FACTORIAL USING FUNCTION


Aim:
Write a C program to find the factorial of a given number by using function.

Algorithm:
main() program
1.
2.
3.
4.
5.
6.

Start the program.


Read the given number.
Call the function fact();
Assign the value of the function fact() to f;
Print f.
Stop the program.

fact() function
1.
2.
3.
4.

start the program.


if( (n = = 0) || ( n = = 1 ), return 1.
else return (n*fact(n-1)).
stop the program.

Program:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,f;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
f=fact(n);
printf("\n Factorial of %d is %d",n,f);
getch();
}
int fact(int n)
{
if(n==0 || n==1)
{
return(1);
}
else
{
return(n*fact(n-1));
}
}
63

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the number : 5
Factorial of 5 is 120

Result:
Thus the factorial of the given number is found successfully by using function
concept in C program.

64

Global Institute of Engineering & Technology

Flow Chart

Start

struct std
int rno,m1,m2,m3,tot;
char name[25],grade;
float avg;

Read n

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

Read
s[i].name,s[i].rno
,
s[i].m1,s[i].m2,
s[i].m3;

s[i].tot=s[i].m1+s[i].m2+s[
i].m3;
s[i].avg=s[i].tot/3;

65

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex N0 : 21
Date :

STRUCTURE
Aim:
Write a C program to create students mark list by using the structure concept.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.

Start the program.


Declare the structure std with its members.
Declare the structure variable s[].
Get the number of students.
Read the name, roll no and three subject marks of each student(s).
Calculate the total and average of the student.
Find the grade of the student based on the average.
Print the name, roll no, total, average and grade of the student(s).
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct std
{
int rno,m1,m2,m3,tot;
char name[25],grade;
float avg;
}s[100];
void main()
{
int i,n;
clrscr();
printf("\n Enter the number of students : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter student %d's name :",i);
scanf("%s",s[i].name);
printf("\n Enter student %d's Roll No: ",i);
scanf("%d",&s[i].rno);
printf("\n Enter student %d's 3 marks : ",i);
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
if(s[i].m1<50 || s[i].m2<50 || s[i].m3<50)
66

Global Institute of Engineering & Technology

Computer practice lab -1

A
B
Yes

if(s[i].m1<50
|| s[i].m2<50
|| s[i].m3<50)

No

s[i].grade='-';

s[i].grade='S'
;

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

Print
s[i].name,s[i].rno,s[i
].tot,s[i].avg,s[i].gra
de

Stop

67

No

if(s[i].m1<50
|| s[i].m2<50
|| s[i].m3<50)

Yes

s[i].grade='F'
;

Global Institute of Engineering & Technology

Computer practice lab -1

s[i].grade='-';
else
if(s[i].avg >= 75)
s[i].grade='F';
else if(s[i].avg >= 50)
s[i].grade='S';
}
printf("\n Name Roll No Total Avg Grade ");
for(i=1;i<=n;i++)
{
printf("\n %s\t%d\t%d\t%4.2f\t%c",s[i].name,s[i].rno,s[i].tot,s[i].avg,s[i].grade);
}
getch();
}

Output:
Enter the number of students : 2
Enter student 1's name :raj
Enter student 1's Roll No: 2
Enter student 1's 3 marks : 70 80 90
Enter student 2's name :kumar
Enter student 2's Roll No: 2
Enter student 2's 3 marks : 60 70 80
Name
raj
kumar

Roll No Total
2
2

240
210

Avg
80.00
70.00

Grade
F
S

Result:
Thus the student mark list is prepared successfully by using the structure in C
programming.

68

Global Institute of Engineering & Technology

Flow Chart

Start

union std
char name[25];
int age;

Read s.name;

Print s.name;

Read s.age;

Print s.age;

Stop

69

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Ex No : 22
Date :

UNION
Aim:
Write a C program to get and display your name and age by using union concept.

Algorithm:
1.
2.
3.
4.
5.
6.

Start the program.


Declare union std with the members name and age.
Create the union variable s.
Read the name and age.
Display the name and age.
Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
union std
{
char name[25];
int age;
}s;
void main()
{
clrscr();
printf("\n Enter your name :");
scanf("%s",s.name);
printf("\n Name : %s",s.name);
printf("\n Enter your age :");
scanf("%d",&s.age);
printf("\n Age : %d",s.age);
getch();
}

Output:
Enter your name : raj
Name: raj
Enter your age : 26
Age: 27

Result:
Thus the name and age of the student is read and displayed successfully.
70

Global Institute of Engineering & Technology

71

Computer practice lab -1

Global Institute of Engineering & Technology

72

Computer practice lab -1

Global Institute of Engineering & Technology

Computer practice lab -1

Write a C program to find the given number is odd or even.


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
if(n%2 == 0)
{
printf("\n The given number %d is even");
}
else
{
printf("\n The given number %d is odd");
}
getch();
}

Output 1:
Enter the number : 10
The given number 10 is even

Output 2:
Enter the number : 5
The given number 5 is odd

Write a C program to find the given year is leap year or not.


#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("\n Enter the year : ");
scanf("%d",&y);
if(y%4 == 0)
{
printf("\n The given year is leap year ");
}
73

Global Institute of Engineering & Technology

Computer practice lab -1

else
{
printf("\n The given year is not a leap year " );
}
getch();
}

Output 1:
Enter the year : 2006
The given year is leap year.

Output 2:
Enter the year : 2005
The given year is not a leap year.

Write a C program to find the biggest among three numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter the three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("\n A is the biggest no");
}
else if(b>c)
{
printf("\n B is the biggest no");
}
else
{
printf("\n C is the biggest no");
}
getch();
}

Output 1:
Enter the three numbers : 30 20 10
A is the biggest no
74

Global Institute of Engineering & Technology

Computer practice lab -1

Output 2:
Enter the three numbers : 20 30 10
B is the biggest no

Output 3:
Enter the three numbers : 20 10 30
C is the biggest no

Write a C program to display the cool drink name by its starting


character using the switch statement.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("\n Enter a character : ");
scanf("%c",&c);
switch(c)
{
case 'a':
printf("\n Drink for you : Appefizz");
break;
case 'b':
printf("\n Drink for you : Bovento");
break;
case 'c':
printf("\n Drink for you : Cocacola");
break;
case 'f':
printf("\n Drink for you : Fanta");
break;
case 'm':
printf("\n Drink for you : mazza");
break;
default:
printf("\n Your unlucky");
}
getch();
}

75

Global Institute of Engineering & Technology

Computer practice lab -1

Output1:
Enter a character : a
Drink for you : Appefizz

Output2:
Enter a character : g
Your unlucky

Write a C program to find the square root of the numbers


include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float s;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
printf("\n number & its square");
for(i=1;i<=n;i++)
{
s=sqrt(i);
printf("\n %d : %4.2f",i,s);
}
getch();
}

Output
Enter the number : 5
number & its square
1 : 1.00
2 : 1.41
3 : 1.73
4 : 2.00
5 : 2.24

76

Global Institute of Engineering & Technology

Computer practice lab -1

Write a C program to reverse a given number


#include<stdio.h>
#include<conio.h>
void main()
{
int n,t;
clrscr();
printf("\n Enter a number : ");
scanf("%d",&n);
printf("\n The reversed no is : ");
while(n!=0)
{
t=n%10;
printf("%d",t);
n=n/10;
}
getch();
}

Output:
Enter a number : 12345
The reversed no is : 54321

Write a C program to print the numbers in ascending order.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n %d",i);
}
getch();
}

77

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the number : 5
1
2
3
4
5

Write a C program to convert the Celsius into Fahrenheit


#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf("\n Enter the Celsius value : ");
scanf("%f",&c);
f=(1.8*c)+32;
printf("\n the Fahrenheit for the given Celsius is %4.2f",f);
getch();
}

Output:
Enter the Celsius value : 37
The Fahrenheit for the given Celsius is 98.60

Write a C program to find the Cosin Series.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,t,sum;
int i,n=10;
clrscr();
printf("\n Enter the value of X : ");
scanf("%f",&x);
printf(" X=%4.2f \n",x);
x=x*3.14/180;
t=x;
sum=x;
for(i=1;i<n+1;i++)
{
78

Global Institute of Engineering & Technology

Computer practice lab -1

t=(t*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i-1));
sum=sum+t;
}
printf("\n The value of COS(%d) = %6.2f\n",x,sum);
getch();
}

Output:
Enter the X value : 45
The value of COS(45) = 0.56

Write a C program to arrange the numbers in matrix format.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],i,j,m,n;
clrscr();
printf("\n Enter the no of rows and columns : ");
scanf("%d%d",&m,&n);
printf("\n Enter the elements of the matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
getch();
}

Output:
Enter the no of rows and columns : 2 2
Enter the elements if the matrix : 1 2 3 4
The matrix is
a. 2
79

Global Institute of Engineering & Technology


3

Write a c program to transpose the given matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],i,j,m,n;
clrscr();
printf("\n Enter the no of rows and columns : ");
scanf("%d%d",&m,&n);
printf("\n Enter the elements of the matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n The transpose of the matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",a[j][i]);
}
printf("\n");
}
getch();
}

Output:
Enter the no of rows and columns : 2 2
Enter the elements if the matrix : 1 2 3 4
The matrix is
1
2
3
4
The transpose of the matrix is
1
3
80

Computer practice lab -1

Global Institute of Engineering & Technology


2

Computer practice lab -1

Write a C program to find the sum of N numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("\n Enter the no : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\n The sum is %d",sum);
getch();
}

Output:
Enter the no : 5
The sum is 15

Write a C program to arrange the numbers in triangle format.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf(" %d",j);
}
}
getch();
}

81

Global Institute of Engineering & Technology

Computer practice lab -1

Output:
Enter the number : 5
1
12
123
1234
12345

Write a C program to print the * as an inverse triangle.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
printf("\n");
for(j=i;j>=1;j--)
{
printf("*");
}
}
getch();
}
Output:
Enter the number : 5
*****
****
***
**
*

Write a C program to find whether the given string is palindrome or


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

Global Institute of Engineering & Technology

Computer practice lab -1

{
int len=0,i,j;
char s[25];
clrscr();
printf("\n Enter the string : ");
scanf("%s",s);
while(s[len]!='\0')
len++;
printf("\n %d",len);
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(s[i] != s[j])
{
printf("\n The given string is not palindrome");
}
else
printf("\n The given string is palindrome");
break;
}
getch();
}

Output:
Enter the string : madam
5
The given string is palindrome

Output2:
Enter the string : raj
3
The given string is not a palindrome

Write a C program to interchange the values of the two variables.


#include<stdio.h>
#include<conio.h>
int swap(int,int);
void main()
{
int a,b;
clrscr();
printf("\n Enter two values : ");
scanf("%d%d",&a,&b);
printf("\n Before swap");
printf("\n A = %d B = %d",a,b);
printf("\n After swap");
swap(a,b);
getch();
}
83

Global Institute of Engineering & Technology

Computer practice lab -1

int swap(int a, int b)


{
int t;
t=a;
a=b;
b=t;
printf("\n A = %d B = %d",a,b);
}

Output:
Enter two values : 10 20
Before swap
A = 10 B = 20
After swap
A = 20 B = 10

Write a C program to reverse a given string without using the string


handling function. (Example program for recursion).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
void reverse(void);
printf("\n Reversing a string");
printf("\n Enter the string : ");
reverse();
getch();
}
void reverse()
{
char c;
c = getchar();
if(c != '\n')
reverse();
putchar(c);
}

Output:
Reversing a string
Enter the string : c programming
gnimmargorp c
84

Global Institute of Engineering & Technology

Computer practice lab -1

Write a C program to display the value and address of the variable by


using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
p=&a;
printf("\n Enter the value : ");
scanf("%d",&a);
printf("\n The value is : %d",a);
printf("\n The address of the value is : %u",p);
printf("\n The value in the address %u is %d",p,*p);
getch();
}

Output:
Enter the value : 10
The value is : 10
The address of the value is : 65524
The value in the address 65524 is 10

Write a C program to convert the given lower case character into upper
case character.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c;
clrscr();
printf("\n Enter a character : ");
scanf("%c",&c);
printf("\n Upper case character : %c",toupper(c));
getch();
}

Output:
Enter a character : a
85

Global Institute of Engineering & Technology

Computer practice lab -1

Upper case character : A

Write a C program to explain the Global and local variables


#include<stdio.h>
#include<conio.h>
void fun(void);
int a=100;
main()
{
clrscr();
printf("\n Global and Local variables");
printf("\n A = %d",a);
fun();
printf("\n A = %d",a);
getch();
}
void fun()
{
int a=10;
printf("\n A = %d",a);
}

Output:
Global and Local variables
A = 100
A = 10
A = 100

Write a C program to explain the auto variables.


#include<stdio.h>
#include<conio.h>
void fun1(int);
void fun2(int);
void main()
{
auto int a=10;
clrscr();
printf("\n Auto variable");
printf("\n A = %d",a);
fun1(a);
fun2(a);
getch();
}
void fun1(int a)
86

Global Institute of Engineering & Technology

Computer practice lab -1

{
a=20;
printf("\n A = %d",a);
}
void fun2(int a)
{
a=30;
printf("\n A = %d",a);
}

Output:
Auto variable
A = 10
A = 20
A = 30

Write a C program to create a file and write a string in it and also


display the string from the file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
FILE *fp;
clrscr();
fp=fopen("file.txt","w");
printf("\n Reading and writing strings");
if(fp == NULL)
printf("\n Error in file operation");
else
{
printf("\n Enter the string ( 0 to exit ) : ");
gets(s1);
fputs(s1,fp);
}
fclose(fp);
fp = fopen("file.txt","r");
fgets(s2,50,fp);
printf("\n The string is : %s",s2);
fclose(fp);
getch();
}

Output:
Reading and writing strings
Enter the string ( 0 to exit ) : hi hello
87

Global Institute of Engineering & Technology

Computer practice lab -1

The string is : hi hello

Write a C program to append the string with the existing content of the
file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
FILE *fp;
clrscr();
printf("\n Appending the strings");
printf("\n Content of the file before appending ");
fclose(fp);
fp = fopen("file.txt","r");
fgets(s2,50,fp);
printf("\n The string is : %s",s2);
fclose(fp);
fp=fopen("file.txt","a");
if(fp == NULL)
printf("\n Error in file operation");
else
{
printf("\n Enter the string ( 0 to exit) : ");
gets(s1);
fputs(s1,fp);
}
fclose(fp);
printf("\n Content of the file after appending");
fp = fopen("file.txt","r");
fgets(s2,50,fp);
printf("\n the string is : %s",s2);
fclose(fp);
getch();
}

Output:
Appending the strings
Content of the file before appending
The string is : hi hello
Enter the string ( 0 to exit ) : welcome to cse
Content of the file after appending
88

Global Institute of Engineering & Technology

Computer practice lab -1

The string is : hi hello welcome to cse

Write a C program to display the ASCII character set.


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf("ASCII character set\n");
for(i=0;i<=155;i++)
{
printf(" %c",i);
}
getch();
}

Output:
ASCII character set


! " # $ % & ' (
) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B
C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ]
^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x
y z { | } ~ 

89

Vous aimerez peut-être aussi