Sunteți pe pagina 1din 140

Computer Programming Lab Manual

1. Printing any given message.

Aim:

To print any given message like C test program. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

Step 1: start
Step 2: print message like C test program

Step 3: stop

1
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("C test program");
getch();
}
Output:
C test program

2
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

2. Demonstrating the fundamental datatypes (int, float, char & double).


Aim:
To write a program which demonstrates the fundamental data types int , float, char &
double. For input & output statements we include the header file <stdio.h> and for clear the screen
include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read x, y, a, k
Step 3: assign values to x, y, a, k
Step 4: display the values
Step 5: stop

3
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float x;
int y;
char a;
double k;
clrscr();
x = 123.456;
y = 20;
a = 'z';
k = 9.3456677;
printf("x = %f \n", x);
printf("y = %d \n", y);
printf("a = %c \n", a);
printf("k = %lf \n", k);
getch();
}
Output:
x = 123.456001
y = 20
a=z
k = 9.345668
4
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

3. Swapping given two values.


Aim:

To write a program to swap two given integer values. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

Step 1: start

Step 2: read values for a & b.

Step 3: set temp = a

Step 4: set a = b

Step 5: set b = temp


Step 6: display values of a, b

Step 7: stop

5
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, temp;
clrscr();
printf("Enter the values of a, b: \n");
scanf("%d%d", &a, &b);
temp = a;
a = b;
b = temp;
printf("a = %d \n b = %d \n", a, b);
getch();
}
Output:
Enter the values of a, b
5
3
a=3
b=5
Exercise:
1. Swap two integer numbers without using temporary variable.
6
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

4. Calculation of simple interest.

Aim:
To write a program to evaluate simple interest. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read the values for p, n, r
Step 3: substitute the values in the expression (p * n * r) / 100
Step 4: evaluate and store the result in SI
Step 5: display the value of SI
Step 6: stop

7
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float p, n, r;
float simple = 0.0;
clrscr();
printf("Enter the values of p n r: \n");
scanf("%f%f%f", &p, &n, &r);
simple = (p*n* r) / 100.0;
printf("Simple Interest is %f \n", simple);
getch();
}
Output:
Enter the values of p n r
1000
2
20
Simple Interest is 400.000000

8
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

5. Calculating the area and perimeter of a circle.


Aim:

To calculate the area and perimeter of a circle. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

Step 1: start
Step 2: read value for radius

Step 3: set perimeter = 2.0 * 3.14159 * r

Step 4: set area = (2 * 3.14159 * (r * r))

Step 5: write perimeter

Step 6: write area

Step 7: stop

9
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program

#include<stdio.h>

#include<conio.h>

void main()

float r, area, peri;

clrscr();

printf("Enter any radius: \n");

scanf("%f", &r);

area = 3.14*r*r;

peri = 2*3.14*r;

printf("Area is %f \n", area);

printf("Perimeter is %f \n", peri);

getch();

Output:
Enter any radius:

Area is 78.500000
10
Page

Perimeter is 31.400000

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Exercise:

1. Calculating the area and perimeter of a square.


2. Calculating the area and perimeter of a rectangle.

11
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

6. Finding the maximum of three given integers by using conditional operator.

Aim:

To Find the maximum of three given integers by using conditional operator. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.

Algorithm:

Step 1: start

Step 2: read a, b, c

Step 3: set max = a>b?(a>c?a:c):(b>c)?b:c

Step 4: write max

Step 5: stop

12
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>

#include<conio.h>

void main()

int a, b, c, max;

clrscr();

printf("Enter any three integers: \n");

scanf("%d%d%d", &a, &b, &c);

max = a>b?(a>c?a:c):(b>c)?b:c;

printf("Max is %d", max);

getch();

Output:

Enter any three integers:

24 76 48

Max is 76

Exercise:

1. Finding the maximum of three given integers by using ifelse.


13
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

7. Checking the given integer number is even or odd.

Aim:

To check whether the given integer number is even or odd. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:
Step 1: start
Step 2: read n

Step 3: if(n%2 = = 0) then write even else write odd

Step 4: stop

14
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

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

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

8. Checking whether the given year is leap year or not.

Aim:
To check whether the given year is leap year or not. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start

Step 2: read year

Step 3: if (year % 4 = 0 and year % 100 = 0 or year % 400 = 0) then write "Leap Year" else write
"not leap year"

Step 4: stop

16
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year: \n");
scanf("%d", &year);
if(((year % 4) = = 0) && ((year % 100) != 0) || ((year % 400) = = 0))
printf("The year %d is leap year", year);
else
printf("The year %d is not a leap year", year);
getch();
}
Output:
Enter the year:
1884
The year 1884 is leap year.

17
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

9. Calculating the total, average and grade of a student.


Aim:
To Calculate the total, average and grade of a student. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read m1, m2, m3

Step 3: set tot = m1+m2+m3

Step 4: set avg = tot / 3

Step 5: if(m1 < 40 or m2 < 40 or m3 < 40) then set grade = F else goto step 6

Step 6: if(avg<50) then set grade = E else goto step 7

Step 7: if(avg<60) then set grade = D else goto step 8

Step 8: if(avg<70) then set grade = C else goto step 9

Step 9: if(avg<80) then set grade = B else goto step 10

Step 10: set grade = A

Step 11: write tot

Step 12: write avg

Step 13: write grade

Step 14: stop


18
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3, tot;
float avg;
char gr;
clrscr();
printf("Enter the marks of 3 subjects: \n");
scanf("%d%d%d", &m1, &m2, &m3);
tot = m1+m2+m3;
avg = (float)(m1+m2+m3) / 3;
if(m1 < 40 || m2 < 40 || m3 < 40)
gr = 'F';
else if(avg < 50)
gr = 'E';
else if(avg < 60)
gr = 'D';
else if(avg < 70)
gr = 'C';
else if(avg < 80)
gr = 'B';
else
gr = 'A';
printf("Total = %d \n", tot);
19

