0% found this document useful (0 votes)
6 views74 pages

14 Programming in C Lab Manual

The document contains a series of C programming exercises aimed at teaching various programming concepts such as solving quadratic equations, finding the biggest number using a ternary operator, converting centigrade to Fahrenheit, checking leap years, and implementing a simple calculator. Each program includes an aim, algorithm, code, and sample output. The exercises are designed to enhance understanding of C programming through practical implementation.

Uploaded by

BALAJI D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views74 pages

14 Programming in C Lab Manual

The document contains a series of C programming exercises aimed at teaching various programming concepts such as solving quadratic equations, finding the biggest number using a ternary operator, converting centigrade to Fahrenheit, checking leap years, and implementing a simple calculator. Each program includes an aim, algorithm, code, and sample output. The exercises are designed to enhance understanding of C programming through practical implementation.

Uploaded by

BALAJI D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 74

404 Programming in C

Programming in C Laboratory 405

Program No: 1A
File Name: SOLVING QUADRATIC EQUATION
Ex. No:
Date: ___________

Aim:
To write a C program to find the roots of a quadratic equation.

Algorithm:
1. Start
2. Enter three coefficients a,b,c.
3. Calculate d = b*b – 4 * a a.
4. Check if d = 0 then roots are equal, and calculate
root1 = root2 = -b/(2.0 * a)
5. If d> 0 then roots are real and distinct. Calculate root1 = -b(2.0*a)
root1 = (-b + sqrt (d))/(2*a)
root2 = (-b – sqrt (d))/(2*a)
6. If d<0 then roots are imaginary root contains real and imaginary parts.
Calculate realp = -b(2*a),
imagp = sqrt(d)/(2*a) and
root1 = real p+iimgp
root2 = real p-iimgp.
7. Display root1, root2.
8. Stop.
406 Programming in C

Program:

