Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
CS208ES
Computer Programming in C
Lab Manual
SHAIK. JAKEER HUSSAIN Associate Professor
Department of Computer Science & Engineering
Mahaveer Institute of Science & Technology
Hyderabad
Type equation here.
INDEX
Week
No.
Program Description Page
No.
1 Write a C program to find the factorial of a positive integer
Write a C program to find the roots of a quadratic equation
2
Write a C program to determine if the given number is a prime number
or not.
A 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.
3
Write a C program to construct a pyramid of numbers.
Write a C program to calculate the following Sum:
𝑆𝑢𝑚 = 1 −𝑥2
2!+
𝑥4
4!−
𝑥6
6!+
𝑥8
8!−
𝑥10
10!
4
The least common multiple (LCM) of two positive integers a and b is the
smallest integer that is evenly divisible by both a and b. Write a C
program that reads two integers and calls LCM (a, b) function that takes
two integer arguments and returns their LCM. The LCM (a, b) function
should calculate the least common multiple by calling
the GCD (a, b) function and using the following relation:
LCM (a, b) = ab / GCD (a, b)
Write a C program that reads two integers n and r to compute the
𝑛𝐶𝑟value using the following relation: 𝑛𝐶𝑟
(n, r) = n! / r! (n-r)! . Use a
function for computing the factorial value of an integer.
5
Write C program that reads two integers x and n and calls a recursive
function to compute x
Write a C program that uses a recursive function to solve the Towers of
Hanoi problem.
Write a C program that reads two integers and calls a recursive
function to compute 𝑛𝐶𝑟value.
6
Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user using Sieve of Eratosthenes
algorithm.
Write a C program that uses non recursive function to search for a Key
value in a given list of integers. Use linear search method.
7
Write a menu-driven C program that allows a user to enter n numbers
and then choose between finding the smallest, largest, sum, or average.
The menu and all the choices are to be functions. Use a switch
statement to determine what action to take. Display an error message if
an invalid choice is entered.
Write a C program that uses non recursive function to search for a Key
value in a given sorted list of integers. Use binary search method.
8
Write a C program that implements the Bubble sort method to sort a
given list of integers in ascending order.
Write a C program that reads two matrices and uses functions to
perform the following:
1. Addition of two matrices
2. Multiplication of two matrices
9
Write a C program that uses functions to perform the following
operations:
1. To insert a sub-string into a given main string from a given position.
2. To delete n characters from a given position in a given string.
Write a C program that uses a non-recursive function to determine if
the given string is a palindrome or not.
10
Write a C program to replace a substring with another in a given line of
text.
Write a C program that reads 15 names each of up to 30 characters,
stores them in an array, and uses an array of pointers to display them in
ascending (ie. alphabetical) order.
11
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.
Write a C program to convert a positive integer to a roman numeral.
Ex. 11 is converted to XI.
12
Write a C program to display the contents of a file to standard output
device.
Write a C program which copies one file to another, replacing all
lowercase characters with their uppercase equivalents.
13
Write a C program to count the number of times a character occurs in a
text file. The file name and the character are supplied as command-line
arguments.
Write a C program to compare two files, printing the first line where
they differ.
14
Write a C program to change the nth character (byte) in a text file. Use
fseek function.
Write a C program to reverse the first n characters in a file. The file
name and n are specified on the command line. Use fseek function.
15
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).
Define a macro that finds the maximum of two numbers. Write a C
program that uses the macro and prints the maximum of two numbers.
Course Objective:
To write programs in C using structured programming approach to solve the
problems.
Course Outcomes:
Ability to design and test programs to solve mathematical and scientific problems.
Ability to write structured programs using control structures and functions.
Recommended Systems/Software Requirements:
Intel based desktop PC
GNU C Compiler
Week - 1
a) Write a C program to find the factorial of a positive integer.
#include<stdio.h>
int main()
{
int n,i=1,fact=1,f;
printf("enter a number :");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("factorial of %d is : %d",n,fact);
}
Output :
enter a number :4
factorial of 4 is : 24
b) Write a C program to find the roots of a quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, root1, root2;
clrscr();
printf("Enter the values of a, b, c\n");
scanf("%f%f%f", &a, &b, &c);
if(a == 0 || b == 0 || c == 0)
{
printf("Error: Roots can't be determined");
}
else
{
d = (b * b) - (4.0 * a * c);
if(d > 0.00)
{
printf("Roots are real and distinct \n");
root1 = -b + sqrt(d) / (2.0 * a);
root2 = -b - sqrt(d) / (2.0 * a);
printf("Root1 = %f \nRoot2 = %f", root1, root2);
}
else if (d < 0.00)
{
printf("Roots are imaginary");
root1 = -b / (2.0 * a) ;
root2 = sqrt(abs(d)) / (2.0 * a);
printf("Root1 = %f +i %f\n", root1, root2);
printf("Root2 = %f -i %f\n", root1, root2);
}
else if (d == 0.00)
{
printf("Roots are real and equal\n");
root1 = -b / (2.0 * a);
root2 = root1;
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
}
getch();
}
Output:
Enter the values of a, b, c
1
-6
9
Roots are real and equal
Root1 = 3.000000
Root2 = 3.000000
Week 2:
a) Write a C program to determine if the given number is a prime number or not.
#include <stdio.h>
void main(){
int num,i,ctr=0;
printf("Input a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num % i==0){
ctr++;
break;
}
}
if(ctr==0 && num!= 1)
printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number",num);
}
Output:
Input a number: 9
9 is not a prime number
b) A 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
Week-3
a) 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");
}
getch();
}
b) Write a C program to calculate the following Sum:
𝑆𝑢𝑚 = 1 −𝑥2
2!+
𝑥4
4!−
𝑥6
6!+
𝑥8
8!−
𝑥10
10!
# include<stdio.h>
# include<conio.h>
# include<math.h>
void main()
{
int i, n ;
float x, val, sum = 1, t = 1 ;
clrscr() ;
printf("Enter the value for x : ") ;
scanf("%f", &x) ;
printf("\nEnter the value for n : ") ;
scanf("%d", &n) ;
val = x ;
x = x * 3.14159 / 180 ;
for(i = 1 ; i < n + 1 ; i++)
{
t = t * pow((double) (-1), (double) (2 * i - 1)) * x * x / (2 * i * (2 * i - 1)) ;
sum = sum + t ;
}
printf("\nCosine value of sin %f is : %8.4f", val, sum) ;
getch() ;
}
Output:
Enter the Value of x: 2
Enter the limit of n: 4
The sum of sin 2.000000 series is 0.9994
Week – 4
The least common multiple (LCM) of two positive integers a and b is the smallest integer
that is evenly divisible by both a and b. Write a C program that reads two integers and
calls LCM (a, b) function that takes two integer arguments and returns their LCM. The
LCM (a, b) function should calculate the least common multiple by calling the GCD (a,
b) function and using the following relation: LCM (a, b) = ab / GCD (a, b) # include<stdio.h>
# include<conio.h>
void main()
{
int a,b,lcm;
int gcd(int,int);
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
lcm=(a*b)/gcd(a,b);
printf("lcm=%d",lcm);
getch();
}
int gcd(int a,int b)
{
int r;
while(a%b!=0)
{ r=a%b;
a=b;
b=r;
}
return(b);
}
Output:
enter two number 36 24
lcm=72
b) Write a C program that reads two integers n and r to compute the 𝒏𝑪𝒓value using the
following relation: 𝒏𝑪𝒓 (n, r) = n! / r! (n-r)! . Use a function for computing the factorial
value of an integer.
# include<stdio.h>
# include<conio.h>
void main()
{
int a,b;
int fact(int);
clrscr();
printf("enter two numbers");
scanf("%d%d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("ncr=%d",ncr);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}
Output:
enter n value :4
enter r value :2
ncr value for 4 and 2 value is : 6
a) Write C program that reads two integers x and n and calls a recursive function to compute
𝑥𝑛
#include<stdio.h>
int mul(int a,int b);
main()
{
int x,n,m;
printf("enter values of x and n :"); scanf("%d%d",&x,&n); m=mul(x,n);
printf("value of after finding %d power %d is :%d",x,n,m);
}
int mul(int a,int b)
{
if(b==1) return a;
else
return a*mul(a,b-1);
}
Output:
enter values of x and n :2 4
value of after finding 2 power 4 is :16
b) Write a C program that uses a recursive function to solve the Towers of Hanoi problem.
#include<stdio.h>
int tower_of_hanoi(int limit, char source_tower, char temporary_tower, char
destination_tower);
int main()
{
char source_tower = 'A', temporary_tower = 'B', destination_tower = 'C';
int limit;
printf("\nEnter The Number of Disks:\t");
scanf("%d", &limit);
printf("\nSequence of Disks:\n");
tower_of_hanoi(limit, source_tower, temporary_tower, destination_tower);
printf("\n");
return 0;
}
int tower_of_hanoi(int limit, char source_tower, char temporary_tower, char
destination_tower)
{
if(limit == 1)
{
printf("\nMove Disk %d From %c To %c\n", limit, source_tower, destination_tower);
return 0;
}
tower_of_hanoi(limit - 1, source_tower, destination_tower, temporary_tower);
printf("Move Disk %d From %c To %c\n", limit, source_tower, destination_tower);
tower_of_hanoi(limit - 1, temporary_tower, source_tower, destination_tower);
return 0;
}
Output:
Enter The Number of Disks: 3
Sequence of Disks:
Move Disk 1 From A To C
Move Disk 2 From A To B
Move Disk 1 From C To B
Move Disk 3 From A To C
Move Disk 1 From B To A
Move Disk 2 From B To C
Move Disk 1 From A To C
c) Write a C program that reads two integers and calls a recursive function to compute
𝒏𝑪𝒓 value.
#include<stdio.h>
long int factorial(long int a);
main()
{
long int n,r,ncr; printf("enter n value :");
scanf("%li",&n); printf("enter r value :");
scanf("%li",&r);
ncr=factorial(n)/(factorial(r)*factorial(n-r));
printf("ncr value for %li and %li value is : %li ",n,r,ncr);
}
long int factorial(long int a)
{
if(a==1) return 1; else
return a*factorial(a-1);
}
Output:
enter n value :4
enter r value :2
ncr value for 4 and 2 value is : 6
Week – 6
a) Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user using Sieve of Eratosthenes algorithm.
#include <stdio.h>
#include <math.h>
void sieve(int n, int primes[]);
main()
{
int i, n;
printf("enter n value ");
scanf("%d",&n);
int v[n]; sieve(n, v);
printf("prime numbers 1 to %d is\n" ,n);
for (i=0;i<n;i++) if (v[i] == 1)
printf("%d\t",i);
}
void sieve(int n, int primes[])
{
int i, j;
for (i=2;i<n;i++)
primes[i]=1;
for (i=2;i<sqrt(n);i++)
for (j=i*i;j<n;j=j+i)
{
primes[j] = 0;
}
}
Output:
enter n value 20
prime numbers 1 to 20 is
2 3 5 7 11 13 17 19
b) Write a C program that uses non recursive function to search for a Key value in a
given list of integers. Use linear search method.
#include<stdio.h>
main()
{
int n,s,i,f=0;
printf("enter number of elements in list: ");
scanf("%d",&n);
int a[n];
printf("enter list of elements in to array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter search element :");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(a[i]==s)
{
printf("search element is present in list at location %d",i+1);
f=f+1;
}
}
if(f==0)
{
printf("search element is not present in given list");
}
}
Output:
enter number of elements in list: 5
enter list of elements in to array: 5 8 0 9 6
enter search element :8
search element is present in list at location 2
a) Write a menu-driven C program that allows a user to enter n numbers and then choose
between finding the smallest, largest, sum, or average. The menu and all the choices are to be
functions. Use a switch statement to determine what action to take. Display an error message
if an invalid choice is entered.
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i;
printf("enter number of elements :");
scanf("%d",&n);
int a[n],s,l,sum=0,op; float avg;
printf("enter numbers into list ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf(" \n 1.smallest");
printf(" \n 2.largest");
printf(" \n 3.sum");
printf(" \n 4.average");
printf("\n 5.exit");
printf("\n invalid operator");
while(1)
{
printf("\n enter option:");
scanf("%d",&op);
switch (op)
{
case 1 :
s=a[0];
for(i=1;i<n;i++)
{
if(a[i]<s)
s=a[i];
}
printf("smallest number is : %d",s);
break;
case 2 :
l=a[0];
for(i=1;i<n;i++)
{
if(a[i]>l)
l=a[i];
}
printf("largest number is: %d",l);
break;
case 3 :
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("sum of numbers in list is :%d",sum);
break;
case 4 :
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=(float)sum/n;
printf("average of numbers in list is :%f",avg);
break;
case 5 :
exit(0);
default :
printf("invalid operator");
break;
}
}
}
Output:
enter number of elements :5
enter numbers into list 1 3 2 4 5
1.smallest
2.largest
3.sum
4.average
5.exit
invalid operator
enter option:1
smallest number is : 1
enter option:2
largest number is: 5
enter option:3
sum of numbers in list is :15
enter option:4
average of numbers in list is :6.000000
enter option:6
invalid operator
enter option:5
b) Write a C program that uses non recursive function to search for a Key value in a given
sorted list of integers. Use binary search method.
#include<stdio.h>
main()
{
int n,i,key,low,high,mid,f=0;
printf("enter no of elements :");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter search element:");
scanf("%d",&key);
high=n-1;
low=0;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("\n search element %d is found at index: %d",key,mid+1);
f=1;
break;
}
else if(a[mid]>key)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(f==0)
{
printf("\n element is not present in given list");
}
else
printf("\n element is found");
}
Output:
enter no of elements :5
12 23 34 45 56
enter search element:34
search element 34 is found at index: 3
element is found
enter no of elements :5
12 23 34 45 56
enter search element:67
element is not present in given list
Week-8
Write a C program that implements the Bubble sort method to sort a given list of
integers in ascending order.
#include <stdio.h>
#define MAXSIZE 10
main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
Output:
Enter the value of num
10
Enter the elements one by one
12 23 34 45 56 67 78 89 98 75
Input array is
12
23
34
45
56
67
78
89
98
75
Sorted array is...
12
23
34
45
56
67
75
78
89
98
Write a C program that reads two matrices and uses functions to perform the following:
1. Addition of two matrices
#include<stdio.h>
#include<conio.h>
void read_arr(int a[10][10],int row,int col);
void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col);
void print_arr(int m[10][10],int row,int col);
main()
{
int a[10][10],b[10][10],c[10][10],row,col;
printf("Enter number of rows :");
scanf("%d",&row);
printf("Enter number of colomns :");
scanf("%d",&col);
printf("\nEnter A matrix elements\n");
read_arr(a,row,col);
printf("\nEnter B matrix elements\n");
read_arr(b,row,col);
add_arr(a,b,c,row,col);
printf("\nA matrix\n");
print_arr(a,row,col);
printf("\nB matrix is \n");
print_arr(b,row,col);
printf("\nAddition of give two matrix is \n");
print_arr(c,row,col);
getch();
}
void read_arr(int a[10][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Enter Element %d %d : ",i,j);
scanf("%d",&a[i][j]);
}
}
}
void add_arr(int a[10][10],int b[10][10],int c[10][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j] = (a[i][j] + b[i][j]);
}
}
}
void print_arr(int c[10][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
}
Output:
Enter number of rows :2
Enter number of colomns :2
Enter A matrix elements
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 1 0 : 2
Enter Element 1 1 : 3
Enter B matrix elements
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 1 0 : 3
Enter Element 1 1 : 4
A matrix
1 2
2 3
B matrix is
1 2
3 4
Addition of give two matrix is
2 4
5 7
Write a C program that reads two matrices and uses functions to perform the following:
2. Multiplication of two matrices
#include<stdio.h>
#include<conio.h>
void read_arr(int a[10][10],int row,int col);
void mul_arr(int a[10][10],int b[10][10],int c[10][10],int row,int col);
void print_arr(int c[10][10],int row,int col);
main()
{
int a[10][10],b[10][10],c[10][10],row,col;
printf("Enter number of rows :");
scanf("%d",&row);
printf("Enter number of colomns :");
scanf("%d",&col);
printf("\nEnter A matrix elements\n");
read_arr(a,row,col);
printf("\nEnter B matrix elements\n");
read_arr(b,row,col);
mul_arr(a,b,c,row,col);
printf("\nA matrix\n");
print_arr(a,row,col);
printf("\nB matrix is \n");
print_arr(b,row,col);
printf("\nMultiplication of give two matrix is \n");
print_arr(c,row,col);
getch();
}
void read_arr(int a[10][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Enter Element at %d %d : ",i,j);
scanf("%d",&a[i][j]);
}
}
}
void mul_arr(int a[10][10],int b[10][10],int c[10][10],int row,int col)
{
int i,j,k;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=0;
for(k=0;k<row; k++)
{
c[i][j] = c[i][j]+a[i][k] * b[k][j];
}
}
}
}
void print_arr(int c[10][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
}
Enter number of rows :2
Enter number of colomns :2
Enter A matrix elements
Enter Element at 0 0 : 1
Enter Element at 0 1 : 2
Enter Element at 1 0 : 3
Enter Element at 1 1 : 4
Enter B matrix elements
Enter Element at 0 0 : 5
Enter Element at 0 1 : 6
Enter Element at 1 0 : 7
Enter Element at 1 1 : 8
A matrix
1 2
3 4
B matrix is
5 6
7 8
Multiplication of give two matrix is
19 22
43 50
Week-9
a) Write a C program that uses functions to perform the following operations:
1. to insert a sub-string into a given main string from a given position.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char main[30], sub[30], result[100];
int n;
printf("enter main string");
fflush(stdin);
gets(main);
printf("enter A sub string");
fflush(stdin);
gets(sub);
printf("Enter the position of sub string");
scanf("%d", &n);
if(n>strlen(main))
{
printf("Postion is exceeds the main string");
}
else
strncpy(result, main, n);
result[n]='\0';
strcat(result,sub);
strcat(result, main+n);
printf("result string is %s", result);
getch();
}
Output:
enter main string shaik hussain
enter A sub string jakeer
Enter the position of sub string 6
result string is shaik jakeer hussain
Write a C program that uses functions to perform the following operations:
2. to delete n characters from a given position in a given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
printf("Enter the string:");
gets(st);
printf("\nEntre the index position:");
scanf("%d",&pos);
printf("\nEnter the no.of characters to delete:");
scanf("%d",&n);
if(pos<=strlen(st))
{
for(i=0;i<=strlen(st);i++)
{
if(i==pos)
{
for(j=0;st[i]!='\0';j++)
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}
st[i]='\0';
}
}
printf("\n\nAfter deletion the string: %s",st);
}
else
printf("\nSorry, we cannot delete from that position.");
getch();
}
Enter the string:shaik jakeer hussain
Entre the index position:6
Enter the no.of characters to delete:6
After deletion the string: shaik hussain
Write a C program that uses a non recursive function to determine if the given string is
a palindrome or not
#include <stdio.h>
#include <string.h>
main()
{
char str[20]; int i, l, f = 0;
printf("Enter any string\n"); gets(str);
l = strlen(str);
for(i = 0; i <= l - 1; i++)
{
if(str[i] == str[l - 1 - i]) f = f + 1;
}
if(f == l)
{
printf("The string is palindrome");
}
else
{
printf("The string is not a palindrome");
}
}
Enter any string
madam
The string is palindrome
Enter any string
hai
The string is not a palindrome
Week-10
Write a C program to replace a substring with another in a given line of text.
#include <stdio.h>
#include <string.h>
main()
{
char text[100],word[10],rpwrd[10],str[10][10]; int i=0,j=0,k=0,w,p;
printf("Please write any text.\n");
printf("Give only one space after every word\n");
printf("WHEN COMPLETE PRESS Ctrl-Z \n");
gets(text);
printf("\nEnter which word is to be replaced\n");
scanf("%s",word);
printf("\nEnter by which word the %s is to be replaced\n",word);
scanf("%s",rpwrd);
p=strlen(text);
for (k=0; k<p; k++)
{
if (text[k]!=' ')
{
str[i][j] = text[k]; j++;
}
else
{
str[i][j]='\0'; j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
}
Output:
Please write any text.
Give only one space after every word
WHEN COMPLETE PRESS Ctrl-Z
shaik jakeer hussain
Enter which word is to be replaced
jakeer
Enter by which word the jakeer is to be replaced
shakeer
shaik shakeer hussain
Write a C program that reads 15 names each of up to 30 characters, stores them
in an array, and uses an array of pointers to display them in ascending (ie.
alphabetical) order.
#include <stdio.h>
#include <string.h>
main()
{
char name[30][30], tname[30][30], temp[30]; int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter %d words \n",n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]); strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]); strcpy(name[i], name[j]); strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Namest\tSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
}
Enter the value of n
10
Enter 10 words
apple
banana
custedapple
pineapple
grapes
guava
kiwi
orange
strabery
chicoo
----------------------------------------
Input Namest Sorted names
------------------------------------------
apple apple
banana banana
custedapple chicoo
pineapple custedapple
grapes grapes
guava guava
kiwi kiwi
orange orange
strabery pineapple
chicoo strabery
week – 11
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.
#include <conio.h>
main()
{
char a[20];
int i, carry, l;
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);
getch();
}
Output:
Enter the binary number
1100101
The 1's compliment of the binary number is 0011010
The 2's compliment of the binary number is 0011011
Write a C program to convert a positive integer to a roman numeral. Ex. 11 is
converted to XI.
#include <stdio.h>
void predigit(char num1, char num2);
void postdigit(char c, int n);
char romanval[1000];
int i = 0;
int main()
{
int j;
long number;
printf("Enter the number: ");
scanf("%d", &number);
if (number <= 0)
{
printf("Invalid number");
return 0;
}
while (number != 0)
{
if (number >= 1000)
{
postdigit('M', number / 1000);
number = number - (number / 1000) * 1000;
}
else if (number >= 500)
{
if (number < (500 + 4 * 100))
{
postdigit('D', number / 500);
number = number - (number / 500) * 500;
}
else
{
predigit('C','M');
number = number - (1000-100);
}
}
else if (number >= 100)
{
if (number < (100 + 3 * 100))
{
postdigit('C', number / 100);
number = number - (number / 100) * 100;
}
else
{
predigit('L', 'D');
number = number - (500 - 100);
}
}
else if (number >= 50 )
{
if (number < (50 + 4 * 10))
{
postdigit('L', number / 50);
number = number - (number / 50) * 50;
}
else
{
predigit('X','C');
number = number - (100-10);
}
}
else if (number >= 10)
{
if (number < (10 + 3 * 10))
{
postdigit('X', number / 10);
number = number - (number / 10) * 10;
}
else
{
predigit('X','L');
number = number - (50 - 10);
}
}
else if (number >= 5)
{
if (number < (5 + 4 * 1))
{
postdigit('V', number / 5);
number = number - (number / 5) * 5;
}
else
{
predigit('I', 'X');
number = number - (10 - 1);
}
}
else if (number >= 1)
{
if (number < 4)
{
postdigit('I', number / 1);
number = number - (number / 1) * 1;
}
else
{
predigit('I', 'V');
number = number - (5 - 1);
}
}
}
printf("Roman number is: ");
for(j = 0; j < i; j++)
printf("%c", romanval[j]);
return 0;
}
void predigit(char num1, char num2)
{
romanval[i++] = num1;
romanval[i++] = num2;
}
void postdigit(char c, int n)
{
int j;
for (j = 0; j < n; j++)
romanval[i++] = c;
}
Output:
Enter the number: 5021
Roman number is: MMMMMXXI
Week-12
Write a C program to display the contents of a file to standard output device.
#include <stdio.h>
int main()
{
char in_name[80];
FILE *in_file;
int ch;
printf("Enter file name:\n");
scanf("%s", in_name);
in_file = fopen(in_name, "r");
if (in_file == NULL)
{
printf("Can't open %s for reading.\n", in_name);
}
else
{
while ((ch = fgetc(in_file)) != EOF)
{
printf("%c", ch);
}
fclose(in_file);
}
return 0;
}
Output:
Enter file name:
myfile.txt
SHAIK JAKEER HUSSAIN
ASSOCIATE PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
MAHAVEER INSTITUTE OF SCIENCE AND TECHNOLOGY
BANDLAGUDA,
VYASAPURI COLONY,
KESHAV GIRI
HYDERABAD
500 005
PH. 9912721786
EMAIL: [email protected]
Write a C program which copies one file to another, replacing all lowercase characters
with their uppercase equivalents
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("source.txt", "r");
if (fp1 == NULL)
{
puts("File does not exist..");
exit(1);
}
fp2 = fopen("target.txt", "w");
if (fp2 == NULL)
{
puts("File does not exist..");
fclose(fp1);
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
{
ch = toupper(ch);
fputc(ch,fp2);
}
printf("\nFile successfully copied..");
return 0;
}
Output:
Content of source.txt:
shaik jakeer hussain
associate professor
department of computer science and engineering
mahaveer institute of science and technology
bandlaguda,
vyasapuri colony,
keshav giri
hyderabad
500 005
ph. 9912721786
email: [email protected].
File successfully copied..
After execution of target.txt:
SHAIK JAKEER HUSSAIN
ASSOCIATE PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
MAHAVEER INSTITUTE OF SCIENCE AND TECHNOLOGY
BANDLAGUDA,
VYASAPURI COLONY,
KESHAV GIRI
HYDERABAD
500 005
PH. 9912721786
EMAIL: [email protected].
Week – 13
Write a C program to count the number of times a character occurs in a text file. The
file name and the character are supplied as command-line arguments.
Write a C program to compare two files, printing the first line where they differ.
#include <stdio.h>
#include <string.h>
#define MAXNAME 20
#define MAXLINE 100
FILE *first, *second;
int main()
{
char f[MAXNAME], s[MAXNAME], str1[MAXLINE], str2[MAXLINE];
printf("type the names of the compared files\n");
printf("first: ");
gets(f);
printf("second: ");
gets(s);
if((first = fopen(f, "r")) == NULL)
{
perror(f);
return 1;
}
else if((second = fopen(s, "r")) == NULL)
{
perror(s);
return 1;
}
else
printf("files open\n\n");
while(!feof(first) && !feof(second))
{
fgets(str1, MAXLINE-1, first);
fgets(str2, MAXLINE-1, second);
if(strcmp(str1,str2) != 0)
{
printf("first different strings:\n\n");
printf("%s\n%s\n", str1, str2);
break;
}
}
fclose(first);
fclose(second);
return 0;
}
Output:
type the names of the compared files
first: first.txt
second: second.txt
files open
first different strings:
this file created by jakeer hussain.
this file created by jakeer hussain.
Week 14.
Write a C program to change the nth character (byte) in a text file. Use fseek function.
Write a C program to reverse the first n characters in a file. The file name and n are
specified on the command line. Use fseek function.
include<stdio.h >
#include<conio.h >
#include<string.h >
#include<process.h >
void main(int argc, char *argv[])
{
FILE *fs, *fd;
char s[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();
}
Week-15
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).
#include<stdio.h>
void concatenate(FILE *fp1, FILE *fp2, char *argv[], int argc);
int main(int argc, char *argv[]){
FILE *fp1, *fp2;
concatenate(fp1, fp2, argv, argc);
return 0;
}
void concatenate(FILE *fp1, FILE *fp2, char **argv, int argc){
int i, ch;
fp2 = fopen("files", "a");
for(i = 1; i < argc - 1; i++){
fp1 = fopen(argv[i], "r");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
}
}
b) Define a macro that finds the maximum of two numbers. Write a C program that
uses the macro and prints the maximum of two numbers.