printf("Average = %f \n", avg);


printf("Grade = %c \n", gr);
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

getch();
}
Output:
Enter the marks of 3 subjects:
89 76 57
Total = 222
Average = 74.000000
Grade = B

20
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

10. Printing name of given month using switch case


Aim:
To Print the name of a given month using switch case. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1:start

Step 2: read m

Step 3: use switch case to display the months

Step 4: if(m = = 1) then write "January" else goto step 5

Step 5: if(m = = 2) then write "February" else goto step 6

Step 6: if(m = = 3) then write "march" else goto step 7

Step 7: if(m = = 4) then write "April" else goto step 8

Step 8: if(m = = 5) then write "May" else goto step 9

Step 9: if(m = = 6) then write "June" else goto step 10


Step 10: if(m = = 7) then write "July" else goto step 11

Step 11: if(m = = 8) then write "August" else goto step 12

Step 12: if(m = = 9) then write "September" else goto step 13

Step 13: if(m = = 10) then write "October" else goto step 14

Step 14: if(m = = 11) then write "November" else goto step 15

Step 15: if(m = = 12) then write "December" else goto step 16
21

Step 16: write "invalid month"


Page

Step 17: stop


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
printf("Enter any month number: \n");
scanf("%d", &m);
switch(m)
{
case 1: printf("January \n");
break;
case 2: printf("February \n");
break;
case 3: printf("March \n");
break;
case 4: printf("April \n");
break;
case 5: printf("May \n");
break;
case 6: printf("June \n");
break;
case 7: printf("July \n");
break;
case 8: printf("August \n");
22

break;
case 9: printf("September \n");
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

break;
case 10: printf("October \n");
break;

case 11: printf("November \n");


break;
case 12: printf("December \n");
break;
default: printf("Invalid Month \n");
}
getch();
}
Output:
Enter any month number:
12
December
Output:
Enter any month number:
5
May
Output:
Enter any month number:
19
Invalid Month
Exercise:
1. Printing name of given month using if.else ladder
23
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

11. Finding the sum of natural numbers up to given N using while loop.
Aim:
To write a program to sum of natural numbers up to given N using while. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.
Algorithm:
Step 1: start
Step 2: read value for n, i
Step 3: if(i < = n) then goto step 4
Step 4: evaluate & set sum = sum + i goto step 5
Step 5: evaluate & set i = i + i goto step 3
Step 4: display value of sum
Step 5: stop

24
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

Output:
Enter the value of n:
150
Sum is 11325
Exercise:
1. Finding the sum of natural numbers up to given N using for loop.
25
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

12. Finding factorial of the given number.


Aim:
To find the factorial of a given number. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:

Step 1: start

Step 2: set fact = 1 and i = 1


Step 3: read value for n
Step 4: if(i <= n) then goto step 5 else goto step 7
Step 5: set fact = fact * i
Step 6: set i = i + 1 and goto step 4
Step 7: write fact
Step 8: stop

26
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
long fact = 1;
clrscr();
printf(Enter n integer: \n);
scanf(%d, &n);
for(i = 1;i <= n;i++)
fact = fact * i;
printf("Factorial is %ld \n", fact);
getch();
}
Output:
Enter an integer:
5
Factorial is 120

27
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

13. Printing fibonacci series up to the given value.


Aim:
To print the fibonacci series up to the given value. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: read value for n
Step 2: set a = 0 and b = 1
Step 3: write a and b
Step 4: compute c = a + b
Step 5: if(c <= n) then write c goto step 6 else goto step 9
Step 6: set a = b and b = c
Step 7: set c = a + b and goto step5
Step 8: stop

28
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int a = 0, b = 1, c;
clrscr();
printf("Enter the n value: \n");
scanf("%d", &n);
printf(%d \t %d \t, a, b);
c = a+b;
while(c <= n)
{
printf("%d", c);
a = b;
b = c;
c = a+b;
}
getch();
}
Output:
enter the n value
8
0 1 1 2 3 5 8
Exercise:
1. Printing fibonacci series within the given range using functions.
29
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

14. Printing the given Nth mathematical table.


Aim:
To print the mathematical table for the given number. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read the values for n & set i = 1
Step 3: if(i < n ) then compute n * i and print the value
Step 4: set i = i + 1 and goto step 3
Step 5: else goto step 6
Step 6: stop

30
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

Output:
Enter n:
4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
31
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

15. Finding the maximum and minimum of given set of numbers.

Aim:

To find the maximum and minimum of given set of numbers. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:
Step 1: start
Step 2: read n
Step 3: set i = 0
Step 4: if(i < n) then goto step 5 else goto step 7
Step 5: read a[i]
Step 6: set i = i + 1 and goto step 4
Step 7: set max = a[0], min = a[0] and i = 1
Step 8: if(i < n) then goto step 9 else goto step 11
Step 9: if(a[i] > max) then max = a[i] else goto step 7

Step 10: if(a[i] < min) then min = a[i];

Step 11: write max and min


Step 12: stop

32
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[50], i, n, max, min;

clrscr();

printf(Enter size of the array: \n);

scanf(%d, &n);

printf(Enter elements in the array: \n);

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

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

max = a[0];

max = a[0];

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