#include<stdio.h>
#include<math.h>
int main()
{
float root1,root2,realp,imgp,a,b,c,d;

clrscr()

printf(“Enter three coefficient of quadratic equation”);

scanf(“%f%f%f”,&a,&b,&c);

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

if(d= =0)
{
printf(“roots are equal\n”);
root1=root2=-b/(2*a);
printf(“root1=root2=%f\n”, root1);
}
else if(d>0)
{
printf(“roots are real and distinct\n”);
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf(“root1=%f\n”,root1);
printf(“root2=%f\n”,root2);
}
else
{
printf(“roots are imaginary\n”);
realp=-b/(2*a);
imgp=sqrt((-d))/(2*a);
Programming in C Laboratory 407

printf(“root1=%f+i %f\n”,realp,imgp);

printf(“root2=%f- i %f\n”,realp,imgp);

getch( );

Output:

Enter three coefficient of quadratic equation


1
2
1
roots are equal
roots 1=root2= -1.000000

Program No: 1B
File Name: FINDING BIGGEST AMOUNG TWO NUMBERS
Ex. No: USING TERNARY OPERATOR
Date: ___________

Aim:
To write a c program to find biggest among two numbers using ternary
operator.
Algorithm:
1. Start.
2. Read two numbers A and B.
3. Check c = (A>B)? A:B. That is if A is bigger than B then C=A otherwise
C=B.
4. Print bigger value as C.
5. Stop.
408 Programming in C

Program:

#include<stdio.h>

#include<conio.h>

void main()

int A,B,C;

clrscr();

printf(“ Enter the value of A :”);

scanf(“%d”,&A);

printf(“ Enter the value of B :”);

scanf(“%d”,&B);

C=(A>B) ? A : B;

printf(“The Biggest value is = %d\n”, C);

getch();

Output:

Enter the value of A :34

Enter the value of B :78

The Biggest value is = 78


Programming in C Laboratory 409

Program No: 1C
CONVERTING CENTIGRADE TO
File Name:
Ex. No: FAHRENHEIT
Date: ___________

Aim:
To write a C program to convert centigrade to Fahrenheit.
Algorithm:

1. Start.
2. Read centigrade value C.
3. Calculate F = C* (9/5)+32
4. Print Fahrenheit value F.
5. Stop.

Program:

#include <stdio.h>
#include <conio.h>
void main()
{
float c,f;
clrscr();
printf(“CONVERSION OF CENTIGRADE TO FARENHIT\n”);
scanf(“%f”,&c);
printf(“CENTIGRADE VALUE: %f\n”,c);
printf(“\n FORMULA USED\n”);
printf(“\n F = C*(9/5) + 32\n”);
410 Programming in C

f=c*9/5+32;
printf(“\n FARENHEIT VALUE:%\n”,f);
getch();
}
Output:

Conversion of centigrade to farenhit


165
Centigrade value: 165.000000
Formula used
F=C*(9/5)+32
Farenheit value: 329.000000

Program No: 2A
File Name:
CHECKING LEAP YEAR OR NOT
Ex. No:
Date: ___________

Aim:

To write a C program to check leap year or not.

Algorithm:

1. Start.

2. Read year.

3. Check whether year is divisible by 4 and 400 and not divisible by 100.

4. If the condition is true then print year is leap year. Otherwise print year is not
leap year.

5. Stop.
Programming in C Laboratory 411

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main()

int i, year;

clrscr();

printf(“Enter the value of N:”);

scanf(“%d”,&year);

if(((year%4==0)&&(year%100!=0))||(year%400==0))

printf(“%d is a leap year\n”,year);

else

printf(“%d is not a leap year\n”,year);

getch();

Output:

Enter the Value of N: 2000


2000 is a leap year.
Enter the value of N: 1700
1700 is not a leap year.
412 Programming in C

Program No: 2B
File Name:
BIGGEST AMONG TWO NUMBERS USING
Ex. No:
GOTO
Date: ___________

Aim:
To write a C program to find biggest among two numbers using goto.
Algorithm:

1. Start.
2. Read two numbers a, b.
3. If a>b then go to greater. Otherwise print B is greater than A.
4. In greater section print A is greater than B.
5. Stop.

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main()

inta,b;

clrscr();

printf(“Enter two numbers:\n”);

scanf(“%d %d”,&a,&b);

if(a>b)
Programming in C Laboratory 413

goto greater;

else

printf(“B is greater than A”);

exit(0);

greater:

printf(“A is greater than B”);

getch();

Output:

Enter two numbers

45

12

A is greater than B

Enter two numbers

45

97

B is greater than A
414 Programming in C

Program No: 2C
File Name:
Ex. No:
SIMULATING SIMPLE CALCULATOR
Date: ___________

Aim:

To write a C program to simulate simple calculator.

Algorithm:

1. Start
2. Display the menu
3. Read the choice
4. If the choice is 1, then Read two numbers. Add two numbers, and print the
result.
5. If the choice is 2, then read two numbers, subtract a-b and print the result.
6. If the choice is 3 then read two numbers, multiply two numbers and print the
result.
7. If the choice is 4 then read two numbers, divide a/b and print the result.
8. If the choice is 5, then exit.

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main( )

float num1,num2, result;


Programming in C Laboratory 415

char op;

clrscr( );

printf(“Type your expression(num1 op num2)”);

scanf(“%f %c %f”, &num1,&op,&num2);

switch (op)

case ‘+’:

result = num1+ num2;

break;

case ‘-’:

result = num1- num2;

break;

case ‘*’:

result = num1* num2;

break;

case ‘/’:

if (num2 = = 0)

printf(“\nDivision by zero error”);

else

{
416 Programming in C

result = num1/ num2;

break;

default:

printf(“\n Invalid operator”);

printf(“\n%.2f %c %.2f = %.2f”, num1,op, num2, result);

getch( );

Output:

Type your expression (num1 op num2)

65-98

65.00-98.00=-33.00

Type your expression (num1 op num2)

65+76

65.00+76.00=141.00

Type your expression (num1 op num2)

7*7

7*7=49

Type your expression (num1 op num2)

6/6

6/6=0
Programming in C Laboratory 417

Program No: 2D
File Name: PRINTING NUMBER DIGITS
Ex. No:
Date: ___________
Aim:
To write a c program to print number digits.
Algorithm:
1. Start.
2. Read n
3. If n is 1 then print “ONE”,
if n is 2 then print “TWO”,
Similarly print till number 9 and numbers 0.
4. Stop.

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main()

int n;

clrscr();

printf(“Enter the value of n”);

scanf(“%d”,&n);

switch(n)
418 Programming in C

case 1:

printf(“ONE”);

break;

case 2:

printf(“TWO”);

break;

case 3:

printf(“THREE”);

break;

case 4:

printf(“FOUR”);

break;

case 5:

printf(“FIVE”);

break;

case 6:

printf(“SIX”);

break;

case 7:

printf(“SEVEN”);

break;
Programming in C Laboratory 419

case 8:

printf(“EIGHT”);

break;

case 9:

printf(“NINE”);

break;

case 0:

printf(“ZERO”);

break;

default:

printf(“Invalid number”);

break;

getch();

Output:

Enter the value of N: 1


ONE
Enter the value of N:2
TWO
Enter the value of N:3
THREE
420 Programming in C

Enter the value of N:4


FOUR
Enter the value of N:5
FIVE
Enter the value of N:6
SIX
Enter the value of N:7
SEVEN
Enter the value of N:8
EIGHT
Enter the value of N:9
NINE
Enter the value of N:0
ZERO

Program No: 2E
File Name:
Ex. No:
ILLUSTRATION OF CONTINUE
Date: ___________
STATEMENT

Aim:
To write a C program to print numbers to illustrate continue statement.
Algorithm:
1. Start
2. Assign j = 10.
3. For (i=0; i<=j; i++)
if i==j then continue
otherwise print i value
Programming in C Laboratory 421

4. Stop.

Program:
Program:
#include<stdio.h>

#include<conio.h>

void main()

inti;

int j=10;

clrscr();

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

if(i==5)

continue;

printf(“Hello %d\n”,i);

getch();

Output:

Hello 0
Hello 1
422 Programming in C

Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Program No: 3A
File Name:
ILLUSTRATION OF FOR
Ex. No: EVALUATE 13 + 23 + 33 + …N3
Date: ___________

Aim:
To write a c program to evaluate the series.

Algorithm:
1. Start
2. Read n, assign sum = 0
3. for i = 1 to n
calculate sum = sum + 1/(i*i*i)
4. print sum
5. Stop.
Programming in C Laboratory 423

Program:
Program:

#include<stdio.h>

void main()

intn,i,sum=0;

clrscr();

printf(“\n Enter n \t”);

scanf(“%d”,&n);

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

sum=sum+i*i*i;

printf(“Sum = %d”,sum);

getch();

Output:

Enter n 5
Sum = 225
424 Programming in C

Program No: 3B
ILLUSTRATION OF FOR
File Name:
Ex. No: GENERATING FIBONACCI SERIES
Date: ___________

Aim:

To write a C program to generate a Fibonacci series.

Algorithm:

1. Start
2. Read num
3. Assign f1=0, f2=1.
4. Set loop for I and for all i<num value and calculate f3=f1+f2.
5. Display f1
6. Assign f1=f2 and f2=f3
7. Stop.

Program:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
int f1=0,f2=1,f3;
clrscr();
printf(“Enter how many numbers”);
scanf(“%d”,&num);
Programming in C Laboratory 425

for(i=1;i<=num;i++)
{
f3=f1+f2;
printf(“%5d”,f1);
f1=f2;
f2=f3;
}
getch();
}
Output:
Enter how many numbers 7
0 1 1 2 3 5 8

Program No: 3C ILLUSTRATION OF WHILE


File Name:
CHECKING PALINDROME NUMBER OR NOT
Ex. No:
Date: ___________

Aim:

To write a C program to check whether given number is palindrome number


or not.

Algorithm:

1. Start.

2. Read n.

3. Assign rev = 0 and temp = h

4. Calculate

r=n%10
426 Programming in C

rev=rev*10+r

n=n/10

5. Repeat step 4 till n>0

6. Check whether the values of temp and revere equal.

7. If both are equal then print number is palindrome.

8. If both are not equal then print number is not palindrome.

9. Stop.

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main()

int n,r,rev,temp;

rev=0;

clrscr();

printf(“Enter the number”);

scanf(“%d”,&n);

temp=n;

while(n>0)

r=n%10;
Programming in C Laboratory 427

rev=rev*10+r;

n=n/10;

printf(“The reversed of number %d is = %d\n”, temp, rev);

if(temp==rev)

printf(“It is palindrome”);

else

printf(“It is not palindrome”);

getch();

Output:

Enter the number: 123

The reversed of number 123 is = 321

It is not palindrome

Enter the number: 151

The reversed of number 151 is = 151

It is palindrome
428 Programming in C

Program No: 3D
File Name: ILLUSTRATION OF DO-WHILE
PRINTING NUMBERS
Ex. No:
Date: ___________

Aim:
To write a C program to print numbers using do-while loop.
Algorithm:
1. Start
2. Assign digit = 0
3. Using do-while loop
Print the digit and increment i. If digit <=9
4. Stop.

Program:
Program:

#include<stdio.h>

#include<conio.h>

void main()

int digit=0;

clrscr();

do

printf(“%d \t”,digit);

++digit;

}
Programming in C Laboratory 429

while(digit<=9);

getch();

Output:

0 1 2 3 4 5 6 7 8 9

Program No: 4A
File Name:
COMPUTING MEAN VALUE OF N NUMBERS
Ex. No:
Date: ___________

Aim:
To write a C program to compute the mean value of N numbers.
Algorithm:
1. Start
2. Read number of elements n, and n numbers in an array.
3. Assign sum=0.
4. Read one element at a time from the array and add it to the sum.
5. Calculate mean = sum/n
6. Print mean
7. Stop.
430 Programming in C

Program:
Program:

#include<stdio.h>

#include<math.h>

void main()

int n,i;
float A[20],sum=0.0,mean;
clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&A[i]);
}
for(i=0;i<n;i++)
{
sum=sum+A[i];
}
mean=sum/n;
printf("Arithmetic mean=%f\n",mean);
getch();
}
Programming in C Laboratory 431

