C Program
C Program
Number Number
1. SEQUENTAL CONTROL PROGRAMS 8-20
-Program to add two numbers.
-Program to calculate total marks and percentage of
student in five subjects.
-Program to covert distance from kilometre to
meter.
-Program to convert temp.from F to C.
-Program to convert temp.from C to F.
-Program to calculate area and perimeter of
rectangle.
-Program to calculate area and circumference of a
circle.
-Program to swap two number using third variable.
-Program to swap two number without using third
variable.
-Program to show the use of width option with
scanf().
-Program to show the use of flag and width option
with printf().
-Program to input and display a character using
getch(),getche().getchar(),putch() and putchar().
7. ARRAYS 86-130
►1D ARRAY
-Program to input elements to array and then
display on screen
-Program to input elements in array and then
calculate sum and average
-Program to reverse an array
-Program to input elements in an array and then
count the number of positive, negative and zero
element
-Program to inter change corresponding elements of
two equal size array
-Program to find max element from an array
-Program to find min element from an array
-Program to search an element from an array using
linear search
-Program to search an element from an array using
binary search
-Program to sort the elements of array using bubble
sorting.
-Program to insert an elements in an unsorted array
at a specific postion
-Program to delete an element from an unsorted
array
-Program to find the largest and second largest
element from an array.
-Program to add a pointer to an integer.
-Program to subtract a pointer from an integer.
-Program to subtract 2 pointers
-Program to compare 2 pointers
-Program to pass individual aaray elements to a
fuction using call by value method.
-Program to pass individual array elements to a
fuction using call by reference method.
-Program to pass entire array to a function using
call by reference method.
-Program to show the use of array of pointers to
integers.
►2D ARRAY
-Program to add 2 matrix.
-Program to subtract 2 matrices.
Program to pass 2D array to a function.-Program to
input elementys of a 2D and then display them in a
matrix format.
-Program to find product of matrices.
-Program to calculate transpose of a matrix
-Program to calculate sum of square of diagional
elements of a matrix.
-Program to determine a square matrix is
symmetric or not
-Program to display upper triangle and lower
triangle of a square matrix.
8. STRINGS 130-154
-Program to input a string from user using scanf().
-Program to input string from the user gets() and
display using puts().
-Program to pass a string to a function and also
display ACSII value of string character.
-Program to calculate length of string (using strlen)
-Program to to calculate length of string with using
strlen()
-Program to copy one string to another
string(strcpy)
- Program to copy one string to another string
without using (strcpy)
-Program to compare two strings using(strcmp)
- Program to compare two strings without
using(strcmp)
-Program to concatenate two strings using (strcat)
- Program to concatenate two strings without using
(strcat)
-Program to reverse a string using(strrev)
- Program to reverse a string without using(strrev)
-Program to check whether the string is palindrome
or not
-Program to convert a sting into lower case using
(strlwr)
-Program to convert a sting into lower case without
using (strlwr)
-Program to convert a sting into upper case using
(strupr)
-Program to convert a sting into upper case without
using (strupr)
-Program to demonstrate 2D array of strings
-Program to demonstrate array of a pointer to
string
-Program to sort string alphabetically
-Program to count number of lower case,upper case
alphabet,digits,space and special symbols in strings
Output:
Enter first number 60
Enter second number 80
Sum=140
Output:
Enter marks in first subject 50
Enter marks in second subject 70
Enter marks in third subject 60
Enter marks in fourth subject 80
Enter marks in fifth subject 95
Total=355
Percentage=71.000000
Output:
Enter distance in kilometer 50
Distance in meters is 50000.000000
Output:
Enter the basic salary of the employee 5000
1000.000000
1500.000000
Total salary=7500.000000
Output:
Enter temp in centigrade 90
Temp in ferenheit=219.600006
Output:
Enter length 60
Enter breadth 20
Area=1200
Perimeter=160
Output:
Enter radius 67
Area=14095.459961
Circumference=420.760010
Output:
Enter the value of a6
Enter the value of b7
a=7
b=6
Output:
Enter the value of a 40
Enter the value of b 79
a=79
b=40
Output:
Enter principal 500
Enter rate 150
Enter time 40
Si=30000.000000
Output:
Enter a decimal integer 3.44
Octal representation=3
Hexadecimal representation=3
Output:
enter two numbers: 56
78
sum= ce
Output:
Enter the value of a,b,c
6
6
5
a=6,b=6,c=5
15.Program to show the use of the flag and width option with printf().
#include<stdio.h>
#include<conio.h>
void main()
{ int a=4;
clrscr();
printf(" Value of a: %03d");
}
Output:
Value of a: 004
Output:
Enter a character:
Entered value is:a
Enter a character:b
Entered value is b
Enter a character:
Enter a character:c
Entered value is:c
Enter a character:*
Entered value is:*
Enter a character:*•
Entered value is:*
Output:
Enter a number: 4
4 is positive number
Output:
Enter a number -6
-6 is negative number
Output:
Enter two numbers 50 98
98 is largest
Output:
Enter two numbers 50 69
50 is smallest
5.Program to check whether number is even or odd.
#include <stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nEnter one number");
scanf("%d",&a);
if (a%2==0)
printf("even number");
else
printf("odd number");
}
Output:
Enter one number 70
even number
Output:
enter a year 2016
year is leap
Output:
Enter three numbers 50 70 90
90 is largest
8.Program to input cost price and selling price of an item and then determine
whether profit,lovss or no profit,no loss.
#include<stdio.h>
#include<conio.h>
void main()
{ int cp,sp,amt;
clrscr();
printf("\nEnter cost price");
scanf("%d",&cp);
printf("\nEnter selling price");
scanf("%d",&sp);
if(sp>cp)
{
amt=sp-cp;
printf("\nProfit=%d",amt);
}
else if(cp>sp)
{
amt=cp-sp;
printf("\nLoss=%d",amt);
}
else
{ printf("\No profit no loss");
}
}
Output:
Enter cost price 350
Enter selling price 500
Profit=150
else
printf("%d is smallest",c);
}
}
Output:
Output:
Enter a number -39
-39 is negitive number
Output:
Enter first number 50
Enter second number 99
Enter third number 103
103 is largest
Output:
Enter the value of x 33
Value 66
Output:
Enter the value of a 40
Enter the value of b 30
Enter the value of c 70
d=-18
Imaginarey roots
Output:
Enter any character d
Uppercase alphabet=D
#include<conio.h>
void main()
char ch;
clrscr();
printf("\nEnter a character");
scanf("%c",&ch);
if(ch>='A'&&ch<='Z')
else if(ch>='0'&&ch<='9')
printf("\nDigit");
else
printf("\nSpecial symbol");
Output:
Enter a character 7
Digit
Output:
Output:
Enter three numbers 100 50 22
100 is largest
Output:
Enter three numbers 70 65 33
33 is smallest
Output:
Enter a number 73
73 is odd
Output:
Enter a number 302
302 is a positive number
23.To display bytes of memory occupied by different data types using sizeof
operator.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Memory occupied by int=%d",sizeof (int));
printf("\nMemory occupied by float=%d",sizeof (float));
printf("\nMemory occupied by char=%d",sizeof (char));
printf("\nMemory occupied by double=%d",sizeof (double));
printf("\nMemory oocupied by short int=%d", sizeof (short int));
printf("\nMemory occupied by long int=%d",sizeof (long int));
printf("\nMemory occupied by long double=%d",sizeof (long double));
printf("\nMemory occupied by unsigned char=%d",sizeof (unsigned char));
}
OUTPUT
Memory occupied by int=2
Memory occupied by float=4
Memory occupied by char=1
Memory occupied by double=8
Memory oocupied by short int=2
Memory occupied by long int=4
Memory occupied by long double=10
Memory occupied by unsigned char=1
C. ITERATION PROGRAMS
Output
Hello
Hello
Hello
Hello
Hello
Output:
Enter the value of n till u want to display natural numbers 20
20 natural numbers are
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sum=210
Output:
Enter the value of n till which you want to display even natural numbers 10
10 even natural numbers are
2
4
6
8
10
12
14
16
18
20
sum=110
Output:
Enter a number 77
77 is odd
OUTPUT
Enter marks of first subject91
Enter marks of second subject92
Total=465
Percentage=93.000000
A grade
Enter marks of first subject96
Total=490
Percentage=98.000000
A grade
output:
Hello
Hello
Hello
Hello
Hello
output:
Hello
Hello
Hello
Hello
Hello
Output:
enter a number 30
factorial of 30=1409286144
OUTPUT
enter the value of n till which you want to display natural numbers: 4
4 natural numbers
1
2
3
4
int i;
printf( "%d\n", i );
return 0;
Output:
10
reverse=101
Numbers are palindrome
OUTPUT
Enter the value of n tell which you want to display natural numbers8
#include<stdio.h>
#include<conio.h>
void main ()
{
int i=1,j,n;
long int fact=1;
float sum=0.0;
clrscr();
printf("\nEnter a number");
scanf("%d",&n);
while(i<=n)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
if(i<n)
printf("%d/%d!+",i,i);
else
printf("%d/%d!",i,i);
sum=sum+(float)i/fact;
i++;
}
printf("=%f",sum);
}
OUTPUT
Enter a number3
1/1!+2/2!+3/3!=2.250000
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=5;j>=i;j--)
printf("*");
printf("\n");
getch();
Output:
*****
****
***
**
23.To display all 3 digits well ordered numbers less than 200.
#include<stdio.h>
#include<conio.h>
void main()
{ int i,n,a,rem1,rem2,rem3,j;
clrscr();
printf("\n 3digit well order no less than 200 are");
for(j=100;j<=200;j++)
{ a=j;
i=j;
rem1=i%10;
i=i/10;
rem2=i%10;
i=i/10;
rem3=i%10;
i=i/10;
if(rem1>rem2&&rem2>rem3)
printf("%d",a);
}
getch();
}
OUTPUT:
3 digit welloder no less than 200:
126
127
128
129
134
135
136
137
138
139
145
146
147
148
149
156
157
158
159
167
168
169
178
179
189
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
for(i=1;i<=3;i++)
{
if(i==2)
{
break;
}
printf("\n%d",i);
}
}
OUTPUT
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf("\n%d%d",i,j);
}
}
}
OUTPUT
11
12
21
22
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
for(i=1;i<=3;i++)
{
if(i==2)
{
continue;
}
printf("\n%d",i);
}
}
OUTPUT
1
3
if(i==n)
{
printf("\n %d",n);
delay(50);
}
}
}
Output
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,d,bin=0,i=0;
clrscr();
printf(" \n enter a decimal number");
scanf("%d",&n);
while(n>0)
{
d=n%2;
bin=bin+d*pow(10,i);
n=n/2;
i++;
}
printf("\n Binary number=%d",bin);
}
OUTPUT
enter a decimal number 23
Binary number=10111
#include<stdio.h>
#include<conio.h>
void main()
{ int day;
clrscr();
printf("\nEnter a day number between 1 and 7");
scanf("%d",&day);
switch(day)
{ case 1:printf("Monday");
break;
case 2:printf("Tuesday");
break;
case 3:printf("Wednesday");
break;
case 4:printf("Thursday");
break;
case 5:printf("Friday");
break;
case 6:printf("Saturday");
break;
case 7:printf("Sunday");
break;
default:printf("Invalid number");
}
Output:
Saturday
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
int a,b,choice;
float result;
clrscr();
printf("\nMenu");
printf("\n1Addition \n2Subtraction \n3Multiplication \n4Division \n5Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a+b;
printf("\nSum of %d and %d=%f",a,b,result);
break;
case 2:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a-b;
printf("\nDifference between %d and %d=%f",a,b,result);
break;
case 3:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a*b;
printf("\nResult=%f",result);
break;
case 4:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
if(b==0)
printf("Divide by zero error");
else
{
result=(float)a/b;
printf("\nDivision of %d and %d=%f",a,b,result);
break;
}
case 5:exit(0);
default:printf("\nInvalid choice");
}
}
OUTPUT:
Menu
1Addition
2Subtraction
3Multiplication
4Division
5Exit
Enter your choice: 4
Enter two numbers10 5
E. Goto Statements
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<=3;i++)
if(i==2)
goto end;
else
printf("\n%d",i);
}
end:printf("\nEnd of program");
Output:
End of program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int x;
float y;
clrscr();
scanf("%d",&x);
if(x<0)
goto input;
else
{
y=sqrt(x);
Output:
F. Function Programs.
#include<stdio.h>
#include<conio.h>
int calculate(int,int);
void main()
{
int a,b,sum;
clrscr();
printf(" \n enter two numbers");
scanf(" %d %d",&a,&b);
sum=calculate(a,b);
printf("\n sum of %d and %d=%d",a,b,sum);
}
int calculate(int x,int y)
{
int z;
z=x+y;
return(z);
}
OUTPUT:
enter two numbers 8 7
#include<stdio.h>
#include<conio.h>
void even_odd(int);
void main()
int n;
clrscr();
printf("Enter a number");
scanf("%d",&n);
even_odd(n);
void even_odd(int n)
if(n%2==0)
else
Output:
Enter a number7
7 is odd number
#include<stdio.h>
#include<conio.h>
void message1();
void message2();
void main()
clrscr();
message1();
message2();
message1();
void message1()
printf("\nHello");
void message2()
printf("\nBye");
Output:
Hello
Bye
Hello
#include<stdio.h>
#include<conio.h>
void swap (int,int);
void main()
{
int a,b;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
swap(a,b);
printf("\n values of variables after calling swap function a=%d,b=%d",a,b);
}
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
printf("\n values in swap function after swapping a=%d.b=%d",a,b);
}
Output:
enter two numbers 26 28
values in swap function after swapping a=28.b=26
values of variables after calling swap function a=26,b=28
5. Program to swap two numbers using call by reference method.
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("\n values after calling swap function a=%d,b=%d", a,b);
}
void swap (int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\n values in swap function are %d %d",*x,*y);
}
Output:
enter two numbers 12 23
values in swap function are 23 12
values after calling swap function a=23,b=12
6. Program to calculate sum and difference of two number using
function).
#include<stdio.h>
#include<conio.h>
void operations (int,int,int*,int*);
void main()
{
int a,b,sum,diff;
clrscr();
printf(" enter two numbers");
scanf("%d %d",&a,&b);
operations(a,b,&sum,&diff);
printf("\n sum of %d and %d=%d",a,b,sum);
printf("\n diff of %d and %d =%d",a,b,diff);
}
void operations(int a,int b,int *s,int *d)
{
*s=a+b;
*d=a-b;
}
Output:
enter two numbers 3 2
sum of 3 and 2=5
diff of 3 and 2 =1
#include<stdio.h>
#include<conio.h>
int natural(int);
void main()
{
int n,sum;
clrscr();
printf("\n enter value of n");
scanf("%d", &n);
sum=natural(n);
printf("\n sum of first %d natural numbers=%d", n,sum);
}
int natural (int n)
{
static int s;
if(n==0)
return(s);
else
{
s=s+n;
return(natural (n-1));
}
}
OUTPUT:
enter value of n 4
sum of first 4 natural numbers=10
#include<stdio.h>
#include<conio.h>
int fibo(int);
void main()
{
int n,i;
clrscr();
printf("\n enter number of terms");
scanf("%d",&n);
printf("\n fibonacci series is");
for(i=1;i<=n;i++)
{
printf("\n %d", fibo(i));
}
}
int fibo(int a)
{
if(a==1)
return(0);
else if(a==2)
return(1);
else
return(fibo(a-1)+fibo(a-2));
}
Output:
enter number of terms 8
fibonacci series is
0
1
1
2
3
5
8
13
10. Program to find sum of digits of a number using recursion.
#include<stdio.h>
#include<conio.h>
int digit(int);
void main()
{
int n,sum;
clrscr();
printf("\n enter a number");
scanf("%d",&n);
sum=digit(n);
printf("\n sum of digits of %d =%d",n,sum);
}
int digit(int n)
{
int d;
static int s;
if(n==0)
return(s);
else
{
d=n%10;
s=s+d;
return(digit(n/10));
}
}
Output:
enter a number123
sum of digit of 123 =6
recursion.
#include<stdio.h>
#include<conio.h>
int calculate(int,int);
void main()
{
int a,b,hcf,lcm;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
hcf=calculate(a,b);
lcm=a*b/hcf;
printf("\n hcf=%d lcm=%d", hcf,lcm);
}
int calculate(int a, int b)
{
if(a%b==0)
return(b);
else
return(calculate(b,a%b));
}
Output:
enter two numbers 6 8
hcf=2 lcm=24
G. ARRAYS
on the screen.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n;
clrscr();
printf("\n Enter number of array elements ");
scanf("%d",&n);
printf("\n Enter %d elements \n",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\n Enter array elements are \n");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
Output:
Enter number of array elements 4
Enter 4 elements
22
88
99
55
Enter array elements are
22
88
99
55
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,sum=0;
float avg;
clrscr();
printf("\n Enter the number of array elements ");
scanf(" %d ",&n);
printf("\n enter %d elements ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
avg=(float)sum/n;
printf("\n sum=%d avg=%f", sum,avg);
}
Output:
Enter the number of array elements 3 2
enter 3 elements 4 5 6
sum=11 avg=3.666667
#include<stdio.h>
#include<conio.h>
void main()
int a[20],temp[20],i,j,n;
clrscr();
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
for(i=n-1,j=0;i>=0;i--,j++)
temp[j]=a[i];
for(i=0;i<n;i++)
a[i]=temp[i];
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
Output:
Enter 4 numbers
10
20
30
40
Numbers in reverse order are 40 30 20 10
#include<stdio.h>
#include<conio.h>
void main()
int i,a[20],n;
int countn=0,countz=0,countp=0;
clrscr();
printf("Enter numbers");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
if (a[i]==0)
countz++;
else if(a[i]>0)
countp++;
else
countn++;
getch();
Output:
Enter numbers 4
20
-33
70
sized array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],b[30],i,n,c;
clrscr();
printf("\n Enter the number of array elements ",n);
scanf("%d",&n);
printf("\n enter %d elements for first array\n ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", &a[i]);
}
printf("\n enter %d elements for second array \n",n);
for(i=0;i<=n-1;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<=n-1;i++)
{
c=a[i];
a[i]=b[i];
b[i]=c;
}
printf("\n elements of first array after interchange",n);
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
printf("\n elements of second array after interchange",n);
for(i=0;i<=n-1;i++)
printf("\n %d",b[i]);
}
Output:
Enter the number of array elements 3
#include<stdio.h>
#include<conio.h>
void main()
{ int n,i,a[30],max,loc;
clrscr();
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
max=a[0];
loc=0;
for (i=1;i<=n-1;i++)
if (a[i]>max)
{
max=a[i];
loc=i;
Output:
Enter 4 elements
10
99
20
72
#include<conio.h>
void main()
{ int n,i,a[30],min,loc;
clrscr();
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
min=a[0];
loc=0;
for (i=1;i>=n-1;i++)
if (a[i]<min)
min=a[i];
loc=i;
Output:
Enter 5 elements
search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30], i, n, loc=-1, item;
clrscr();
printf("\n Enter number of element",n);
scanf("%d",&n);
printf("\n Enter %d elements",n);
for(i=0;i<n-1;i++)
scanf("%d",&a[i]);
printf("\n Enter element to be searched");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(a[i]==item)
{
loc=i;
printf("\n %d found at location %d",item,loc);
break;
}
}
if(loc==-1)
printf("%d not found in list", item);
}
Output:
Enter number of element 5
Enter 5 elements
15 14 13 12
search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,item, beg, end, mid,loc;
clrscr();
printf("Enter the number of array elements ");
scanf("%d",&n);
printf("\n enter %d elements ",n);
for(i=0;i<=n-1;i++)
scanf("%d", &a[i]);
printf("\n enter element to be searched ");
scanf("%d",&item);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
printf("\nitem is found at location %d",item,loc);
break;
}
else if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
}
if(beg>end)
printf("\n element is not found");
}
Output:
Enter the number of array elements 3
enter 3 elements
12
34
56
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],j,temp;
clrscr();
printf("\n Enter number of elements");
scanf("%d",&n);
printf("\n Enter %d element",n);
for(i=0;i<=n-1;i++)
scanf("%d", &a[i]);
for(i=0;i<=n-2;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n sorted list");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
Output:
Enter the number of array elements 3
enter 3 elements
12
34
56
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],loc,item;
clrscr();
printf("\nEnter number of elements");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\nEnter item to be inseted");
scanf("%d",&item);
printf("\nEnter location");
scanf("%d",&loc);
for(i=n-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=item;
n=n+1;
printf("\nNew array after insertion");
for (i=0;i<=n-1;i++)
printf("\n%d",a[i]);
}
Output:
Enter number of elements 4
Enter 4 elements
6
8
9
2
Enter item to be inseted 10
Enter location 3
New array after insertion
6
8
9
10
2
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,item,loc=-1;
clrscr();
printf("\nEnter Number of elements in an array");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\nEnter item to be deleted");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(a[i]==item)
loc=i;
}
if(loc==-1)
printf("item is not present in an array");
else
{
for(i=loc;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
printf("\nNew list after delection");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
}
OUTPUT:
Enter Number of elements in an array 4
Enter 4 elements
12
34
56
78
an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],first,second;
clrscr();
printf("\nEnter number of elements");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
first=a[0];
second=a[0];
for(i=1;i<=n-1;i++)
{
if(a[i]>first)
{
second=first;
first=a[i];
}
else if (a[i]>second)
second=a[i];
}
printf("\nFirst largest value=%d",first);
printf("\nSecond largest value=%d",second);
}
OUTPUT
Enter number of elements6
Enter 6 elements
9
10
15
99
153
44
First largest value=153
Second largest value=99
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p;
clrscr();
p=&a[1];
p=p+1;
printf("\n %d",*p);
}
OUTPUT
30
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p;
clrscr();
p=&a[1];
p=p-1;
printf("\n %d",*p);
}
OUTPUT
10
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p,*k;
clrscr();
p=&a[1];
k=&a[3];
printf("\n %d",k-p);
}
OUTPUT
2
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p,*k;
clrscr();
p=&a[0];
k=a;
if(p==k)
printf("Both pointers printing at the same elements");
else
printf("\n Pointers are pointing at different element");
}
OUTPUT
Both pointers printing at the same elements
18.Program to pass individual array elements to a function using
#include<stdio.h>
#include<conio.h>
void display(int);
void main()
{
int a[3]={10,20,30};
int i;
clrscr();
for(i=0;i<=2;i++)
display (a[i]);
}
void display(int m)
{
printf("\n %d",m);
}
OUTPUT
10
20
30
OUTPUT
10
20
30
reference method.
#include<stdio.h>
#include<conio.h>
void display(int[],int);
void main()
{
int a[3]={10,20,30};
clrscr();
display(a,3);
}
void display(int a[], int n)
{
int i;
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
OUTPUT
10
20
30
integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p[3];
int m=5,n=7,k=3,i;
p[0]=&m;
p[1]=&n;
p[2]=&k;
printf("\n Values are \n");
for(i=0;i<=2;i++)
printf("\n %d",*p[i]);
}
OUTPUT
Values are
5
7
3
► 2D Array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j;
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
OUTPUT
Enter number of rows and columns 2 2
Enter elements
a[0][0]=6
a[0][1]=5
a[1][0]=9
a[1][1]=3
Matrix is
6 5
9 3
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j,b[10] [10],c[10] [10];
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\nSum is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
OUTPUT
Enter elements of first matrix
a[0][0]=6
a[0][1]=3
a[1][0]=1
a[1][1]=2
Enter elements of second matrix
b[0][0]=6
b[0][1]=3
b[1][0]=5
b[1][1]=3
Sum is
12 6
6 5
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j,b[10] [10],c[10] [10];
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\nDifference is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
OUTPUT:
Enter elements of first matrix
a[0][0]=6
a[0][1]=3
a[1][0]=5
a[1][1]=9
Enter elements of second matrix
b[0][0]=4
b[0][1]=6
b[1][0]=3
b[1][1]=2
Difference is
2 -3
2 7
#include<stdio.h>
#include<conio.h>
void display(int[][3],int ,int);
void main()
{
int a[2][3]={{10,20,30},
{40,50,60}
};
clrscr();
display(a,2,3);
}
void display(int a[][3],int m,int n)
{
int i,j;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
OUTPUT
10 20 30
40 50 60
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],m1,n1,m2,n2,i,j,k;
int c[10][10]={0};
clrscr();
printf("\nEnter number of rows and columns of first matrix");
scanf("%d%d",&m1,&n1);
printf("\nEnter number of rows and columns of second matrix");
scanf("%d%d",&m2,&n2);
if(n1!=m2)
printf("\nMultiplication is not possible");
else
{
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n1-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m2-1;i++)
{
for(j=0;j<=n2-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n1-1;j++)
{
for(k=0;k<=n2-1;k++)
{
c[i][j]=c[i][j]+a[i][j]*b[i][j];
}
}
}
printf("\nMatrix is \n");
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n2-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}
OUTPUT
Enter elements of first matrix
a[0][0]=1
a[0][1]=2
a[1][0]=3
a[1][1]=4
Enter elements of second matrix
b[0][0]=1
b[0][1]=4
b[1][0]=3
b[1][1]=1
Matrix is
2 16
18 8
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],t[10][10],m,n,i,j;
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nTranspose of matrix is\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
t[i][j]=a[j][i];
printf("%d\t",t[i][j]);
}
printf("\n");
}
}
OUTPUT
Enter number of rows and columns 2 3
Enter elements of matrix
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
Transpose of matrix is
1 4
2 5
3 6
matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],n,i,j;
int sum=0;
clrscr();
printf("\nEnter size of matrix");
scanf("%d",&n);
printf("\nEnter elements \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i==j)
sum=sum+a[i][j]*a[i][j];
}
}
printf("\nSum=%d",sum);
}
OUTPUT
Enter size of matrix 3
Enter elements
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Sum=107
OUTPUT
Enter size of matrix 2
Enter elements of matrix
a[0][0]=6
a[0][1]=8
a[1][0]=4
a[1][1]=2
Transpose of matrix is
6 4
8 2
Not a symmetric matrix
square matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\nEnter size of matrix");
scanf("%d",&n);
printf("\nEnter elements of matrix \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Upper triangle is \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i<j)
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Lower triangle is ");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i>j)
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
Output:
Enter size of matrix 3
Enter elements of matrix
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Upper triangle is
2 3
6
Lower triangle is
4
7 8
H. Strings
1. Program to input a string from the user using scanf().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char name[20];
clrscr();
scanf("%s",&name);
printf("\nHello %s",name);
getch();
Output:
Hello harkirat
2. Program to input a string from user using gets() and display using
puts().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
clrscr();
printf("\n enter your name");
gets(name);
puts("Hello");
puts(name);
}
OUTPUT
Enter your name Krishna
Hello
Krishna
of string characters.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void display(char[]);
void main()
{
char str[20];
clrscr();
puts("Enter a string");
gets(str);
display(str);
}
void display(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
printf("\n Character=%c Ascii value=%d",str[i],str[i]);
}
OUTPUT
Enter a string
bca class
function strlen().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
int z;
clrscr();
printf("enter a string:");
gets(str);
z=strlen(str);
printf("lenght of string:%d",z);
getch();
Output:
enter a string:hello
lenght of string:5
function strlen().
#include<stdio.h>
#include<conio.h>
#include<string.h>
int str_length(char[]);
void main()
char str[81];
int k;
clrscr();
printf("Enter a string:");
gets(str);
k=str_length(str);
getch();
int i=0;
while(s[i]!='\0')
i++;
return(i);
Output:
function strcpy().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char source[20],target[20];
clrscr();
printf("\nEnter a string");
gets(source);
strcpy(target,source);
Output:
strcpy().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char source[20],target[20];
int i;
clrscr();
printf("\nEnter a string");
gets(source);
strcpy(target,source);
for(i=0;source[i]!='\0';i++)
target[i]=source[i];
target[i]='\0';
Output:
strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char s1[20],s2[20];
clrscr();
gets(s1);
gets(s2);
if(strcmp(s1,s2)==0)
printf("Equal strings");
else
printf("unequal strings");
Output:
Enter first string hello
unequal strings
function strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int l1,l2,i,end,flag=0;
char s1[20],s2[20];
clrscr();
gets(s1);
gets(s2);
l1=strlen(s1);
l2=strlen(s2);
if(l1>l2)
end=l1;
else
end=l2;
for(i=0;i<=end;i++)
if(s1[i]!=s2[i])
printf("unqual strings");
flag=1;
break;
if(flag==0)
printf("equal strings");
Output:
equal strings
strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char s1[81],s2[81];
clrscr();
gets(s1);
gets(s2);
if(strcmp(s1,s2)==0)
else if(strcmp(s1,s2)>0)
else
getch();
Output:
strcat().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char s1[30]="Good";
char s2[20]="Morning";
clrscr();
strcat(s1,s2);
printf("\nConcatenate string=%s",s1);
Output:
Concatenate string=GoodMorning
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char a[80];
clrscr();
gets(a);
strrev(a);
printf("\nString after reversing:%s",a);
getch();
Output:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void str_reverse(char[]);
void main()
char s[81];
clrscr();
printf("Enter a string:");
gets(s);
str_reverse(s);
getch();
{
char s2[80];
int i,j,count=0;
for(i=0;str[i]!='\0';i++)
++count;
s2[i]=str[i];
for(i=count-1,j=0;i>=0;i--,j++)
str[j]=s2[i];
str[j]='\0';
Output:
Enter a string:Welcome
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\n Enter a string ");
gets(s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s2,s2)==0)
{
printf("strings are palindrome");
}
else
printf(" Stringa are not palindrome");
}
Output
Enter a string harkirat
strings are palindrome
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
clrscr();
puts("Enter a string");
gets(str);
Output:
Enter a string
HELLO EVERYONE
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int i;
char str[20];
clrscr();
puts("Enter a string");
gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
Output:
Enter a string
HELLO EVERYONE
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
clrscr();
puts("Enter a string");
gets(str);
Output:
Enter a string
hello everyone
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int i;
char str[20];
clrscr();
puts("Enter a string");
gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
Output:
Enter a string
hello everyone
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char*name[]={"Keyboard","Mouse","Printer"};
clrscr();
int i;
for(i=0;i<=2;i++)
puts(name[i]);
Output:
Keyboard
Mouse
Printer
#include<stdio.h>
#include<string.h>
#include<conio.h>
void str_sort(char[]);
void main()
char str[81];
clrscr();
gets(str);
str_sort(str);
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i, u=0,l=0,d=0,s=0;
clrscr();
puts("Enter a string");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='A' && str[i]<='Z')
u++;
if(str[i]>='a' && str[i]<='z')
l++;
else if (str[i]>='0' && str[i]<='9')
d++;
else
s++;
}
printf("\n uppercase=%d \n lowercase=%d \n digit=%d \n special symbol=%d",
u,l,d,s);
}
OUTPUT
Enter a string
comp123&*$
uppercase=0
lowercase=4
digit=3
special symbol=4
#include<stdio.h>
#include<conio.h>
struct student
int rollno;
char name[20];
float percentage;
};
void main()
{
struct student s1,s2;
clrscr();
printf("Roll number\tName\tPercentage");
scanf("%d%s%f",&s1.rollno,s1.name,&s1.percentage);
scanf("%d%s%f",&s2.rollno,s2.name,&s2.percentage);
printf("%d\t%s\t%0.2f",s1.rollno,s1.name,s1.percentage);
printf("\n%d\t%s\t%0.2f",s2.rollno,s2.name,s2.percentage);
getch();
Output:
#include<conio.h>
#include<string.h>
struct
char title[20];
int pages;
float price;
}book1;
void main()
clrscr();
strcpy(book1.title,"Learn C");
book1.pages=528;
book1.price=320.50;
printf("Book's title=%s",book1.title);
printf(",pages=%d",book1.pages);
printf(",price=%0.2f",book1.price);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
struct student
int rollno;
char name[20];
float percentage;
};
void main()
clrscr();
printf(",name=%s",s1.name);
printf(",percentage=%0.2f",s1.percentage);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct book
char publisher[20];
char title[20];
int discount;
};
void main()
clrscr();
b2=b1;
strcpy(b2.title,"System software");
printf("\nBook title=%s,",b2.title);
printf("\t Publisher=%s,",b2.publisher);
printf("\t Discount=%d",b2.discount);
getch();
Output:
memory locations?
#include<stdio.h>
#include<conio.h>
struct student
int rollno;
char name[20];
float percentage;
};
void main()
clrscr();
printf("\nAddress of rollno=%u",&s1.rollno);
printf("\nAddress of name=%u",&s1.name);
printf("\nAddress of percentage=%u",&s1.percentage);
getch();
Output
Address of rollno=65500
Address of name=65502
Address of percentage=65522
#include<stdio.h>
#include<conio.h>
struct student
int rollno;
char name[20];
int marks[3];
};
void main()
int i,n,loc=0;
float large,percentage[10];
clrscr();
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("enter details of %d students",i);
scanf("%d%s%d%d%d",&s[i].rollno,&s[i].name,&s[i].marks[0],&s[i].marks[1],&s[i]
.marks[2]);
percentage[i]=(float)((s[i].marks[0]+s[i].marks[1]+s[i].marks[2]))/3;
large=percentage[0];
for(i=1;i<n;i++)
if(large<percentage[i])
large=percentage[i];
loc=1;
printf("\nRoll
no=%d,Name=%s,Percentage=%0.2f",s[loc].rollno,s[loc].name,percentage[loc]);
getch();
Output:
Roll no=213,Name=tanwar,Percentage=78.33
#include<stdio.h>
#include<conio.h>
struct employee
int emp_code;
char emp_name[20];
int dept_code;
double salary;
};
void main()
{101,"Raj",5,21525.70},
{102,"Paul",6,17112.50},
{103,"Arun",4,1167.50}
};
int i;
clrscr();
printf("Employee Information");
for(i=0;i<3;i++)
printf("\nEmployee code:%d",emp[i].emp_code);
printf("\tName:%s",emp[i].emp_name);
printf("\tDepartment: %d",emp[i].dept_code);
printf("\tSalary:%0.2f",emp[i].salary);
getch();
Output
Employee Information
#include<stdio.h>
#include<conio.h>
struct date
int day;
int month;
int year;
};
struct birthdaylist
char name[25];
};
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
printf("Birth date(dd-mm-yyyy)\tName\n");
for(i=0;i<n;i++)
scanf("%d-%d-
%d%s",&p[i].DOB.day,&p[i].DOB.month,&p[i].DOB.year,p[i].name);
for(i=0;i<n;i++)
printf("\nName of person=%s",p[i].name);
printf("\tDate of
birth=%d/%d/%d",p[i].DOB.day,p[i].DOB.month,p[i].DOB.year);
}
getch();
Output:
25-05-1988 Anil
17-11-1978 Kapil
#include<stdio.h>
#include<conio.h>
struct date
int dd;
int mm;
int yy;
}today;
void main()
struct date*ptr;
clrscr();
ptr=&today;
scanf("%d%d%d",&today.dd,&today.mm,&today.yy);
printf("%d/%d/%d",(*ptr).dd,(*ptr).mm,(*ptr).yy);
getch();
Entered date is
18/11/2016
10. To show how members of structure are accessed using a
#include<stdio.h>
#include<conio.h>
struct date
int dd;
int mm;
int yy;
};
struct birthdaylist
char name[25];
};
void main()
clrscr();
printf("Name=%s",p2->name);
getch();
Output:
#include<stdio.h>
#include<conio.h>
typedef struct
int num;
int deno;
}fraction;
void mul_of_frac(fraction,fraction);
void main()
fraction f1={2,3};
fraction f2={4,5};
clrscr();
mul_of_frac(f1,f2);
getch();
fraction res;
res.num=fr1.num*fr2.num;
res.deno=fr1.deno*fr2.deno;
printf("%d/%d",res.num,res.deno);
Output
Reference method.
#include<stdio.h>
#include<conio.h>
struct book
char*title;
int pages;
int price;
};
void main()
clrscr();
display(&b);
ptr->pages=555;
ptr->price=295;
return;
Output
I.DATA FILES
while((ch=fgetc(fs))!=EOF)
{
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
printf("\nFile copied");
}
4.Program to read and write in afile character by character
#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fp;
char str[100];
char ch;
int i=0;
clrscr();
fp=fopen("file1.txt","w");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("\nEnter text you want to insert into file");
gets(str);
while(str[i]!='\0')
{
fputc(str[i],fp);
i++;
}
printf("\nEnter text you want to insert into file:");
gets(str);
while(str[i]!='\0')
{
fputc(str[i],fp);
}
printf("\nContents written in file");
fclose(fp);
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("\nContents of file are:\n");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
getch();
}