{
33

if(a[i] > max)


Page

max = a[i];
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

if(a[i] < min)

min = a[i];

printf(Maximum element = %d \n, max);

printf(Minimum element = %d \n, min);

getch();

Output:

Enter size of the array:

Enter elements in the array:

78 45 67 23 14 49

Maximum element = 78

Minimum element = 14

34
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

16. Finding the roots of a given Quadratic equation.

Aim:
To find the roots of a given quadratic equation. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start

Step 2: read coefficients of quadratic equations as a, b and c

Step 3: if (a = = 0) then write "Value of a cannot be 0"

Step 4: set d = b * b - 4 * a * c

Step 5: if( d > 0) then go to step 6 else go to step 10

Step 6: set r1 = (-b + sqrt(d)) / 2 * a;

Step 7: set r2 = (-b - sqrt(d)) / 2 * a;

Step 8: write r1 and r2

Step 9: go to step 18

Step 10: if(disc = = 0) then go to step 11 else go to step 15

Step 11: r1 = -b / (2 * a);

Step 12: r2 = -b / (2 * a);

Step 13: write r1 and r2

Step 14: go to step 18

Step 15: r1 = -b / (2 * a);


35
Page

Step 16: r2 = sqrt(4 * a * c - (b * b)) / (2 * a);

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Step 17: write r1 and r2

Step 18: Stop

36
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, r1, r2, rp, ip;
clrscr();
printf("Enter a b c values: \n");
scanf("%f%f%f", &a, &b, &c);
if(a = = 0)
printf(" not a QE");
else
{
d = b*b-4*a*c;
if(d >= 0)
{
printf("Roots are real \n");
r1 = (-b + sqrt(d)) / (2*a);
r2 = (-b - sqrt(d)) / (2*a);
printf("Roots are %f and %f \n", r1, r2);
}
else
{
printf("Roots are imaginary \n");
rp = -b / (2*a);
ip = sqrt(-d) / (2*a);
37

printf("Roots are (%f , %f) and (%f , %f) \n", rp, ip, rp, -ip);
Page

}
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

}
getch();
}
Output:
Enter a b c values:
3 5 2
Roots are real
Roots are -0.666667 and -1.000000
Output:
Enter a b c values:
4 2 8
Roots are imaginary
Roots are (-0.250000, 1.391941) and (-0.250000, -1.391941)

38
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Sin x and Cos x values using series expansion.

17. Sin x value using series expansion.

Aim:
To find the value of sin x using series expansion. For input & output statements we include
the header file <stdio.h>, for arithmetic operations include the <math.h> and for clear the screen
include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read the values for n & x
Step 3: compute x = (x * 3.14) / 180
Step 4: set sum = x
Step 5: set term = x
Step 6: if(i <= n) then goto step 7
Step 7: compute & set term = (term * (-1) * x * x) / ((2 * i) * (2 * i + 1))
Step 8: compute & set sum = sum + term
Step 9: compute i = i + 1 then goto step 6
Step10: display the value of sum
Step11: stop

39
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i, n;
clrscr();
printf("Enter the no of terms: \n");
scanf("%d", &n);
printf("Enter the angle in degrees x: \n");
scanf("%f", &x);
x = (x*3.14) / 180;
sum = x;
term = x;
for(i = 1;i <= n;i++)
{
term = (term*(-1)*x*x) / ((2*i)*(2*i+1));
sum+ = term;
}
printf("Sin valve of given angle is %f", sum);
getch();
}
Output:
Enter the no of terms:
3
40

Enter the angle in degrees x:


Page

30
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Sin valve of given angle is 0.499770

41
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

18. Cos x value using series expansion.

Aim:
To find the value of cos x using series expansion. For input & output statements we include
the header file <stdio.h>, for arithmetic operations include the <math.h> and for clear the screen
include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read the values for n & x
Step 3: compute x = (x * 3.14) / 180
Step 4: set sum = 1
Step 5: set term = 1
Step 6: if(i <= n) then goto step 7
Step 7: compute & set term = (term * (-1) * x * x) / ((2 * i) * (2 * i - 1))
Step 8: compute & set sum = sum + term
Step 9: compute i = i + 1 then goto step 6
Step10: display the value of sum
Step11: stop

42
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i, n;
clrscr();
printf("Enter the no of terms: \n");
scanf("%d", &n);
printf("Enter the angle in degrees x: \n");
scanf("%f", &x);
x = (x*3.14) / 180;
sum = 1;
term = 1;
for(i = 1;i <= n;i++)
{
term = (term*(-1)*x*x) / ((2*i)*(2*i-1));
sum+ = term;
}
printf("Cos valve of given angle is %f", sum);
getch();
}
Output:
Enter the no of terms:
3
43

Enter the angle in degrees x:


Page

30
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Cos valve of given angle is 0.866158

44
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

19. Finding the sum of digits of a given number


Aim:
To find the sum of digits of a given number. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: set sum = 0
Step 3: read value for n
Step 4: set temp = n

Step 5: set rem = n % 10


Step 6: set sum = sum + rem
Step 7: set n = n / 10
Step 8: if(n != 0) goto step 4 else goto step 8
Step 9: write sum
Step 10: stop process

45
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, temp, rem, sum = 0;
clrscr();
printf("Enter a number: \n");
scanf("%d", &n);
temp = n;
while(n != 0)
{
rem = n % 10;
sum += rem;
n = n / 10;
}
printf("The sum of digits of %d is %d \n", temp, sum);
getch();
}
Output:
Enter a number:
234
The sum of digits of 234 is 9

46
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

20. Checking whether given number is palindrome or not.


Aim:
To check whether given number is palindrome or not. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read n and i

Step 3: set temp = n and rev = 0

Step 4: if(temp != 0) then goto step 5 else goto step 10

Step 5: compute & set rem = temp % 10

Step 6: compute & set rev = rev * 10 + rem

Step 7: compute & set n = n / 10

Step 8: goto step 4

Step 9: if(temp = = rev) then write palindrome else not palindrome


Step 10: stop

47
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev = 0, temp, rem;
clrscr();
printf("enter a number: \n");
scanf("%d", &n);
temp = n;
while(n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if(temp = = rev)
printf("The number %d is a palindrome \n", temp);
else
printf("The number %d is not a palindrome\n", temp);
getch();
}