Output:
Enter number of elements
6
Enter the elements
8
3
9
4
5
6
Arithmetic mean=5.833333

Program No: 4B
File Name:
Ex. No:
ADDITION OF TWO MATRICES
Date: ___________

Aim:

To write a C program to add two matrices.

Algorithm:

1. Start.

2. Read the elements of matrix A.

3. Read the elements of matrix B.

4. Set a loop up to the row.

5. Set a inner loop up to the column.


432 Programming in C

6. Add the element of A and B in column wise and store the result in
sum matrix.

7. After execution of two loops, print the values in sum matrix.

8. Stop.

Program:
Program:

#include<stdio.h>

void main()

int A[3][3],B[3][3],sum[3][3],i,j;
clrscr();
printf(“Enter the elements of matrix A”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}

printf(“Enter the elements of matrix B”);

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

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

scanf(“%d”,&B[i][j]);
Programming in C Laboratory 433

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

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

sum[i][j]=A[i][j]+B[i][j];

printf(“sum of two matrix\n”);

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

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

printf(“%3d”,sum[i][j]);

printf(“\n”);

}
Output:
Enter the elements of matrix A
101
111
010
434 Programming in C

Enter the elements of matrix B


011
100
111
sum of two matrix
112
211
121

Program No: 4C
File Name:
MULTIPLICATION OF TWO MATRICES
Ex. No:
Date: ___________

