1. Sum of the individual digits means adding all the digits of a number.
Ex: 123 sum of digits is 1+2+3 = 6
Algorithm:
Step 1: Start
Step 2: Read num as integer
Step 3: while (num > 0)
begin
dig ← num MOD 10;
sumOfDigits ← sumOf Digits + dig;
num ← num DIV 10;
end
Step 4: Print sumOfDigits
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int number = 0, digit = 0, sumOfDigits = 0;
clrscr();
printf("Enter any number\n ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
sumOfDigits = sumOfDigits + digit;
number = number / 10;
}
printf ("Sum of individual digits of a given number is %d", sumOfDigits);
getch();
}
Input & Output:
Enter any number
1234
Sum of individual digits of a given number is 10
========================================================================
2 .A fibonacci series is defined as follows:
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1, t2, ………… tn be terms in fibinacci sequence
t1 = 0, t2 = 1
tn = tn - 2 + tn - 1 …… where n > 2
Algorithm:
Step 1: Start
Step 2: Read a, b, sum, lengthOfSeries values as integers
Step 3: Set a as 0 and b as 1
Step 4: for counter: 2 to no increment counter by 1
begin
sum ← a + b;
Print sum
a ← b;
b ← sum;
end
Step 5: Stop
Program:
#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();
}
Input & 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. Prime number is a number which is exactly divisible by one and itself only Ex: 2, 3, 5, 7,………
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i = 1, c = 0
Step 4:if i <= n goto step 5
If not goto step 10
Step 5: initialize j = 1
Step 6: if j <= 1 do as the follow. If no goto step 7
i)if i%j == 0 increment c
ii) increment j
iii) goto Step 6
Step 7: if c == 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
Program:
#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();
}
Input & 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.
Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary
Algorithm:
Step 1: Start
Step 2: Read A, B, C as integer
Step 3: Declare disc, deno, x1, x2 as float
Step 4: Assign disc = (B * B) - (4 * A * C)
Step 5: Assign deno = 2 * A;
Step 6: if( disc > 0 )
begin
Print “THE ROOTS ARE REAL ROOTS”
Assign x1 ← (-B / deno) + (sqrt(disc) / deno)
Assign x2 ← (-B / deno) - (sqrt(disc) / deno)
Print x1, x2
end
else if(disc = 0)
begin
Print “ THE ROOTS ARE REPEATED ROOTS"
Assign x1 ← -B / deno
Print x1
end
else Print “THE ROOTS ARE IMAGINARY ROOTS”
Step7: Stop
Program:
#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();
}
Input & Output:
Enter the values of a, b, c
1 2 3
Roots are imaginary
Root1 = -1.000 + i
Root2 = -1.000 - i
======================================================================
5.Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
The above equation looks like a COSINE Equation of Taylor Sries i.e.,
Algorithm:
Step 1: Start
Step 2: Read x, n values as integers
Step 3: Set i = 2, s = 1, pwr = 1, nr = 1
Step 4: Convert x1 into degrees
Step 5: Assign x1 as sum
Step 6: while (i <= n)
begin
pwr ← pwr + 2;
dr ← dr * pwr * (pwr - 1);
sum ← sum + (nr DIV dr) * s;
s ← s * (-1);
nr ← nr * x1 * x1;
i ← i + 2;
end
Step 7: Print sum
Step 8: Stop
Program:
# 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() ;
}
Input & Output:
Enter the Value of x: 2
Enter the limit of n: 4
The sum of sin 2.000000 series is 0.9994
========================================================================
6.The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2)
Algorithm:
Step 1: Start
Step 2: Read interval as integer
Step 3: for counter: 1 to interval increment counter by 1
begin
Read time, velocity, acceleration
Distance += (velocity * time + (accelerations * pow(time, 2)) / 2);
end
Step 4: Print Distance
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() e
{
int i, n, sec;
float d, u, a;
clrscr();
printf("Enter the no. of intervals\n");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("interval: %d \n", i);
printf("Enter the time in seconds \n");
scanf("%d",&sec);
printf("Enter the velocity \n");
scanf("%f", &u);
printf("Enter the acceleration \n");
scanf("%f", &a);
d= d + (u * sec + (a * (pow(sec, 2))) / 2);
}
printf("Total distance travelled is %.2f", d);
getch();
}
Input & Output:
Enter the number of intervals: 2
Interval: 1
Enter the time in seconds
30
Enter the velocity
35
Enter the acceleration
20
Interval: 2
Enter the time in seconds
40
Enter the velocity
45
Enter the acceleration
30
Total distance travelled is 35850.00
========================================================================
7.To take the two integer operands and one operator from user to perform the some arithmetic operations by using the following operators like
+,-,*, /, %
Ex: 2+3=5
Algorithm:
Step 1: Start
Step 2: Read x and y values
Step 3: Read option + or – or * or / or %
Step 4: If option is ‘+’ res = x + y
Step 5: If option is ‘-’ res = x - y
Step 6: If option is ‘*’ res = x * y
Step 7: If option is ‘/’ res = x / y
Step 8: If option is ‘%’ res = x % y
Step 9: If option does not match with + or – or * or / or %
Print select option +, -, *, /, /, % only
Step 10: Print x, option, y, res values
Step 11: Stop
Program:
#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();
}
Input & Output:
Enter you operator(+, -, /, *, %)
+
Enter the values of a and b
1 3
addition of two numbers is 4
8. Factorial of a number is nothing but the multiplication of numbers from a given number to 1 Ex: 5! =5*4*3*2*1= 120
i) To find the factorial of a given integer.
Algorithm:
Step 1: Start
Step 2: Read n value as integer
Step 3: Call function factorial (int n)
Step 4: End
Call function factorial(int n)
begin
if (n = 0)
return 1;
else
return (n * factorial(n - 1));
end
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
a = recfactorial(n);
printf("The factorial of a given number using recursion is %d \n", a);
b = nonrecfactorial(n);
printf("The factorial of a given number using nonrecursion is %d ", b);
getch();
}
int recfactorial(int x)
{
int f;
if(x == 0)
{
return(1);
}
else
{
f = x * recfactorial(x - 1);
return(f);
}
}
int nonrecfactorial(int x)
{
int i, f = 1;
for(i = 1;i <= x; i++)
{
f = f * i;
}
return(f);
}
Input & Output:
Enter any number
5
The factorial of a given number using recursion is 120
The factorial of a given number using nonrecursion is 120
========================================================================
9.ii) To find the GCD (greatest common divisor) of two given integers
Algorithm:
Step 1: Start
Step 2: Read a, b values as integers
Step 3: Call function gcd (a, b) and assign it to res
Step 4: Print res
Step 5: Stop
Called function gcd (a, b)
begin
while(v != 0)
begin
temp ← u MOD v;
u ← v;
v ← temp;
end
return(u);
end
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, d;
clrscr();
printf("Enter two numbers a, b\n");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
getch();
}
int recgcd(int x, int y)
{
if(y == 0)
{
return(x);
}
else
{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y)
{
int z;
while(x % y != 0)
{
z = x % y;
x = y;
y = z;
}
return(y);
}
Input & Output:
Enter two numbers a, b
3 6
The gcd of two numbers using recursion is 3
The gcd of two numbers using no recursion is 3
10 .Binary search program


10 .Binary search program
#include <stdio.h>
int main()
{
int c, first, last, middle, n,
search, array[100];
printf("Enter number of
elements\n");
scanf("%d",&n);
printf("Enter %d
integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to
find\n");
scanf("%d",
&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] <
search)
first = middle + 1;
else if (array[middle] ==
search) {
printf("%d found at
location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d
is not present in the list.\n", search);
return 0;
}
Sample Input:
Sample out put:
11. Bubble sort program
#include<stdio.h>
#include<conio.h>
int
main( )
{
int a[100];
int i, j, temp, n ;
printf("how many numbers you want to
sort : \n");
scanf("%d",&n);
printf("Enter %d number values you
want to sort\n", n);
for(j=0; j<n; j++)
scanf("%d",&a[j]);
for(j=1;j<n;j++)
{
for(i=0; i<n; i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf ( "\n\nArray after
sorting:\n") ;
for ( i = 0 ; i <n ; i++ )
printf ( "%d\t", a[i] ) ;
getch();
}

12.Addition of two matrixes
#include<stdio.h>
int
main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First
matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second
matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",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("\nThe Addition of two matrix
is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Sample
out put

13.Multiplication of
two matrixes
#include<stdio.h>
int
main() {
int a[10][10], b[10][10], c[10][10], i, j,
k;
int sum = 0;
printf("\nEnter First Matrix :
n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is:
\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is :
\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\nMultiplication Of Two
Matrices : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
return (0);
}

14.Insert a substring in main string
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter
First String:");
gets(a);
puts("Enter
Second String:");
gets(b);
printf("Enter
the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string
into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i<s;i++)
{
x
= c[i];
if(t<n)
{
a[i]
= b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s",
a);
getch();
}
Sample input : Enter the first
string: welcome
Enter ther second
string:eee
Enter the position
where the item has to be inserted:3
Sample input : “Welcomeeee”
15.To delete n characters from a
given position in a given string
#include
<stdio.h>
#include
<conio.h>
#include
<string.h>
void
delchar(char *x,int a, int b);
void
main()
{
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from
where to delete");
scanf("%d",&pos);
printf("Enter the number of
characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
void
delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}
Sample input: enter the string:amar yengala
Enter the
position from where to delete 6
Enter the number
of characters to be deleted 3
Sample Output:amaryngala
16.Using Non recursive given number
is palindrome or not
#include<stdio.h>
int
main(){
int num,reverse_number;
//User would input the number
printf("\nEnter any number:");
scanf("%d",&num);
//Calling user defined function to perform
reverse
reverse_number=reverse_function(num);
printf("\nAfter reverse the no is
:%d",reverse_number);
return 0;
}
int
sum=0,rem;
reverse_function(int
num){
if(num){
rem=num%10;
sum=sum*10+rem;
reverse_function(num/10);
}
else
return sum;
return sum;
}
Sample input: Enter any number: 23456
Sample out put: After reverse the no is :65432
17
.Write a C program to replace a substring with another in a given line of text.
#include<stdio.h>
#include<conio.h>
void
strreplace(char *,char,char);
int
main()
{
char s[10],chr,repl_chr;
printf("\nEnter a string: ");
scanf("%s", &s);
printf("\nEnter character to be
replaced: ");
scanf("%s", &chr);
printf("\nEnter replacement character:
");
scanf("%s", &repl_chr);
printf("\nModified string after
replacement is: ");
strreplace(s,chr,repl_chr);
getch();
return 0;
}
void
strreplace(char s[], char chr, char repl_chr)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]==chr)
{
s[i]=repl_chr;
}
i++;
}
printf("%s",s);
}
Sample output:
Enter line of
text
Snehal IS A GOOD
GIRL
Enter the word
to replace
GIRL
Enter the word
to replace with
BOY
Text
IS A GOOD BOY
18.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>
void main()
{
char name[10][8], tname[10][8],
temp[8];
int i, j, n;
printf("Enter the value
of n \n");
scanf("%d",
&n);
printf("Enter %d names
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
NamestSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
}
Sample
out put:
$ cc pgm32.c
$ a.out
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
----------------------------------------
Input Names Sorted names
------------------------------------------
heap class
stack heap
queue object
object program
class project
program queue
project stack
------------------------------------------
19.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;
}
Sample Output:
$ cc pgm8.c
$ a.out
Enter the number:
500
Roman number is be:
D
20.Write a C program
to display the contents of a file to standard output device.
#include <stdio.h>
#include <errno.h>
long count_characters(FILE *);
void main(int argc, char * argv[])
{
int i;
long cnt;
char ch, ch1;
FILE *fp1, *fp2;
if (fp1 = fopen(argv[1],
"r"))
{
printf("The FILE has been
opened...\n");
fp2 = fopen(argv[2],
"w");
cnt =
count_characters(fp1); // to count the total number of characters inside the
source file
fseek(fp1, -1L, 2); // makes the pointer fp1 to point at the
last character of the file
printf("Number of
characters to be copied %d\n", ftell(fp1));
while (cnt)
{
ch = fgetc(fp1);
fputc(ch, fp2);
fseek(fp1, -2L,
1); // shifts the pointer to the
previous character
cnt--;
}
printf("\n**File
copied successfully in reverse order**\n");
}
else
{
perror("Error
occured\n");
}
fclose(fp1);
fclose(fp2);
}
// count the total number of characters in the file that *f points to
long count_characters(FILE *f)
{
fseek(f, -1L, 2);
long last_pos = ftell(f); //
returns the position of the last element of the file
last_pos++;
return last_pos;
}
Sample out put:
$ gcc file12.c
$ cat test2
The function
STRERROR returns a pointer to an ERROR MSG STRING whose contents are
implementation defined.
THE STRING is not
MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function.
$ a.out test2
test_new
The FILE has been
opened..
Number of characters
to be copied 203
**File copied
successfully in reverse order**
$ cat test_new
.noitcnuf RORRERTS
eht ot llaC TNEUQESBUS a yb nettirwrevo ebyam dna ELBAIFIDOM ton si GNIRTS EHT
.denifed
noitatnemelpmi era stnetnoc esohw GNIRTS GSM RORRE na ot retniop a snruter
RORRERTS noitcnuf ehT
$ ./a.out test_new
test_new_2
The FILE has been
opened..
Number of
characters to be copied 203
**File copied
successfully in reverse order**
$ cat test_new_2
The function
STRERROR returns a pointer to an ERROR MSG STRING whose contents are
implementation defined.
THE STRING is not
MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function.
$ cmp test
test_new_2
21.Write a C program
which copies one file to another, replacing all lowercase characters with
their uppercase equivalents.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char *fn1,*fn2,ch;
clrscr();
printf("\n enter the source file");
gets(fn1);
printf("\n enter the destination file");
gets(fn2);
fp1=fopen(fn1,"r");
fp2=fopen(fn2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("\n unable to open file");
exit(0);
}
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch>='a'&& ch<='z')
ch=ch-32;
fputc(ch,fp2);
}
printf("\n file successfully copied");
fcloseall();
getch();
}
22.Write a C program
to merge two files into a third file
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Open two files to be merged
FILE *fp1 =
fopen("file1.txt", "r");
FILE *fp2 =
fopen("file2.txt", "r");
// Open file to store the
result
FILE *fp3 =
fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL
|| fp3 == NULL)
{
puts("Could not
open files");
exit(0);
}
// Copy contents of first file
to file3.txt
while ((c = fgetc(fp1)) !=
EOF)
fputc(c, fp3);
// Copy contents of second
file to file3.txt
while ((c = fgetc(fp2)) !=
EOF)
fputc(c, fp3);
printf("Merged file1.txt
and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
Sample Output:
Merged file1.txt and
file2.txt into file3.txt
No comments:
Post a Comment