Output:
Enter a number:
121
The number 121 is a palindrome
48
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Conversion of Binary to Decimal, Octal, Hexa and Vice Versa.

21. Conversion of Binary to Decimal.

Aim:

To convert binary to decimal. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read binary

Step 3: set decimal = 0 and j = 1

Step 4: if(binary != 0) then goto step 5 else goto step 11

Step 5: compute & set rem = binary % 10

Step 6: compute & set decimal = decimal + rem * j

Step 7: compute & set j = j * 2

Step 8: compute & set binary = binary / 10

Step 9: goto step 4

Step 10: write decimal

Step 11: stop

49
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, decimal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

decimal = decimal+rem*j;

j = j*2;

binary = binary / 10;

printf("Equivalent decimal value: %ld", decimal);

getch();

}
50
Page

Output:

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter any number any binary number:

1101

Equivalent decimal value: 13

51
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

22. Conversion of Decimal to Binary.

Aim:

To convert decimal to binary. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read decimal

Step 3: set i = 1 and q = decimal

Step 4: if(q != 0) then goto step 5 else goto step 11

Step 5: compute & set binary[i++] = q % 2

Step 6: compute & set q = q / 2

Step 7: goto step 4

Step 8: set j = i - 1

Step 9: if(j > 0) then write binary else goto step 11

Step 10: compute j = j 1 then goto step 9

Step 11: stop

52
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>

#include<conio.h>

void main()

long int decimal, rem, q;

int binary[100], i = 1, j;

clrscr();

printf("Enter any decimal number: \n");

scanf("%ld", &decimal);

q = decimal;

while(q != 0)

binary[i++] = q % 2;

q = q / 2;

printf("Equivalent binary value of decimal number %d: ", decimal);

for(j = i - 1 ;j > 0;j--)

printf("%d", binary[j]);
53
Page

getch();

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:
Enter any decimal number:

50

Equivalent binary value of decimal number 50: 110010

54
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

23. Conversion of Binary to Octal.

Aim:

To convert binary to octal. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read binary

Step 3: set octal = 0 and j = 1

Step 4: if(binary != 0) then goto step 5 else goto step 11

Step 5: compute & set rem = binary % 10

Step 6: compute & set octal = octal + rem * j

Step 7: compute & set j = j * 2

Step 8: compute & set binary = binary / 10

Step 9: goto step 4

Step 10: write octal

Step 11: stop

55
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, octal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

octal = octal+rem*j;

j = j*2;

binary = binary / 10;

printf("Equivalent octal value: %lo", octal);

getch();

}
56
Page

Output:

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter any number any binary number:

1101

Equivalent octal value: 15

57
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

24. Conversion of Octal to Binary.

Aim:

To convert octal to binary. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

#define MAX 1000

void main()

char octal[MAX];

long int i = 0;

clrscr();

printf("Enter any octal number: \n");

scanf("%s", octal);

printf("Equivalent binary value: \n");

while(octal[i])

switch(octal[i])
58

{
Page

case '0': printf("000");


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

break;

case '1': printf("001");

break;

case '2': printf("010");

break;

case '3': printf("011");

break;

case '4': printf("100");

break;

case '5': printf("101");

break;

case '6': printf("110");

break;

case '7': printf("111");

break;

default: printf("Invalid octal digit %c ", octal[i]);

i++;
59

}
Page

getch();
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:
Enter any octal number:

123

Equivalent binary value:

001010011

60
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

25. Conversion of Binary to Hexadecimal.

Aim:

To convert binary to hexadecimal. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read binary

Step 3: set hexadecimal = 0 and j = 1

Step 4: if(binary != 0) then goto step 5 else goto step 11

Step 5: compute & set rem = binary % 10

Step 6: compute & set hexadecimal = hexadecimal + rem * j

Step 7: compute & set j = j * 2

Step 8: compute & set binary = binary / 10

Step 9: goto step 4

Step 10: write hexadecimal

Step 11: stop

61
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, hexadecimal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

hexadecimal = hexadecimal+rem*j;

j = j*2;

binary = binary / 10;

printf("Equivalent hexadecimal value: %lX", hexadecimal);

getch();

}
62
Page

Output:

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter any number any binary number:

1101

Equivalent hexadecimal value: D

63
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

26. Conversion of Hexadecimal to Binary.

Aim:

To convert hexadecimal to binary. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

#define MAX 1000

void main()

char binary[MAX], hexadecimal[MAX];

long int i = 0;

clrscr();

printf("Enter any hexadecimal number: \n");

scanf("%s", hexadecimal);

printf("Equivalent binary value: \n");

while(hexadecimal[i])

switch(hexadecimal[i])
64

{
Page

case '0': printf("0000");


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

break;

case '1': printf("0001");

break;

case '2': printf("0010");

break;

case '3': printf("0011");

break;

case '4': printf("0100");

break;

case '5': printf("0101");

break;

case '6': printf("0110");

break;

case '7': printf("0111");

break;

case '8': printf("1000");

break;

case '9': printf("1001");


65

break;
Page

case 'A': printf("1010");


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

break;

case 'B': printf("1011");

break;

case 'C': printf("1100");

break;

case 'D': printf("1101");

break;

case 'E': printf("1110");

break;

case 'F': printf("1111");

break;

case 'a': printf("1010");

break;

case 'b': printf("1011");

break;

case 'c': printf("1100");

break;

case 'd': printf("1101");


66

break;
Page

case 'e': printf("1110");


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

break;

case 'f': printf("1111");

break;

default: printf("Invalid hexadecimal digit %c ", hexadecimal[i]);

i++;

getch();

Output:
Enter any hexadecimal number:

2AD5

Equivalent binary value:

0010101011010101

67
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Generating a Pascal Triangle and Pyramid of Numbers.

27. Generating a Pascal Triangle.

Aim:

To print pascal triangle. For input & output statements we include the header file <stdio.h>
and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

long fact(int);

void main()

int n, i, j;

clrscr();

printf("Enter the no. of lines: \n");

scanf("%d", &n);

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

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

printf(" ");
68

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


Page

printf("%ld ", fact(i) / (fact(j)*fact(i-j)));


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

printf("\n");

getch();

long fact(int num)

long f = 1;

int i = 1;

while(i <= num)

f = f*i;

i++;

return f;

69
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:
Enter the no. of lines:

1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

70
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

28. Generating a Pyramid of Numbers (Floyds Triangle).

Aim:

To print Pyramid of Numbers (Floyds Triangle). For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, r, k = 1;
clrscr();
printf("Enter the range: \n");
scanf("%d", &r);
printf("FLOYD'S TRIANGLE: \n \n");
for(i = 1;i <= r;i++)
{
for(j = 1;j <= i;j++,k++)
printf(" %d", k);
printf("\n");
}
getch();
}