Aim:

To write a C program to multiply two matrices.

Algorithm:

1. Start.

2. Enter the row and column of the matrix A.

3. Enter the row and column of the matrix B.

4. Enter the elements of the matrix A.

5. Enter the elements of the matrix B.

6. Print the elements of the matrix A in matrix from.

7. Print the elements of the matrix B in matrix from.

8. Set a loop up to row.

9. Set an inner loop up to column.


Programming in C Laboratory 435

10. Set another inner loop up to column.

11. Multiply the A and B matrix and store the elements in the C matrix.

12. Print the resultant matrix.

13. Stop.

Program:
Program:

#include <stdio.h>

void main()

intA[10][10],B[10][10],C[20][20];

int i,j,k,m,n,p,q;

clrscr();

printf(“Enter the row and column value of A matrix”);

scanf(“%d%d”, &m,&n);

printf(“Enter the row and column value of B matrix”);

scanf(“%d%d”, &p,&q);

if(n!=p)

printf(“matrix multiplication is not possible”);

getch();

printf(“Enter the elements of matrix A”);


436 Programming in C

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

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

scanf(“%d”,&A[i][j]);

printf(“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<q;j++)

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

C[i][j]=C[i][j]+A[i][k]*B[k][j];
Programming in C Laboratory 437

printf(“The resultant matrix is\n”);

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

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

printf(%3d”,C[i][j]);

printf(“\n”);

getch();

Output:

Enter the row and column value of A matrix 3 3

Enter the row and column value of B matrix 3 3

Enter the elements of matrix A 1

5
438 Programming in C

Enter the elements of matrix B 9

The resultant matrix is

30 24 18

84 69 54

138 114 90
Programming in C Laboratory 439

Program No: 4D
File Name: LINEAR SEARCH
Ex. No:
Date: ___________

Aim:
To write a C program to perform linear search.

Algorithm:
1. Start
2. Read n and n numbers in array A.
3. Read the key element to be searched
4. Assign sound = 0
5. Access the elements one at a time and compare it with the key.
6. If both are equal then print key found and increment sound variable.
7. After checking all the elements check if found=0 then print key not found.
8. Stop.

Program:
Program:

#include<stdio.h>

