DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
I B. Tech I Sem ECE (R23)
C PROGRAMMING FOR ENGINEERS LABORATORY
LAB MANUAL
Academic Year 2024-2025
Verified by HOD-CSE
R23 B.Tech. ECE Syllabus JNTU HYDERABAD
C PROGRAMMING FOR ENGINEERS LABORATORY
B.Tech. I Year I Sem. LTPC
0 02 1
Course Outcomes: Upon completing this course, the students will be able to
1. Write algorithms and to draw flowcharts for solving problems and translate the
algorithms/flowcharts to programs (in C language).
2. Use functions to develop modular reusable code.
3. Use arrays, pointers, strings and structures to formulate algorithms and programs.
4. Understand Searching and sorting algorithms
List of Experiments:
1. Write a C program to find the sum of individual digits of a positive integer.
2. Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0
and
Subsequent terms are found by adding the preceding two terms in the sequence.
Write a C program to generate the first n terms of the sequence.
3. Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
4. Write a C program to find the roots of a quadratic equation.
5. Write a C program to find the factorial of a given integer.
6. Write a C program to find the GCD (greatest common divisor) of two given integers.
7. Write a C program to solve Towers of Hanoi problem.
8. Write a C program, which takes two integer operands and one operator from the user, per-
forms the operation and then prints the result. (Consider the operators +,-,*, /, % and use
Switch Statement)
9. Write a C program to find both the largest and smallest number in a list of integers.
10. Write a C program that uses functions to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrices
11. Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) To delete n Characters from a given position in a given string.
12. Write a C program to determine if the given string is a palindrome or not
13. Write a C program that displays the position or index in the string S where the string T be-
gins, or 1 if S doesn’t contain T.
14. Write a C program to count the lines, words and characters in a given text.
15. Write a C program to generate Pascal’s triangle.
16. Write a C program to construct a pyramid of numbers
17. Write a C program to read in two numbers, x and n, and then compute the sum of this geo-
metric progression: 1+x+x2+x3+………….+xn
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum
Perform error checking.
For example, the formula does not make sense for negative exponents – if n is less than 0
Have your program print an error message if n<0, then go back and read in the next pair of
numbers of without computing the sum. Are any values of x also illegal ? If so, test for them
too.
18. 2’s complement of a number is obtained by scanning it from right to left and complementing
all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a
C program to find the 2’s complement of a binary number.
19. Write a C program to convert a Roman numeral to its decimal equivalent.
20. Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
21.
i. Write a C program which copies one file to another.
ii. Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)
22.
i. Write a C program to display the contents of a file.
ii. Write a C program to merge two files into a third file (i.e., the contents of the first
file followed by those of the second are put in the third file)
23. Write a C program that implements the following sorting methods to sort a given list of in-
tegers in ascending order i) Bubble sort ii) Selection sort iii)Insertion sort
24. Write C programs that use both recursive and non recursive functions to perform the follow-
ing searching operations for a Key value in a given list of integers:
i) Linear search ii) Binary search
1. Write a C program to find the sum of individual digits of a positive integer
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
printf("enter a +ve integer"); // enter a integer value
scanf("%d",&n);
while(n>0) // checks the condition
{
sum=sum+n%10; // sum + remainder value
n=n/10;
}
printf("sum of individual digits of a positive integer is %d",sum); // prints the sum of individual
digits
}
OUTPUT:
enter a +ve integer456
sum of individual digits of a positive integer is 15
2. Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0
and 1. Subsequent terms are found by adding the preceding two terms in the sequence.. Write
a C program to generate the first n terms of the sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 0, b = 1, lengthOfSeries = 0, counter, sum = 0;
clrscr();
printf("Enter the length of series \n ");
scanf("%d", &lengthOfSeries);
printf("Fibonacci series\n");
printf("%d %d", a, b);
for(counter = 2; counter <lengthOfSeries; counter++)
{
sum = a + b;
printf(" %d",sum);
a = b;
b = sum;
}
getch();
}
OUTPUT:
Enter the length of series
15
Fibonacci series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377.
3. Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i<= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}
OUTPUT:
Prime no. series
Enter any number
10
The prime numbers between 1 to 10
2 3 5 7
4. Write a C program to find the roots of a quadratic equation.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
5. Write a C program to find the factorial of a given integer.
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
OUTPUT:
Enter a number: 5
Factorial of 5 is: 120
Factorial of 5 is: 120
6. Write a C program to find the GCD (greatest common divisor) of two given integers
// C++ program to find GCD of two numbers
#include <iostream>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
int result = min(a, b); // Find Minimum of a and b
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result; // return gcd of a and b
}
// Driver program to test above function
int main()
{
int a = 98, b = 56;
cout<< "GCD of " << a << " and " << b << " is "
<<gcd(a, b);
return 0;
}
OUTPUT:
GCD of 98 and 56 is 14
7. Write a C program to solve Towers of Hanoi problem.
#include <stdio.h>
// Tower of Hanoi program in C using Recursion
void toH(int n, char rodA, char rodC, char rodB)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c",rodA ,rodC );
return;
}
toH(n-1, rodA, rodB, rodC);
printf("\n Move disk %d from rod %c to rod %c", n, rodA, rodC);
toH(n-1, rodB, rodC,rodA);
}
int main()
{
int no_of_disks ;
printf("Enter number of disks: ");
scanf("%d", &no_of_disks);
toH(no_of_disks, 'A','C','B');
return 0;
}
OUTPUT:
Enter the number of disks: 4
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
8. Write a C program, which takes two integer operands and one operator from the user,
performs and then operation and then prints the result. (Consider the operators +,-,*, /, %
and use Switch
Statement)
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
OUTPUT:
Enter you operator(+, -, /, *, %)
+
Enter the values of a and b
13
addition of two numbers is 4
9. Write a C program to find both the largest and smallest number in a list of integers.
#include<stdio.h>
int main()
{
int i, n, lar,sm, elem;
printf ("Enter total number of elements n");
scanf ("%d", &elem);
printf ("Enter first number n");
scanf ("%d", &n);
lar = n;
sm=n;
for (i=1; i<= elem -1 ;i++)
{
printf ("n Enter another number n");
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("n The largest number is %d", lar);
printf ("n The smallest number is %d", sm);
return 0;
}
OUTPUT:
Enter total number of elements
10
Enter first number
3
Enter another number
8
Enter another number
12
Enter another number
42
Enter another number
89
Enter another number
45
Enter another number
236
Enter another number
890
Enter another number
411
Enter another number
328
The largest number is 890
The smallest number is 3
10. Write a C program that uses functions to perform the following:
i) Addition of Two Matrices
ii) Multiplication of Two Matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("The resultant 3*3 matrix c is \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
}
OUTPUT:
Enter the elements of 3*3 matrix a
123456789
Enter the elements of 3*3 matrix b
123456789
The resultant 3*3 matrix c is
2 4 6
8 10 12
14 16 18
Multiplication of Two MatricesProgram:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = 0
for(k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j])
}
}
}
printf("The resultant 3*3 matrix c is \n");
for(i = 0; i< 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
Enter the elements of 3*3 matrix a
123456789
Enter the elements of 3*3 matrix b
123456789
The resultant 3*3 matrix c is
30 36 42
55 81 96
102 126 150
11. Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) To delete n Characters from a given position in a given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i< l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i< l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
}
OUTPUT:
Enter the string 1
sachin
Enter the string 2
tendulkar
Enter the position where the string is to be inserted
4
After inserting the string is sachtendulkarin
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i< l; i++)
{
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();
}
OUTPUT:
Enter the string
sachin
Enter the position where characters are to be deleted
2
Enter the number of characters to be deleted
2
The string is sain
12. Write a C program to determine if the given string is a palindrome or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[50];
int i, j, len, chk=0;
printf("Enter any String (word): ");
scanf("%s", str);
len = strlen(str);
for(i=0, j=(len-1); i<=(len-1); i++, j--)
{
if(str[i] != str[j])
{
chk=1;
break;
}
}
if(chk==1)
printf("\nIt's not a Palindrome String");
else
printf("\nIt's a Palindrome String");
getch();
return 0;
}
OUTPUT:
enter any string: sachin
The string is Not a Palindrome
13. Write a C program that displays the position or index in the string S where the string T
begins, or– 1 if S doesn’t contain T.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
}
OUTPUT:
1.Enter the first string:
kali
Enter the string to be searched:
li
second string is found in the first string at 2 position
2.Enter the first string:
nagaraju
Enter the string to be searched:
raju
second string is found in the first string at 4 position
3.Enter the first string:
nagarjuna
Enter the string to be searched:
ma
-1
14. Write a C program to count the lines, words and characters in a given text.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i<= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f);
}
OUTPUT:
Enter any string
abc def ghijklmnopqrstuvwxyz
The number of characters in the string are 34
The number of words in the string are 9
15. Write a C program to generate Pascal’s triangle.
intmain()
{
intarray[30], temp[30],i, j, k, l,num;//using 2 arrays
printf("Enter the number of lines to be printed: ");
scanf("%d",&num);
temp[0]=1;
array[0]=1;
for(j =0; j <num;j++)
printf(" ");
printf(" 1\n");
for(i=1;i<num;i++)
{
for(j =0; j <i;j++)
printf(" ");
for(k =1; k <num; k++)
{
array[k]=temp[k -1]+ temp[k];
}
array[i]=1;
for(l =0; l <=i; l++)
{
printf("%3d", array[l]);
temp[l]= array[l];
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of lines to be printed: 4
1
11
121
1331
16. Write a C program to construct a pyramid of numbers
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i, n, j, p = 40;
clrscr();
printf("enter the number of lines\n");
scanf("%d", &n);
printf("pyramid shape is\n");
for(i = 0; i<n ; i++)
{
gotoxy(p, i + 1);
for(j = 0 - i; j <= i; j++)
{
printf("%3d", abs(j % 2));
}
p = p - 3;
printf("\n");
}
}
OUTPUT:
enter the number of lines
5
pyramid shape is
0
1 0 1
0 1 0 1 0
1 0 1 0 1 0 1
17. Write a C program to read in two numbers, x and n, and then compute the sum of this
geometric
progression:1+x+x 2 +x 3 +.............+x n
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum
Perform error checking.
For example, the formula does not make sense for negative exponents – if n is less than 0.
Have your program print an error message if n<0, then go back and read in the next pair of
numbers of without computing the sum. Are any values of x also illegal ? If so, test for them
too.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n, x, i, sum = 0;
clrscr();
printf("Enter the limit\n");
scanf("%d", &n);
printf("Enter the value of x\n");
scanf("%d", &x);
if(x < 0 || n < 0)
{
printf("illegal value");
}
else
{
for(i = 0; i<= n; i++)
sum=sum + pow(x, i);
}
printf("sum=%d", sum);
}
OUTPUT:
Enter the limit
4
Enter the value of x
sum=31
18. 2’s complement of a number is obtained by scanning it from right to left and
complementing allthe bits after the first appearance of a 1. Thus 2’s complement of 11100 is
00100. Write a Cprogram to find the 2’s complement of a binary number.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char a[20];
int i, carry, l;
clrscr();
printf("Enter the binary number \n");
scanf("%s", &a);
l = strlen(a);
for(i = 0; i< l; i++)
{
if(a[i] == '0')
{
a[i] = '1';
}
else
{
a[i] = '0';
}
}
printf("The 1's compliment of the binary number is %s \n", a);
i = strlen(a) - 1;
while(i>= 0)
{
if(a[i] == '0')
{
a[i] = '1';
carry = 0;
break;
}
else
{
a[i] = '0';
carry = 1;
i = i - 1;
}
}
printf("The 2's compliment of the binary number is ");
if(carry == 1)
{
printf("1");
}
printf("%s", a);
}
OUTPUT:
Enter the binary number
100101
The 1’s compliment of binary number is
011010
The 2’s compliment of binary number is
011011
19. Write a C program to convert a Roman numeral to its decimal equivalent.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char rom[30];
int a[30], l, i, k, dec;
clrscr();
printf("Enter the roman number\n");
scanf("%s", &rom);
l =strlen(rom);
for(i = 0; i< l; i++)
{
switch (rom[i])
{
case 'I': a[i] = 1;
break;
case 'V': a[i] = 5;
break;
case 'X': a[i] = 10;
break;
case 'L': a[i] = 50;
break;
case 'C': a[i] = 100;
break;
case 'D': dec = dec + 500;
break;
case 'M': a[i] = 1000;
break;
default :printf("Invalid choice");
break;
}
}
k = a[l - 1];
for(i = l - 1; i> 0; i--)
{
if(a[i] >a[i - 1])
{
k = k - a[i - 1];
}
if(a[i] <= a[i - 1])
{
k = k + a[i - 1];
}
}
printf("decimal equivalent is %d", k);
}
OUTPUT:
Enter the roman number
XIV
Decimal equivalent is 14
20. Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, x, y, z;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to divide two complex numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d", &choice);
if (choice == 5)
exit(0);
if (choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if (choice == 1)
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if (c.img >= 0)
printf("Sum of the complex numbers = %d + %di", c.real, c.img);
else
printf("Sum of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 2)
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if (c.img >= 0)
printf("Difference of the complex numbers = %d + %di", c.real, c.img);
else
printf("Difference of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 3)
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if (c.img >= 0)
printf("Multiplication of the complex numbers = %d + %di", c.real, c.img);
else
printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 4)
{
if (b.real == 0 && b.img == 0)
printf("Division by 0 + 0i isn't allowed.");
else
{
x = a.real*b.real + a.img*b.img;
y = a.img*b.real - a.real*b.img;
z = b.real*b.real + b.img*b.img;
if (x%z == 0 && y%z == 0)
{
if (y/z >= 0)
printf("Division of the complex numbers = %d + %di", x/z, y/z);
else
printf("Division of the complex numbers = %d %di", x/z, y/z);
}
else if (x%z == 0 && y%z != 0)
{
if (y/z >= 0)
printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
else
printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
}
else if (x%z != 0 && y%z == 0)
{
if (y/z >= 0)
printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
else
printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
}
else
{
if (y/z >= 0)
printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y, z);
else
printf("Division of two complex numbers = %d/%d %d/%di", x, z, y, z);
}
}
}
else
printf("Invalid choice.");
printf("\nPress any key to enter choice again...\n");
}
}
OUTPUT:
21.i. Write a C program which copies one file to another.
ii. Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)
i. Write a C program which copies one file to another.
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}
while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}
ii. Write a C program to reverse the first n characters in a file.
#include<stdio.h >
#include<conio.h >
#include<string.h >
#include<process.h >
voidmain(intargc, char *argv[])
{
FILE *fs, *fd;
chars[20], d[20];
int c = 0, count = 0, n;
clrscr();
strcpy(s, argv[1]);
n = atoi(argv[2]);
fs = fopen(s, "r");
if(s == NULL)
printf("\n FILE ERROR");
printf("\n SOURCE FILE :\n");
while(!feof(fs))
{
printf("%c", fgetc(fs));
c++;
}
fclose(fs);
fs = fopen(s, "r+");
count = 0;
while(count < n)
{
d[count] = fgetc(fs);
count++;
}
d[count] = '\0';
fseek(fs, 0L, 0);
fputs(strrev(d), fs);
fclose(fs);
fs = fopen(s,"r");
while(!feof(fs))
{
printf(“%c”, fgetc(fs));
c++;
}
fclose(fs);
getch();
}
22.i.Write a C program to display the contents of a file.
ii.Write a C program to merge two files into a third file (i.e., the contents of the first file
followed by those of the second are put in the third file)
i.Write a C program to display the contents of a file.
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fs;
char ch;
fs = fopen("1.txt","r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF)
break;
else
putchar(ch);
}
fclose(fs);
return 0;
}
ii. Write a C program to merge two files into a third file
#include<stdio.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char ch;
fp1=fopen("a.txt","r");
fp2=fopen("b.txt","r");
fp3=fopen("c.txt","w");
while((ch=getc(fp1))!=EOF)
putc(ch,fp3);
while((ch=getc(fp2))!=EOF)
putc(ch,fp3);
fcloseall();
printf("file contents are\n");
fp3=fopen("c.txt","r");
while((ch=getc(fp3))!=EOF)
printf("%c",ch);
fclose(fp3);
return 0;
}
23. Write a C program that implements the following sorting methods to sort a given list of
integers inascending order i) Bubble sort ii) Selection sortiii)Insertion sort
/*Bubble sort */
#include<stdio.h>
int main()
{
int i,j,t,a[10],n,flag=0;
printf("enter the size of array:");
scanf("%d",&n);
printf("enter elements into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<(n-1-i);j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)
break;
}
printf("the sorted order is:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
return 0;
}
OUTPUT:
enter the size of array: 5
enter elements into array: 5 8 2 34 6
the sorted order is: 2 5 6 8 34
/*Selection sort*/
#include<stdio.h>
int main()
{
int a[10],i,j,n,index,large;
printf(“enter %d elements into array\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=n-1;i>0;i--)
{
index=0;
large=a[0];
for(j=1;j<=1;j++)
{
if(a[j]>large)
large=a[j];
index=j;
}
}
a[index]=a[i];
a[i]=large;
}
printf(“the sorted order elements are:”);
for(i=0;i<n;i++)
printf(“\t %d”,a[i]);
return 0;
}
OUTPUT:
enter 5 elements into array
45138
the sorted order elements are: 1 3 4 5 8
/* Insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, a[10], i, j, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
for (i = 1 ;i<= n - 1; i++) {
j = i;
while ( j> 0 && a[j-1] > a[j]) {
t = a[j];
a[j] = a[j-1];
a[j-1] = t;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i<= n - 1; i++) {
printf("%d\n", a[i]);
}
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter 5 integers
10
9
4
7
3
Sorted list in ascending order: 3 4 7 9 10
24. Write C programs that use both recursive and non recursive functions to perform the
following
searching operations for a Key value in a given list of integers:
i) Linear search
ii) Binary search
/*Linear search*/
#include<stdio.h>
int main()
{
int a[10],i,j,m,n;
printf(“enter no of elements\n”);
scanf('%d”,&n);
printf(“enter %d elements \n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“enter elements to be searched”,m);
scanf(“%d”,&m);
j=0;
while(a[i]!=m)
j++;
if(j<n)
printf(“search successful element found at position %d”,j++)
else
printf(“unsuccessful search\n”);
return 0;
}
OUTPUT:
enter no of elements
5
enter 5 elements
10 20 32 55 21
enter elements to be searched 32
search successful element found at position 2
/* Binary search*/
#include<stdio.h>
int main()
{
int i,n,key,a[10],low,high,mid;
printf("enter range for array:");
scanf("%d",&n);
printf("enter elements into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the search element:");
scanf("%d",&key);
low=0;
high=n-1;
for(i=0;i<n;i++)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("element %d found at %d locaion",key,mid+1);
break;
}
if(key<a[mid])
high=mid;
else
low=mid+1;
if(i==n-1)
printf("element %d not found in array",key);
}
return 0;
}
OUTPUT:
enter range for array:5
enter elements into array: 10 20 40 50 55
the search element: 20
element %d found at %d locaion",20,1