71
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:
Enter the range:
10
FLOYD'S TRIANGLE

1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 7 48 49 50 51 52 53 54 55

72
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Recursion: Factorial, Fibonacci, GCD.

29. Recursion: Factorial.

Aim:

To find factorial of a given number using recursion. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

int num,f;

clrscr();

printf("Enter a number: \n");

scanf("%d", &num);

f = fact(num);

printf("Factorial of %d is: %d", num, f);

getch();

}
73

int fact(int n)
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

if(n = = 1)

return 1;

else

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

Output:
Enter a number:

Factorial of 5 is: 120

74
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

30. Recursion: Fibonacci.

Aim:

To print fibonacci series using recursion. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void fibonacci(int);

void main()

int k, n;

long int i = 0, j = 1, f;

clrscr();

printf("Enter the range of the Fibonacci series: \n");

scanf("%d", &n);

printf("Fibonacci Series: \n");

printf("%d %d ", i, j);

fibonacci(n);

getch();
75

}
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

void fibonacci(int n)

static long int first = 0, second = 1, sum;

if(n > 0)

sum = first + second;

first = second;

second = sum;

printf("%ld ", sum);

fibonacci(n-1);

Output:
Enter the range of the Fibonacci series:

10

Fibonacci Series:

0 1 1 2 3 5 8 13 21 34 55 89 76
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

31. Recursion: GCD.

Aim:

To find gcd of a number using recursion. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
int findgcd(int, int)
void main()
{
int n1, n2, gcd;
clrscr();
printf("Enter two numbers: \n");
scanf("%d %d", &n1, &n2);
gcd = findgcd(n1, n2);
printf("GCD of %d and %d is: %d \n", n1, n2, gcd);
getch();
}
int findgcd(int x, int y)
{
while(x != y)
{
if(x > y)
return findgcd(x-y, y);
else
return findgcd(x, y-x);
77

}
Page

return x;
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

}
Output:
Enter two numbers:

366

60

GCD of 366 and 60 is:

78
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Matrix Addition and Multiplication using Arrays.

32. Matrix Addition using Arrays.

Aim:

To perform addition of two matrices. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:

Step 1: start

Step 2: read the values for m,n,p,q

Step 3: if(m!=p || n!=q) then goto step 4 else goto step

Step 4: print addition is not possible

Step 5: initialize I=0,j=0

Step 6: if(I<m) then goto step 7

Step 7: if(j<n)then goto step 8


Step 8: read a[I][j] goto step 9

Step 9: set I=I+1,j=j+1

Step 10: if(I<p) then goto step 11

Step 11: if(j<q)then goto step 12

Step 12: read b[I][j] goto step 9

Step 13: compute & set c[i][j] = a[i][j] + b[i][j]


79

Step 14: diplay the value of c[I][j] goto step 9


Page

Step 15: stop


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

80
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

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

int a[5][5], b[5][5], c[5][5], i, j, n, m, p, q;

clrscr();

printf("Enter first matrix size: \n");

scanf("%d%d", &m, &n);

printf("Enter second matrix size: \n");

scanf("%d%d", &p, &q);

if(m != p || n != q)

printf("Size mismatch, Addition is not possible \n");

else

printf("Enter first matrix elements: \n");

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

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

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

printf("Enter second matrix elements: \n");


Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

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

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

printf("Addition of two Matrices is \n");

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

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

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

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

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

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

printf("\n");

getch();

}
82

Output:
Page

Enter first matrix size:


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter second matrix size:

Enter first matrix elements:

Enter second matrix elements:

2
83

1
Page

Addition of two Matrices is


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

7 7 7

7 7 7

Output:

Enter first matrix size:

Enter second matrix size:

Size mismatch, Addition is not possible

84
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

33. Matrix Multiplication using Arrays.


Aim:

To multiply two matrices using arrays. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:

Step 1: start

Step 2: read the values for m,n,p,q

Step 3: if n!=p) then goto step 4 else goto step 6

Step 4: print multiplication is not possible

Step 5: initialize I=0,j=0

Step 6: if(I<m) then goto step 7

Step 7: if(j<n)then goto step 8


Step 8: read a[I][j] goto step 9

Step 9: set I=I+1,j=j+1

Step 10: if(I<p) then goto step 11

Step 11: if(j<q)then goto step 12

Step 12: read b[I][j] goto step 9

Step 13:if((I<m) &&(j<q) )then set c[I][j]=0 else goto step 9

Step 13: compute & set c[i][j] = c[I][j]+ a[i][k] * b[k][j]

Step 14: diplay the value of c[I][j] goto step 9


85
Page

Step 15: stop

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
void main()

int a[5][5], b[5][5], c[5][5], i, j, n, m, p, q;

clrscr();

printf("Enter first matrix size: \n");

scanf("%d%d", &m, &n);

printf("Enter second matrix size: \n");

scanf("%d%d", &p, &q);