void main()
{

int n,A[20],key,i,found=0;
clrscr();

printf(“Enter the value of n”);

scanf(“%d”,&n);
440 Programming in C

printf(“Enter %d numbers”,n);

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

scanf(“%d”,&A[i]);

printf(“Enter key element to be searched”);

scanf(“%d”,&key);

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

if(key= =A[i])

printf(“key found”);

found++;

else if(found= =0)

printf(“key not found”);

getch();

Output:

Enter the value of n6

Enter 6 numbers1
Programming in C Laboratory 441

Enter key element to be searched3

key found

Program No: 5
File Name:
SORTING NAMES IN ASCENDING ORDER
Ex. No:
Date: ___________

Aim:
To write a C program to sort the names in alphabetical order using string
function.
Algorithm:
1. Start
2. Enter number of names.
3. Enter the names.
4. Set two loops and compare every two strings.
5. If strcmp function return greater than zero value then swap two strings. Using
strcpy function and use temporary variable
6. Repeat step 5 until all the strings are compared
7. Print the names in sorted order
8. Stop.
442 Programming in C

Program:
Program:

#include<stdio.h>

#include<string.h>

void main()

char names[10][15],temp[25];

int i,j,n;

clrscr();

printf(“Enter number of names \n”);

scanf(“%d”,&n);

printf(“Enter the names \n”);

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

scanf(“%s”,names[i]);

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

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

if(strcmp(names[i],names[j])>0)

strcpy(temp,names[i]);
Programming in C Laboratory 443

strcpy(names[i],names[j]);

strcpy(names[j],temp);

printf(“\nThe names to sorted order”);

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

printf(“\n%s”,names[i]);

getch();

Output:

Enter number of names


5
Enter the names
varsha
denisha
jovita
suthan
Effie

The names to sorted order


Effie
denisha
jovita
suthan
444 Programming in C

varsha

Program No: 6A
File Name: FINDING MAX OF THREE NUMBERS USING
Ex. No: FUNCTION
Date: ___________

Aim:

To write a C program to find maximum of three numbers using maximum of


three numbers using function.

Algorithm:

1. Start

2. Read a,b,c

3. Call max function with the parameters a,b,c.

4. Print maximum value.

5. Stop.

Max function:

1. Start

2. If a>b and a>c then assign max=9.

3. Otherwise if b>c then assign max=b. Otherwise assign max=c.

4. Return max.

5. Stop.
Programming in C Laboratory 445

Program:
Program:

#include<stdio.h>

int max(int,int,int);

void main()

int a,b,c,d;

clrscr();

printf(“\n Enter three integer values:”);

scanf(“%d %d %d”,&a,&b,&c);

d=max(a,b,c);

printf(“\n Maximum is : %d”,d);

getch();
}
int max(int a, int b,int c)
{
int max;
if(a>b&&a>c)
max=a;
else if(b>c)
max=b;
else
446 Programming in C

max=c;
return(max);
}
Output:
Enter three integer values:

56

28

79

Maximum is: 79

Program No: 6B
SWAPPING TWO NUMBERS USING PASS BY
File Name:
VALUE
Ex. No:
Date: ___________
Aim:

Aim:
To write a C program swapping two number using pass by value.
Algorithm:
1. Start
2. Call swap function and pass a,b as arguments.
3. Calculate
x=x+y
y=x-y
x=x-y
4. Display the swapped numbers
5. Stop.
Programming in C Laboratory 447

Program:
Program:

#include<stdio.h>
void swap(int,int);
void main()
{
int a=10,b=20;
clrscr();
printf(“Before swap values are %d %d\n”,a,b);
swap(a,b);
printf(“After swap values are %d %d\n”,a,b);
getch();
}
void swap(int x,int y)
{

x=x+y;

y=x-y;

x=x-y;

printf(“In swap function values are %d %d\n”,x,y);

return;

Output:

Before swap values are 10 20

In swap function values are 20 10

After swap values are 10 20


448 Programming in C

Program No: 6C
SWAPPING TWO NUMBERS USING PASS BY
File Name:
REFERENCE
Ex. No:
Date: ___________

Aim:

To write a C program swapping two number using pass by address/reference.

Algorithm:

1. Start

2. Call swap function and pass address of a,b as arguments

3. Calculate

*x=*x+*y

*y=*x-*y

*x=*x-*y

4. Display x,y values

5. Stop.

Program:
Program:

#include<stdio.h>

int swap(int*,int*);

void main()

int a=10,b=20;
Programming in C Laboratory 449

clrscr();
printf(“Before swap values are %d %d\n”,a,b);
swap(&a,&b);
printf(“After swap values are %d %d\n”,a,b);
getch();
}
int swap(int*x,int*y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf(“In swap function values are %d %d\n”,*x,*y);
return;

Output:

Before swap values are 10 20

In swap function values are 20 10

After swap values are 20 10


450 Programming in C

Program No: 6D
File Name:
SORTING NUMBERS IN ASCENDING ORDER
Ex. No:
USING FUNCTION
Date: ___________

Aim:

To write a C program to sort numbers in ascending order using function.

Algorithm:

1. Start

2. Read total number of items n

3. Read n numbers in array a.

4. Call function sort with the parameters a,n.

5. Print the sorted array.

6. Stop.

Sort function:

1. Start

2. Compare elements to sort in ascending order.

3. If the first element is greater than the second element then interchange the elements.

4. Repeat step 2 and 3 to get the finally sorted element.

5. Stop.

Program:
Program:

#include<stdio.h>
#include<conio.h>
int sort(int[],int);
Programming in C Laboratory 451

void main()
{
int n,i,j,a[50];
clrscr();
printf("Enter total number of items:");
scanf("%d",&n);
printf("\n Enter the elements one by one:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
printf("\n The numbers after sorting is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

int sort(int x[],int m)


{
int i,j,temp;
for(i=0;i<m;i++)
for(j=i+1;j<m;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
return 0;
}

Program No: 7
File Name: FACTORIAL OF GIVEN NUMBER USING
Ex. No: RECURSIVE FUNCTION
Date: ___________
452 Programming in C

Aim:

To write a C program to find the factorial of a number using recursion.

Algorithm:

1. Start

2. Enter the number

3. Call the factorial recursive function by passing the number

4. Print the result

5. Stop.

Recursive Function:

1. Start the function

2. Assign fact=1

3. If num equals 1 then factorial is 1

4. Calculate fact = x* fact(x-1)

5. Return fact value

Program:
Program:
Programming in C Laboratory 453

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

int n;

clrscr();

printf(“Enter the number :\n”);

scanf(“%d”,&n);

printf(“factorial of %d = %d”,n,fact(n));

getch();

int fact(int x)

int i;

if(x == 1)

return(1);

else

i = x*fact(x-1);

return(i);
454 Programming in C

Output:

Enter the number :

factorial of 5 = 120

Program No: 8A
File Name: POINTERS TO FUNCTIONS
Ex. No:
Date: ___________ CALCULATING AREA OF A TRIANGLE

Aim:

To write a C program to calculate area of a triangle using pointers and functions.

Algorithm:

1. Start
2. Read base, height
3. Calculate area=0.5*base*height
4. Print area
5. Stop

Program:
Program:

#include<stdio.h>
void read(float *b, float *h);
void calculate_area (float *b, float *h),
float *a;
int main()
Programming in C Laboratory 455

{
float base, height, area;
read(&base, &height);
calculate_area(&base, &height, &area);
printf(“\n Area of the triangle with base %. If and height % . If = % . 2f”, base,
height, area);
return 0;
}
void read(float *b, float *h)
{
printf(“\n Enter the base of the triangle:”);
scanf(“%f”, b);
printf(“\n Enter the height of the triangle: “);
scanf(“%f”, h);
}

void calculate_area (float *b, float *h, float *a)

*a = 0.5 * (*b) * (*h);

Output:

Enter the base of the triangle: 10


Enter the height of the triangle : 5
Area of the triangle with base 10.0 and height 5.0 = 25.00

Program No: 8B
File Name: POINTERS & ARRAYS
Ex. No: FINDING MEAN OF N NUMBERS USING
Date: ___________ ARRAYS
456 Programming in C

Aim:
Finding mean of n numbers using arrays.
Algorithm:
1. Start
2. Assign mean=0.0
3. Read numbers of elements
4. Read n elements is array
5. Add all the elements in the array and divide the sum by total number of
elements to find the mean
6. Print sum and mean
7. Stop.

Program:
Program:

#include<stdio.h>
int main()
{
int i, n, arr[20], sum =0;
int *pn = &n, *parr = arr, *psum = &sum;
float mean = 0.0, *pmean = &mean;
printf (“\n Enter the number of elements:”);
scanf (“%d”, pn);
for (i = 0; i < *pn; i++)
{
Programming in C Laboratory 457

printf(“\n Enter the number: “);


scanf(“%d”, (parr + i));
}
for(i=0; i< *pn; i++)
*psum += *(arr+i);
*pmean = (float)*psum / *pn;
printf(“\n The numbers you entered are: “);
for (i=0; i< *pn; i++)
printf(“%d “, *(arr + i));
printf(“\n The sum is: %d”, *psum);
printf(“\n The mean is: %.2f”, *pmean);
}
Output:

Enter the number of elements: 5


Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
The numbers you entered are:
12345
The sum is: 15
The mean is: 3. 00

Program No: 9A
File Name: NESTED STRUCTURES:PRINTING
Ex. No: EMPLOYEE PERSONAL DETAILS
Date: ___________
458 Programming in C

Aim:

To write a C program to calculate the gross salary using structure within


structure.

Algorithm:

1. Start
2. Struct employee
e_code[4]=char
e_name[20]=char
e_bp=float
struct
e_da=float
e_hra=float
allo
e_pf=float
end struct.
3. Read the employee code, name, basic pay, da, hra, pf.
4. Print the employee code, name, basic pay, da, hra, pf.
5. Calculate gross
salary=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf.
6. Print the gross salary.
7. Stop.

Program:
Program:
Programming in C Laboratory 459

#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
char e_code[4];
char e_name[20];
float e_bp;
struct
{
float e_da;
float e_hra;
}
allo;
float e_pf;
};
struct employee emp1;
float gross;
printf(“\nEnter the code:”);
gets(emp1.e_code);
printf(“\nEnter the name:”);
gets(emp1.e_name);
printf(“\nEnter the basic pay:”);
scanf(“%f”,&emp1.e_bp);
printf(“\nEnter the dearness allowance:”);
scanf(“%f”,&emp1.allo.e_da);
printf(“\nEnter the house rent allowance:”);
scanf(%f”,&emp1.allo.e_hra);
printf(“\nEnter the provident fund:”);
460 Programming in C

scanf(“%f”,&emp1.e_pf);
printf(“\nCode :”);
puts(emp1.e_code);
printf(“\nName :”);
puts(emp1.e_name);
printf(“\nBasicpay :%8.2f”,emp1.e_bp);
printf(“\nDearness allowance :%8.2f”,emp1.allo.e_da);
printf(“\nHouserent allowance:%8.2f”,emp1.allo.e_hra);
printf(“\nProvidentfund :%8.2f”,emp1.e_pf);
gross=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf;
printf(“\nNetpay :%8.2f”,gross);
}
Output:

Enter the code :1990


Enter the name : Ramesh
Enter the basic pay:15000
Enter the dearness allowance:1500
Enter the house rent allowance:1000
Enter the provident fund:800
Code :1990
Name :Ramesh
Basic pay :15000.00
Dearness allowance :1500.00
House rent allowance :1000.00
Provident fund :800.00
Net pay :18300.00
Programming in C Laboratory 461

Program No: 9B
File Name: POINTERS TO STRUCTURES: PRINTING
Ex. No: STUDENT DETAILS
Date: ___________

Aim:
To write a C program to print student details using pointers and structures.
Algorithm:
1. Start
2. Declare student structure
3. Read student roll number, student name, branch, marks.
4. Print student roll number, student name, branch, marks.
5. Stop.

Program:
Program:

#include<stdio.h>
void main()
{
struct
{
int rollno;
char name[30];
char branch[4];
int marks;
}
*stud;
462 Programming in C

printf(“\n Enter Rollno :”);


scanf(“%d”, &stud->rollno);
printf(“\n Enter Name :”);
scanf(“%s”,stud->name);
printf(“\n Enter Branch:”);
scanf(“%s”,stud->branch);
printf(“\n Enter Marks:”);
scanf(“%d”,&stud->marks);
printf(“\n Roll Number:%d”,stud->rollno);
printf(“\n Name:%s”,stud->name);
printf(“\n Branch:%s”,stud->branch);
printf(“\n Marks:%d”,stud->marks);
getch();
}
Output:
Enter Rollno :1000
Enter Name :Ebisha
Enter Branch:CSE
Enter Marks:90

Roll Number:1000
Name:Ebisha
Branch:CSE
Marks:90
Programming in C Laboratory 463

Program No:9C
File Name: ARRAY OF STRUCTURES:CALCULATING
Ex. No: STUDENT MARK DETAILS
Date: ___________

Aim:

To write a C program to calculate the total marks using array of structures.

Algorithm:

1. Start
2. Declare the structure with members.
3. Initialize the marks of the students
4. Calculate the subject total by adding student
[i].sub1+student[i].sub2+student[i].sub3.
5. Print the total marks of the students
6. Stop.

Program:

#include<stdio.h>
#include<conio.h>
struct marks
{
int sub1;
int sub2;
int sub3;
int total;
};
464 Programming in C

void main()

int i;

struct marks student[3] = { {45, 67, 81, 0},{75, 53, 69, 0},

{57, 36, 71, 0}};

struct marks total;

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

student[i]. total = student[i].sub1+ student[i].sub2+ student[i].sub3;

printf(“TOTAL MARKS\n\n”);

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

printf(“student[%d]: %d\n”, i+1, student[i].total);

Output:

TOTAL MARKS

student[1]: 193

student[2]: 197

student[3]: 164
Programming in C Laboratory 465

Program No: 10A


File Name: READING AND WRITING FILE
Ex. No:
Date: ___________

Aim:
To write a C program to read and write a file.
Algorithm:
1. Start
2. Create a file pointer
3. Read the file name to be opened.
4. Open the file with write mode.
5. Write the data
6. Open the file with read mode
7. Print the data
8. Stop.

Program:

#include <stdio.h>

#define SIZE 50

int main()

FILE *fptr;

char s1[SIZE], s2[SIZE];

printf(“\nEnter any string: “);

gets(s1);
466 Programming in C

fptr = fopen(“abc.txt”, “w”);

fputs(s1, fptr);

fclose(fptr);

fptr = fopen(“abc.txt”, “r”);

fgets(s2, SIZE, fptr);

printf(“\nYou entered:”);

puts(s2);

}
Programming in C Laboratory 467

Program No: 10B


File Name: PROGRAM TO COUNT THE NUMBER OF
Ex. No: CHARACTERS AND NUMBER OF LINES IN A FILE
USING FILE POINTERS
Date: ___________

Aim:
To write a C program to count number of characters and number of lines in a
file using file pointer.
Algorithm:
1. Start
2. Create file pointers
3. Enter the file name
4. Open the file with read mode
5. Till the end of file reached read one character at a time
(a) If it is newline character ‘\n’, then increment no_of_lines count
value by one.
(b) if it is a character then increment no_of_characters count value by
one.
6. Print no_of_lines and no_of_characters values
7. Stop.

Program:

#include <stdio.h>
#include <string.h>
main ()
{
FILE *fp;
int ch,) no_of_characters = 0, no_of_lines = 1;
468 Programming in C

char filename[20];
printf(“\n Enter the filename: “);
fp = fopen(filename, “r”);
if(fp==NULL)
{
printf(“\n Error opening the File”);
exit (1);
}
ch= fgetc(fp);
while(ch!=EOF)
{
if(ch==’\n’);
no_of_lines++;
no_of_characters++;
ch = fgetc(fp);
}
if (no_of_characters >0)
printf(“\n In the file %s, there are %d
lines and %d characters”, filename, no_
of_lines, no_of_characters);
else
printf(“\n File is empty”);
fclose(fp);
}
Output:

Enter the filename: Letter.txt

In the file Letter. txt, there is 1 line and 18 characters


Programming in C Laboratory 469

Program No: 10C


File Name: PROGRAM TO COPY ONE FILE TO ANOTHER
Ex. No:
Date: ___________

Aim:

To write a C program to copy one file to another.

Algorithm:

1. Start

2. Create file pointers

3. Read two file names

4. Open first file with read mode

5. Open second file with write mode

6. Read first file character by character and write the characters in the second
file till end of file reached.

7. Close both file pointers

8. Stop.

Program:

#include <stdio.h>

#include <conio.h>
470 Programming in C

main ()

FILE *fp1, *fp2;

char filename1[20], filename[20];

str[30];

clrscr();

printf(“\n Enter the name of the first filename: “);

gets(filename1);

fflush(stdin);

printf(“\n Enter the name of the second filename: “);

gets(filename2);

fflush(stdin);

if((fp1=fopen(filename1, “r”))==0)

printf(“\n Error opening the first file”);

exit(1);

if((fp2=fopen(filename2, “w”))==0)

printf(“\n Error opening the second file”);

exit(1);

}
Programming in C Laboratory 471