if(n != p)

printf("Not compatible, Multiplication is not possible \n");

else

printf("Enter first matrix elements: \n");

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

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

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

printf("Enter second matrix elements: \n");


Page

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


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

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

printf("Product of two Matrices is \n");

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

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

c[i][j] = 0;

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

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

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

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

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

printf("\n");

}
87

}
Page

getch();
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:

Enter first matrix size:

Enter second matrix size:

Not compatible, Multiplication is not possible

Output:

Enter first matrix size:

Enter second matrix size:

1
88
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter first matrix elements:

Enter second matrix elements:

2
3

Product of two Matrices is


14

32

89
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Bubble Sort, Selection Sort.

34. Bubble Sort.

Aim:
To implement bubble sort. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

step 1: start

step 2: read n

step 3: set i=0

step 4: if(i<n) then goto step 5 else goto step 7

step 5:read a[i]


step 6: set i=i+1 and goto step 4

step 7:set i=0

step 8:if(i<n-1) then goto step 9

step 9:set j=i+1 then goto step 10 else go to step 14

step 10:if(j<n) then goto step 11else goto step 14

step 11:if(a[i]>a[j]) then goto step 12

step 12:set temp=a[i]

a[i]=a[j]
90

a[j]=temp
Page

step 13:set j=j+1 and goto step 10


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

step 14:set i=i+1 and goto step 8

step 15:set i=0

step 16: if(i<n) then goto step 17 else goto step 19

step 17:write a[i]

step 18:set i=i+1 and goto step 16

step 19:stop process

91
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, j, n, temp;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

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

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

if(a[j] > a[j+1])