while((fgets(str, sizeof(str),

fp1))!=NULL) fputs(str, fp2);

fclose(fp1);

fclose(fp2);

getch();

return 0;

Output:

Enter the name of the first filename:

a.txt

Enter the name of the second filename:

b.txt

FILE COPIED
472 Programming in C

Program No: 10D


File Name: PROGRAM TO RANDOMLY READ THE NTH
Ex. No: RECORD OF A FILE
Date: ___________

Aim:

To read nth record in file using random access method.

Algorithm:

1. Start

2. Create employee structure with variables

3. Read the file name with ‘rb’ mode

4. Read the record number to read

5. From the file pointed by fp read a record of the specified record starting from
the beginning of the file

6. Print the record values

7. Stop.

Program:

#include <stdio.h>

#include <conio.h>

main ()

typedef struct employee

{
Programming in C Laboratory 473

int emp_code;

char name[20];

int hra;

int da;

int ta;

};

FILE *fp;

struct employee e;

int result, rec_no;

fp = fopen (“employee.txt”, “rb”);

if(fp==NULL)

printf (“\n Error opening file”);

exit(1);

printf(“\n\n Enter the rec_no you want to

read: ”);

scanf(“%d”, &rec_no);

if(rec_no >= 0)

fseek(fp, (rec_no-1)*sizeof (e), SEEK_SET);