{
92

temp = a[j];
Page

a[j] = a[j+1];
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

a[j+1] = temp;

printf("After sorting, the element are \n");

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

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

getch();

Output:

Enter array size:

Enter any 6 elements:

78 95 62 30 12 39

After sorting, the element are

12 30 39 62 78 95
93
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

35. Selection Sort.

Aim:

To implement selection sort. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, j, n, temp, maxloc, k;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

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

maxloc = 0;
94
Page

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

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

{
if(a[j] > a[maxloc])

maxloc = j;

temp = a[n-1-i];

a[n-1-i] = a[maxloc];

a[maxloc] = temp;

printf("After sorting, the element are \n");

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

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

getch();

Output:

Enter array size:

Enter any 6 elements:

78 95 62 30 12 39

After sorting, the element are


95
Page

12 30 39 62 78 95

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program on Linear Search and Binary Search using Recursive and Non Recursive
Procedures.
36. Program on Linear Search using Recursive Procedure.
Aim:

To implement linear search using recursive procedure. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

int linear(int[], int, int);

void main()

int a[100], n, i, x;

clrscr();

printf("Enter Size: \n");

scanf("%d", &n);

printf("Enter %d elements: \n", n);

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

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

printf("Enter element to search: \n");


96
Page

scanf("%d", &x);

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

i=linear(a, n-1, x);

if(i != -1)

printf("The element %d found at %d location", x, i);

else

printf("The element %d is not found", x);

getch();

int linear(int a[100], int n, int x)

if(n<= 0)

return -1;

if(a[n] = = x)

return n;

else

return linear(a, n-1, x);

Output:

Enter Size:
97

5
Page

Enter 5 elements:
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

25 30 12 54 60

Enter element to search:

60

The element 60 found at 4 location

98
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

37. Program on Linear Search using Non - Recursive Procedure.

Aim:

To implement linear search using non recursive procedure. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

step 1: start

step 2: read n

step 3: set i=0

step 4: if(i<n) then goto step 5 else goto 7

step 5: read a[i]


step 6: set i=i+1 and goto step 4

step 7: read ele

step 8: if(ele==a[i]) then set check=1 goto step 9 else goto step 10

step 9: if(check) then write element is found else write element not

found

step 10:stop process

99
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>
#include<conio.h>

void main()

int a[20], i, n, x, xloc = -1;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

printf("Enter the element you want to search for \n");

scanf("%d", &x);

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

if(a[i] = = x)

{
100

xloc = i;
Page

break;

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

if(xloc = = -1)

printf("%d is not found in the array \n", x);

else

printf("%d is found at location %d \n", x, xloc);

getch();

Output:

Enter array size

Enter any 6 elements

78 45 67 23 14 49

Enter the element you want to search for

23

23 is found at location 3

Output:

Enter array size


101

5
Page

Enter any 5 elements


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

1 2 34 5 6

Enter the element you want to search for

89

89 is not found in the array

102
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

38. Program on Binary Search using Recursive Procedure.


Aim:

To implement binary search using recursive procedure. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define size 10

int binary(int[], int, int, int);

void main()

int n, i, key, position;

int low, high, list[size];

clrscr();

printf("\nEnter the total number of elements: \n");

scanf("%d", &n);

printf("Enter the elements of list: \n");

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


103

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

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

low = 0;

high = n - 1;

printf("Enter element to be searched: \n");

scanf("%d", &key);

position = binary(list, key, low, high);

if(position != -1)

printf("Number present at %d \n", position);

else

printf("The number is not present in the list \n");

getch();

int binary(int a[], int x, int low, int high)

int mid;

if (low > high)

return -1;

mid = (low + high) / 2;

if (x = = a[mid])
104

return mid;
Page

else if (x < a[mid])


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

return binary(a, x, low, mid - 1);

else

return binary(a, x, mid + 1, high);

Output:

Enter the total number of elements:

Enter the elements of list:

11 22 33 44 55

Enter element to be searched:

33

Number present at 2

105
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

39. Program on Binary Search using Non - Recursive Procedure.


Aim:

To implement binary search using non recursive procedure. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Algorithm:

step 1: start

step 2: read n

step 3: set i=0

step 4: if(i<n) then goto step 5 else goto 7

step 5: read a[i]


step 6: set i=i+1 and goto step 4

step 7: read ele

step 8: set low=0 high=n-1 and check=0

step 9: if(low<=high) then goto step 10 else goto step 14

step 10:set mid=(low+high)/2

step 11: if(ele==a[mid-1]) then set check=1 goto step else goto STEP 12

step 12: if(ele<a[mid-1]) then set high=mid-1 else goto step 13

step 13: if(ele>a[mid-1]) then set low=mid+1;

step 14 if(check) then write element is found else write element not
106

found
Page

step 15:stop process


Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, n, x, xloc = -1, low, high, mid;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements in the sorted order: \n",n);

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

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

printf("Enter the element you want to search for \n");

scanf("%d", &x);

low = 0;

high = n-1;

while(low <= high)

{
107

mid = (low+high) / 2;
Page

if(a[mid] = = x)

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

xloc = mid;

break;

else if(x < a[mid])

high = mid - 1;

else

low = mid + 1;

if(xloc = = -1)

printf("%d is not found in the array \n", x);

else

printf("%d is found at location %d \n", x, xloc);

getch();

Output

Enter array size:

6
108

Enter any 6 elements:


Page

78 45 67 23 14 49
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter the element you want to search for

23

23 is found at location 3

Output:

Enter array size:

Enter any 5 elements:

1 2 34 5 6

Enter the element you want to search for

89

89 is not found in the array

109
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

40. Printing all prime numbers in the given range (using function).

Aim:

To Print all prime numbers in the given range (using function). For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>

Algorithm:

Step 1: start

Step 2: read the values for m,n

Step 3: call primerange(m,n)

Step 4: if (i<=n) then goto step 5 else goto 8

Step 5: if(j<=i) then goto step 6

Step 6: if(i % j == 0) then goto step 7

Step 7: set i=i+1 goto step 4

Step 8: if(i==j) then print i

Step 9: stop

110
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>

#include<conio.h>

int main()

int m,n;

void primerange(int,int);

clrscr();

printf("Enter the range\n");

scanf("%d%d",&m,&n);

printf("The prime numbers in the given range are\n");

primerange(m,n);

getch();

return 0;

void primerange(int m,int n)

int i,j;
111

for(i=m;i<=n;i++)
Page

{
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

if(i % j == 0)

break;

if(i==j)

printf("%d\n",i);

Output:

Enter the range

20

The prime numbers in the given range are

5
112

7
Page

11
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

13

17

19

113
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Functions (Library Functions) of String Manipulations.

41. Find the Length of a given String using Library Function.

Aim:

To find the length of a given string using library function. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char arr[ ] = "Methodist College" ;


int len1, len2 ;

clrscr();

len1 = strlen (arr) ;


len2 = strlen ("Engineerimg") ;

printf ("String = %s length = %d \n", arr, len1) ;


printf (String = %s length = %d \n", "Engineerimg", len2) ;

getch();

}
114

Output:
Page

string = Methodist College length = 17

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

string = Engineerimg length = 11

115
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

42. Copy the given String to another String using Library Function.

Aim:

To copy the given string to another string using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char source[ ] = "Methodist College of Engineering & Technology" ;

char target[20] ;

cltscr();

strcpy(target, source) ;
printf("Source String = %s \n", source ) ;
printf("Target String = %s \n", target ) ;

getch();

Output:

Source String = Methodist College of Engineering & Technology


116

Target String = Methodist College of Engineering & Technology


Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

43. Appendding one String at the end of another String using Library Function.

Aim:

To appends one string at the end of another string using library function. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char source[ ] = "Methodist College";

char target[30] = Welcome to;

cltscr();

strcat ( target, source) ;


printf("Source String = %s \n", source);
printf("Target String = %2s \n", target);

getch();

Output:
117

Source String = Methodist College


Page

Target String = Welcome to Methodist College

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

44. Reverse the Contents of the given String using Library Function.

Aim:

To reverse the contents of the given string using library function. For input & output statements
we include the header file <stdio.h>, for clear the screen include the <conio.h> and for string
functions include <string.h>.

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char a[20];

cltscr();

printf(Enter any string: \n);

gets(a);

strrev(a);

printf(Reverse string: \n, a);

getch();

}
118

Output:
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Enter any string: methodist

Reverse string: tsidohtem

119
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

45. Compare the Contents of given two Strings using Library Function.

Aim:

To reverse the contents of the given two strings using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char string1[ ] = "Methodist";

char string2[ ] = MCET;

int i, j, k;

cltscr();

i = strcmp(string1, Methodist);

j = strcmp(string1, string2);

k = strcmp(string1, Methodist College);

printf("i = %d \n", i);


printf("j = %d \n", j);
printf("k = %d \n", k);
120

getch();
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:

i=0

j = 34

k = -32

121
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

46. Abbreviate the Contents of given Strings using Library Function.

Aim:

To abbreviate the contents of the given string using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char s[100];

int i;

clrscr();

printf("Enter a String: \n");

gets(s);

i = 0;

while(s[i] = = ' ')

i++;

putchar(s[i]);
122

putchar(' ');
Page

while(s[i] != '\0')

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

if(s[i] = = ' ' && s[i+1] != ' ')

putchar(s[i+1]);

putchar(' ');

i++;

getch();

Output:

Enter a String:

Methodist College Engineering Technology

MCET

123
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

47. Generating the student records with total, average if marks of 3 subjects are given as inputs
by using an array of student structures.
Aim:

To Generate the student records with total, average if marks of 3 subjects are given as inputs by
using an array of student structures. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>

Algorithm:

Step 1:start

Step 2:read n

Step 3:initialize I=0

Step 4:if (I<=n)then goto step 5

Step 5: read sno,name,m1,m2,m3

Step 6: if(i<n-1) then goto step 5

Step 7: if (I<=n)then goto step 8

Step 8: compute & set tot=m1+m2+m3

Step 9: compute & set avg=tot/3

Step 10: set I=I+1

Step 11: write sno,name,tot,avg goto step 10

Step 12:stop
124
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:

#include<stdio.h>

#include<conio.h>
struct student
{

int sno;

char name[20];

int m1,m2,m3;

int tot;

float avg;

};

main()

struct student s[10];

int n,i;

clrscr();

printf("\n Enter no of students");

scanf("%d",&n);

printf("\n Enter no name and marks of 3 subjects of


125

%d students",n);
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

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

fflush(stdin);

scanf("%d",&s[i].sno);

fflush(stdin);

gets(s[i].name);

fflush(stdin);

scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);

if(i<n-1)

printf("\n next student\n \n");

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

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

s[i].avg=(float)s[i].tot/3;

printf("\n STUDENT REPORTS ARE\n\n\n");

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


126

{
Page

printf("\n \n \n");
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

printf("\n ROLL NO = %d",s[i].sno);

printf("\n NAME = %s",s[i].name);

printf("\n TOTAL = %d",s[i].tot);

printf("\n AVERAGE = %f",s[i].avg);

getch();

Output:

STUDENT REPORTS ARE

ROLL NO = 1001

NAME = rama

TOTAL = 265

AVERAGE = 88.333336

ROLL NO = 1002

NAME = krishna
127

TOTAL = 247
Page

AVERAGE = 82.333336
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

ROLL NO = 1003

NAME = ranga

TOTAL = 190

AVERAGE = 63.333332

128
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

48. Finding the No. of Characters, Words and Lines of given Text File.

Aim:

To find the no. of characters, words and lines of given text file. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int noc = 0, now = 0, nol = 0;

FILE *fw,*fr;

char fname[20], ch;

clrscr();

printf("Enter the source file name: \n");

gets(fname);

fr = fopen(fname, "r");

if(fr = = NULL)

{
129

printf("\n error \n");


Page

exit(0);

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

ch = fgetc(fr);

while(ch != EOF)

noc++;

if(ch = = ' ');

now++;

if(ch = = '\n')

nol++;

now++;

ch = fgetc(fr);

fclose(fr);

printf("Total no. of character = %d \n", noc);

printf("Total no of words = %d \n", now);

printf("Total no of lines = %d \n", nol);


130

getch();
Page

}
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:

Enter the source file name:

namecount.c

Total no. of character = 488

Total no of words = 522

Total no of lines = 34

131
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

File Handling Programs

49. Program to create a file.


Aim:

To create a file. For input & output statements we include the header file <stdio.h> and for
clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("text.dat", "w");
printf("Enter text, to stop press Ctrl Z: \n");
while((ch = getchar()) != EOF)
{
fputc(ch,fp);
}
fclose(fp);
getch();
}
Output:
Enter text, to stop press Ctrl-Z:
This is my first program in C
132

language to create a file.


Bye.
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

To see the contents of this file


open text.dat in any
editor program.
^Z

File Saved on disk

text.dat:
This is my first program in C
language to create a file.
Bye.
To see the contents of this file
open text.dat in any
editor program.

133
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

50. Program to print the contents of the file on the monitor.


Aim:

To print the contents of the file on the monitor. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("text.dat", "r");
if(fp==NULL)
printf("No such file \n");
else
{
printf("File contents are: \n");
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
fclose(fp);
}
134

getch();
}
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Output:
Given text.dat
File contents are:
This is my first program
in C to create a file with some text.
Bye
To see the contents of the file open text.dat
in any editor.
Output:
Given example.dat
No such file

135
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

51. Program to Copy one File in to another.


Aim:

To copy one file in to another. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fs, *ft ;
char ch;
clrscr();
fs = fopen ("text.dat", "r");
if( fs == NULL )
{
puts ("Cannot open source file");
}
ft = fopen ("example.dat", "w");
if(ft == NULL)
{
puts("Cannot open target file");
fclose(fs);
}
while (1)
{
136

ch = fgetc (fs);
if(ch = = EOF)
Page

break;
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

else
{
fputc (ch, ft);
puts("File copied");
}
}
fclose (fs);
fclose (ft);
getch();
}
Output:
File copied

137
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

52. Creating a file of student records

Aim:

To write a program which diplay the student details like Rno,name,address. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>

Algorithm:
Step 1:start
Step2:read n
Step 3:create file student.dat
Step 4: initialize I=0
Step 5: if(I<=n) then goto step 6 else goto step 11
Step 6: read rno,name,addr from keyboard
Step 7: store on disk
Step 8: open student.dat
Step 9: read rno,name,addr from student.dat
Step 10:diplay rno,name,addr
Step 11:stop

138
Page

Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

Program:
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
char addr[30];
};
void main()
{
int i,n;
struct student s;
FILE *fp;
clrscr();
printf("How many records you want to store in the file\n");
scanf("%d",&n);
printf("Enter any %d student records\n",n);
printf("For each record enter rollnumber,name and addr\n");
fp = fopen("student.dat","wb");
for(i=1;i<=n;i++)
{
printf("enter record\n");
scanf("%d",&s.rno);
fflush(stdin);
gets(s.name);
139

scanf("%d",&s.addr);
fwrite(&s,sizeof(s),1,fp);
Page

printf(rollno=%d\n\nname=%s\n\naddress=%s);
Department of CSE

Methodist College of Engineering


Computer Programming Lab Manual

}
fclose(fp);
printf("file created\n");
getch();
}

Output:
How many records you want to store in the file
2
Enter any 2 student records
For each record enter rollnumber,name and addr
enter record
rllno=12
name=rama rao
address=hyderabad

enter record
rollno=20
name=sita kumari
address=ameerpet
file created

140
Page

Department of CSE

Methodist College of Engineering

S-ar putea să vă placă și