result = fread(&e, sizeof(e), 1, fp);


474 Programming in C

if (result == 1)

printf(“\n EMPLOYEE CODE: %d”, e. Emp_code);

printf(“\n Name: %S”, e.name);

printf(“\n HRA, TA and DA: %d %d %d”,

e.hra, e.ta, e.da);

else

printf(“\n Record Not Found”);

fclose(fp);

getch();

return 0;

Output:

Enter the rec_no you want to read: 06

Employee CODE: 06

Name : Tanya

HRA, DA and TA: 20000 10000 3000


Programming in C Laboratory 475

Program No: 10E


File Name: PROGRAM TO COMPUTE AREA OF A CIRCLE
Ex. No: USING PREPROCESSOR DIRECTIVES
Date: ___________

Aim:

To write a C program to compute area of a circle using pre-processor


directives.

Algorithm:

1. Start

2. Define pi as 3.1415

3. Define circle_area (or) as pi*r*r

4. Read radius

5. Call circle area function radius with to calculate area of a circle.

6. Print area

7. Stop.

Program:

#include <stdio.h>

#define PI 3. 1415

#definecircleArea(r) (PI*r*r)

int main()

float radius, area;

printf(“Enter the radius:”);


476 Programming in C

scanf(“%f”, &radius);

area = circleArea(radius);

printf(“Area = %.2f”, area);

return();

Output:

Enter the radius:6

Area = 113.094002

You might also like