You are on page 1of 44

//w.a.p. to swap 3 no.

s without using 4th variable//


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter three no");
scanf("%d%d%d",&a,&b,&c);
a=a+b+c;
b=a-b-c;
c=a-b-c;
a=a-b-c;
printf("%d%d%d",a,b,c);
getch();
}
***output***
enter three no 2
4
6
6 2 4

//w.a.p. calculating area and circumference of circle//


#include<stdio.h>
#include<conio.h>
void main()
{
float r,a,c;
clrscr();
printf("Enter radius of circle\n");
scanf("%f",&r);
a=3.142*r*r;
c=2*3.142*r;
printf("area of circle = %f\n",a);
printf("circumference of circle = %f",c);
getch();

}
***output***
Enter radius of circle
5
area of circle = 78.550003
circumference of circle = 31.420000

1
//w.a.p. leap year//
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("enter year");
scanf("%d",&y);
if((y%4==0 && y%100!=0) || (y%400==0))
{
printf("leap year");
}
else
{
printf("not a leap year");
}
getch();
}
***output***
enter year 2017
not a leap year

2
// w.a.p. for switch case//
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("enter a no");
scanf("%d",&a);
switch(a)
{
case 1:
printf("sunday");
break;

case 2:
printf("monday");
break;

case 3:
printf("tuesday");
break;

case 4:
printf("wednesday");
break;

case 5:
printf("thrusday");
break;

case 6:
printf("friday");
break;

case 7:
printf("saturday");
break;
default:
printf("entered wrong value");
}
getch();
}

***output***
enter a no 1
sunday

3
// w.a.p. for switch case//
#include<stdio.h>
#include<conio.h>
void main()
{
char op;
int a,b,c;
clrscr();
printf("enter value of a,b");
scanf("%d%d",&a,&b);
printf("enter operater");
fflush(stdin);
scanf("%c",&op);
switch(op)
{
case '+':
c=a+b;
break;

case '-':
c=a-b;
break;

case '/':
c=a/b;
break;

case '*':
c=a*b;
break;

case '%':
c=a%b;
break;

default:
printf("wrong operator");
}
printf("value=%d",c);
getch();
}
***output***
enter value of a,b 2 4
enter operater+
value=6

4
//w.a.p. find greatest of 3 numbers using nested if else//
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)

printf("a is greatest");

else
{
if(b>a && b>c)

printf("b is greatest");

else

printf("c is greatest");

}
getch();
}
***output***
enter three numbers
2
4
6
c is greatest

5
//w.a.p. roots of quadratic equation //
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x1,x2;
clrscr();
printf("enter value of a b c\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
printf("roots are real and unequal\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))*(2*a);
printf("x1=%f\n",x1);
printf("x2=%f\n",x2);
}
else
{
if(d<0)
{
printf("roots are imaginary");
}
else
{
printf("roots are equal");
x1=-b/(2*a);
x2=x1;
printf("x1=%f\n",x1);
printf("x2=%f\n",x2);
}
}
getch();

}
***output***
Enter value of a b c 2 4 7
roots are imaginary

6
//w.a.p. no. is palindrome or not//
#include<stdio.h>
#include<conio.h>
void main()
{
int r,rev=0,n,rev1;
clrscr();
printf("enter value of n");
scanf("%d",&n);
rev1=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev1==rev)
{
printf("no is palindrome");
}
else
{
printf("no is not palindrome") ;
}
getch();
}
***output***
enter value of n 200
no is not palindrome

7
// w.a.p. to check if no. is armstrong or not//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,copy;
clrscr();
printf("enter a no");
scanf("%d",&n);
copy=n;
while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(copy==sum)
{
printf("armstrong");
}
else
{
printf("not armstrong");
}
getch();
}
***output***
enter a no 153
Armstrong

8
// w.a.p. to find factorial of a no. using DO WHILE LOOP//
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1,n,i;
clrscr();
printf("enter value of n");
scanf("%d",&n);
i=n;
do
{
fact=fact*i;
i--;
}while(i!=0);
printf("factorial is = %d",fact);

getch();
}
***output***
enter value of n 5
factorial is = 120

// w.a.p to print fibonicce series upto n terms //


#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("enter value of n ");
scanf("%d",&n);
printf("%d\n",a);
printf("%d\n",b);
i=3;
while(i<=n)
{
c=a+b;
printf("%d\n",c);
a=b;
b=c;
i++;
}
getch();
}

***output***
enter value of n 10
0
1
1
2
3
5
8
13
21
34

9
//w.a.p. to show sum of series 1+3+5….//
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter n\n");
scanf("%d",&n);
for(i=1;i<=n;i+=2)
{
sum = sum+i;
}
printf("Sum=%d",sum);
getch();
}
//***output***
Enter n
5
Sum=9

//w.a.p.sum of series 1+1/2+1/3......//


#include<stdio.h>
#include<conio.h>
void main()
{
float n,i,sum=0;
clrscr();
printf("enter no.");
scanf("%f",&n);
for(i=1;i<n;i++)
{
sum=sum+1/i;
}
printf("Sum is %f",sum);
getch();
}
***output***
enter no.5
Sum is 2.083333

10
//w.a.p to check no. is prime or not//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j=0;
clrscr();
printf("Enter a positive integer");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
j=1;
break;
}
}
if(j==0)
{
printf("%d is a prime number",n);
}
else
printf("%d is not a prime number",n);
getch();
}
***output***
Enter a positive integer 7
7 is a prime number

//w.a.p to print all combination of 1,2,3//


#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=0,n3=0;
clrscr();
printf("combination of 1,2,3 are");
for(n1=1;n1<=3;n1++)
{
for(n2=1;n2<=3;n2++)
{
for(n3=1;n3<=3;n3++)
{
if(n1!=n2&&n2!=n3&&n1!=n3)
{
printf("\n%d%d%d\n",n1,n2,n3);
}
}
}
}
getch();
}

11
***output***
combination of 1,2,3 are
123

132

213

231

312

321

//w.a.p to print all prime no. upto N//


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,status,a;
clrscr();
printf("enter no. upto which you want to see prime no.s");
scanf("%d",&n);
printf("Prime no. upto %d:\n",n);
{
for(j=2;j<=n;j++)
{
status=0;
for(i=2;i<=j/2;i++)
{
if(j%i==0)
{
status=1;
break;
}
}
if(status==0)
printf("%d\n",j);
}
getch();
}
}
***output***
enter no. upto which you want to see prime no.s 10
Prime no. upto 10:
2
3
5
7

12
//w.a.p for any pattern//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("enter size of pattern \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=i;j>=1;j--)
{
printf("%d",j);
}
for(j=2;j<=i;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}
***output***
enter size of pattern 4

1
2 12
32 123
43 2 1 2 3 4

13
//w.a.p. of pattren *** //
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}

for(j=1;j<=i;j++)
{
printf("*");
}
for(j=1;j<=i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
***output***
enter value of n 5
*
***
*****
*******
*********

14
//w.a.p to print any pattern//
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
int c=1;
printf(“enter n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,c%2);
c++;
}
if(i%2==0)
{
c=1;
}
else
{
c=0;
}

}
getch();
}
***output***
enter n 5
1
01
101
0101
10101

15
//w.a.p of factorial of n using function without return value//
#include<stdio.h>
#include<conio.h>
void main()
{
void fact(int);
int n;
clrscr();
printf("enter no.\n");
scanf("%d",&n);
fact(n);
getch();
}
void fact(int n)
{
int f=1,j;
for(j=1;j<=n;j++)
{
f=f*j;
}
printf("factorial of number is %d",f);
}
***output***
enter no.
4
factorial of number is 24

//w.a.p. swapping using call by value//


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void swap(int,int);
clrscr();
printf("Enter a and b\n");
scanf("%d%d",&a,&b);
swap(a,b);

getch();
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("After swapping a=%d and b=%d",a,b);
}

16
***output***
Enter a and b
7
8
After swapping a=8 and b=7

//w.a.p swap using call by reference//


#include<conio.h>
#include<stdio.h>
void main()
{
void swap(int*,int*);
int a,b;
clrscr();
printf("enter value of a,b\n");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("value of a=%d and b=%d after swap",a,b);
getch();
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
}
***output***
enter value of a,b
5 9
value of a=9 and b=5 after swap

17
//w.a.p to find factorial using recursion//
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
long int f;
long fact (int);
clrscr();
printf("enter n");
scanf("%d",&n);
f=fact(n);
printf("factorial=%ld",f);
getch();

}
long int fact(int n)
{
long int f1;
if(n==1)
{
f1=1;
}
else
{
f1=n*fact(n-1);
}
return(f1);
}

***output***
enter n 5
factorial=120

18
//w.a.p. xn using recursion//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x;
long p;
long power(int,int);
clrscr();
printf("enter x and n");
scanf("%d%d", &x,&n);
p=power(x,n);
printf("power=%ld",p);
getch();
}
long power(int x,int n)
{
if(n==0)
{
return(1);
}
else
{
return(x * power (x,n-1));
}
}
***output***
enter x and n 5 4
power=625

// w.a.p. to show static storage class//


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,sum=0;
clrscr();
printf("enter no. of elements");
scanf("%d",&n);
printf("enter array a");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("sum=%d",sum);
getch();
}

19
***output***
enter no. of elements 4
enter array a1
2
3
4
sum=10

//w.a.p for largest element in 1-d array//


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,n,max;
clrscr();
printf("enter how many no");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];

for(i=0;i<n;i++)
{
if(a[i]<a[i+i])
{
max=a[i];

}
printf("largest element is %d ",max);
getch();
}
***output***
enter how many no 5
1
2
3
4
5
largest element is 5

20
// write a program to insert an element in 1D array //
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,n,item,loc;
clrscr();
printf("Enter the size");
scanf("%d",&n);
printf("\nEnter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the location");
scanf("%d",&loc);
printf("\nEnter the item to be inserted");
scanf("%d",&item);
for(j=n-1;j>=loc;j--)
{
a[j+1]=a[j];
}
a[loc]=item;
n=n+1;
printf("\nAfter insertion");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

***output***
Enter the size 5

Enter the elements 1


2
3
4
5

Enter the location 4

Enter the item to be inserted 8

After insertion
1
2
3
8
4
5

21
//w.a.p. to delete an element from array//
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],j,i,n,d,loc;
clrscr();
printf("Enter the size\n");
scanf("%d",&n);
printf("Enter the %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter element to be deleted\n");
scanf("%d",&d);
for(i=0;i<n;i++)
{
if(a[i]==d)
loc=i;
}
for(j=loc;j<n;j++)
a[j]=a[j+1];
n=n-1;
printf("\nAfter deletion the new list\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
***Output***
Enter the size
5
Enter the 5 elements
1
2
3
4
5
Enter element to be deleted
3

After deletion the new list

1
2
4
5

22
//w.a.p. for linear search//
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,val;
clrscr();
printf("enter no. of elements");
scanf("%d",&n);
printf("enter array a");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("val to be searched");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(val==a[i])
{
break;
}
}
if(a[i]==val)
{
printf("val present at location %d",i+1);
}
else
{
printf("val is not present");
}
getch();
}
***output***
enter no. of elements4
enter array a1
2
3
4
val to be searched3
val present at location 3

23
// w.a.p. for binary search//
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,value,end,start,mid;
clrscr();
printf("enter no of element");
scanf("%d",&n);
printf("enter array a");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("value to be searched");
scanf("%d",&value);
start=0;
end=n-1;
mid=(start+end)/2;
while(start<=end&&value!=a[mid])
{
if(value>a[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
if(start>end)
{
printf("value not found in list");
}

else
{
printf("value found at location %d",mid+1);
}
getch();
}

***output***
enter no of element 3
enter array a 1
2
3
value to be searched 2
value found at location 2

24
//w.a.p to arrange numerics in ascending order by bubble method//

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],t,n,i,p;
clrcsr();
printf(“enter no. of elements\n”);
scanf(“%d”,&n);
printf(“enter array a\n”);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
printf(“%d\t”,a[i]);
}
for(p=1;p<=n-1;p++)
{
for(i=0;i<n-p;i++)
{
If(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
printf(“sorted array is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
***output***
enter no. of elements
7
enter array a
6 7 5 3 4 1 2
sorted array is
1 2 3 4 5 6 7

25
//w.a.p. sum of two matrices//
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,n,m;
printf("enter no of rows=");
scanf("%d",&m);
printf("enter no of column=");
scanf("%d",&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter value of a=");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter value of b=");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d",c[i][j]);
printf(" ");
}
printf("\n");
}
getch();
}
***output***
enter no of rows=2
enter no of column=3
enter value of a=3
enter value of a=2
enter value of a=4
enter value of a=2
enter value of a=4
enter value of a=2
enter value of b=3
enter value of b=23
enter value of b=3
enter value of b=2
enter value of b=3
enter value of b=2

26
6 25 7

4 7 4

//w.a.p. to get transpose of matrix//


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],m,n,i,j;
clrscr();
printf("enter no. of rows and columns\n");
scanf("%d%d",&n,&m);
printf("matrix a is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("matrix a is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
printf("transpose of a is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}
//***output***
enter no. of rows and columns
2
2
matrix a is
1
2
3
4
matrix a is
1 3
2 4
transpose of a is
27
1 2
3 4

// w.ap. sum of diagonal element in a matrix//


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],m,n,i,j;
int sum=0;
clrscr();
printf("\n enter the order of matrix A");
scanf("%d%d",&m,&n);
printf("\n enter the elements of matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum+=a[i][j];
}
}
}
printf("\nsum of the diagonals elements of matrix A:%d",sum);
getch();
}
***output***
enter the order of matrix A 2
2
enter the elements of matrix A1
2
3
4
matrix A
1 2
3 4
sum of the diagonals elements of matrix A:5

28
//w.a.p. matrix multiplication//
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,m,n;
clrscr();

printf("Enter the no. of rows = ");


scanf("%d",&n);
printf("Enter the no. of col. = ");
scanf("%d",&m);
printf("\nEnter the elements of a = ");
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
scanf("%d",&a[i][j]);
printf("\nMatrix of a\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\nEnter the elements of b = ");
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
scanf("%d",&b[i][j]);
printf("\nMatrix of b\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
printf("%3d",b[i][j]);
}
printf("\n");
}
printf("multiplication of a and b matrix = \n");
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
c[i][j]=0;
for(k=1; k<=m; k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}
29
***output***
Enter the no. of rows = 2
Enter the no. of col. = 2

Enter the elements of a = 1


2
3
4

Matrix of a
1 2
3 4

Enter the elements of b = 5


6
7
8

Matrix of b
5 6
7 8
multiplication of a and b matrix =
19 22
43 50

//w.a.p. reverse of a string//


#include<stdio.h>
#include<conio.h>
void main()
{
char str[100],ch,c;
int i,l;
clrscr();
printf("enter a string");
gets(str);
i=0;
while(str[i]!='\0')
{
i++;
}
i--;
for(l=0;l<i;l++,i--)
{
c=str[l];
str[l]=str[i];
str[i]=c;
}
printf("Reverse of string is %s",str);
getch();

30
***output***
enter a string hello
Reverse of string is olleh

// w.a.p. for copy one string to another//


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[20],string2[20];
int i;
clrscr();
printf("\nenter string 1");
gets(string1);
for(i=0;string1[i]!='\0';i++)
{
string2[i]=string1[i];
}
string2[i]='\0';
printf("\n the string2 becomes =%s",string2);
getch();
}
***output***

enter string 1hello

the string2 becomes =hello

31
// w.a.p. to check if a string is pelindrome or not without using
library functions//
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int i,l=0,status=0;
char str[80];
clrscr();
printf("ENTER STRING\n");
for(i=0;(str[i]=getchar())!='\n';i++);
{
str[i]='\0';
l=i;
}
for(i=0;i<l/2;i++)
{
if(str[i]!=str[l-(i+1)])
{
status=1;
break;
}
}
if(status==0)
{
printf("STRING IS PALINDROME");
}
else
{
printf("STRING IS NOT PALINDROME");
}
getch();
}
***output***
ENTER STRING
madam
STRING IS PALINDROME

32
//w.a.p to show string concatenation without using strcat//
#include<conio.h>
#include<stdio.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
clrscr();
printf("enter first string\n");
gets(str1);
printf("enter second string\n");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\n concatenated string is %s",str1);
getch();
}
***output***
enter first string
text
enter second string
book

concatenated string is textbook

33
//w.a.p. to find average marks of student in 3 subjects using
structures//
#include<stdio.h>
#include<conio.h>
struct student
{
int sub1,sub2,sub3;
};
void main()
{
struct student s1;
int i;
float avg=0;
clrscr();
printf("Enter marks in 3 subjects\n");
scanf("%d%d%d",&s1.sub1,&s1.sub2,&s1.sub3);
avg=(s1.sub1+s1.sub2+s1.sub3)/3.0;
printf("Average marks in 3 subjects are %f",avg);
getch();
}
***output***
Enter marks in 3 subjects
1
2
3
Average marks in 3 subjects are 2.000000

// w.a.p. using pointer to a structure//


#include<stdio.h>
#include<conio.h>
struct student
{
int rno,m1,m2,m3;
float per;
};
void main()
{
struct student *s1;
clrscr();
printf("enter a rno");
scanf("%d",&(*s1).rno);
printf("\nenter marks");
scanf("%d%d%d",&(*s1).m1,&(*s1).m2,&(*s1).m3);
(*s1).per=((*s1).m1+(*s1).m2+(*s1).m3)/3.0;
printf("\n per=%f",(*s1).per);
getch();
}
***output***
enter a rno1
enter marks23
12
12

per=15.666667

34
// w.a.p. to show use of array of structure//
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20],dob[10];
};
void main()
{
struct student s[20];
int i,n;
clrscr();
printf("Enter no. of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Student no.=%d\n",i+1);
printf("Enter roll no ,name and dob(dd/mm/yyyy) \n");
scanf("%d%s%s",&s[i].rno,s[i].name,s[i].dob);
printf("Roll no=%d\t Name=%s\t
Dob=%s\n",s[i].rno,s[i].name,s[i].dob);
}
getch();
}
***output***
Enter no. of students
2
Student no.=1
Enter roll no ,name and dob(dd/mm/yyyy)
1
sam
23/04/2017
Roll no=1 Name=sam Dob=23/04/2017
Student no.=2
Enter roll no ,name and dob(dd/mm/yyyy)

2
rakesh
23/09/2018
Roll no=2 Name=rakesh Dob=23/09/2018

35
//w.a.p. to show use of program array within structure//
#include<stdio.h>
#include<conio.h>
struct student
{
int rno,m[5];
char name[20];
float per;
};
void main()
{
struct student s;
int i,total=0;
clrscr();
printf("Enter rno and name\n");
scanf("%d%s",&s.rno,s.name);
printf("Enter marks in 3 subjects\n");
for(i=0;i<3;i++)
{
scanf("%d",&s.m[i]);
total=total+s.m[i];
}
s.per=total/3.0;
printf("Name=%s\t Roll no=%d\t Percentage=%f\t " ,s.name,s.rno,s.per);
getch();
}
***output***
Enter rno and name
2
sam
Enter marks in 3 subjects
1
23
23
Name=sam Roll no=2 Percentage=15.666667

36
// w.a.p. to show use of structure within structure//
#include<stdio.h>
#include<conio.h>
struct date
{
int dd;
int mm;
int yy;
};
struct student
{
int rno;
struct date dob;
};
void main()
{
struct student s;
clrscr();
printf("Enter rno and dob\n");
scanf("%d%d%d%d",&s.rno,&s.dob.dd,&s.dob.mm,&s.dob.yy);
printf("Roll no.=%d\t Dob=%d/%d/%d" ,s.rno,s.dob.dd,s.dob.mm,s.dob.yy);
getch();
}

***output***
Enter rno and dob
1
27
02
2017
Roll no.=1 Dob=27/2/2017

37
// w.a.p. demonstrate union//
#include<stdio.h>
#include<conio.h>
union student
{
int m1,m2,m3;
float per;
};
void main()
{
union student s1;
int total=0;
clrscr();
printf("Enter marks in 3 subjects\n");
scanf("%d",&s1.m1);
total=s1.m1;
scanf("%d",&s1.m2);
total+=s1.m2;
scanf("%d",&s1.m3);
total+=s1.m3;
s1.per=total/3.0;
printf("Percentage=%f",s1.per);
getch();
}
***output***
Enter marks in 3 subjects
12
23
34
Percentage=23.000000

// w.a.p. to show use of malloc() and free()//


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i;
printf("Enter no. of elements\n");
scanf("%d",&n);
a=(int*)malloc(n * sizeof(int));
printf("Enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&*(a+i));
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(a+i));
}
getch();
free(a);
}
38
***output***
Enter no. of elements
2
Enter elements
1
2
Elements of array are
1 2

// w.a.p. to show use of calloc() ,realloc() and free()//


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n1,i,n2;
clrscr();
printf("Enter size of array\n");
scanf("%d",&n1);
a=(int*)calloc(n1,sizeof(int));
printf("addresses of previously allocated memory:\n");
for(i=0;i<n1;i++)
{
printf("%u\t",a+i);
}
printf("\nEnter new size of array\n");
scanf("%d",&n2) ;
for(i=0;i<n2;i++)

{
printf("%u\t",a+i);
}
free(a);
getch();
}
***output***
Enter size of array
3
addresses of previously allocated memory:
1928 1930 1932
Enter new size of array
5
1928 1930 1932 1934 1936

39
//w.a.p for read and write in file//
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("acc.txt","w");
if(fp==NULL)
{
printf("error");
getch();
exit(1);
}
while((ch=fgetchar())!='\n')
{
fputc(ch,fp);
}
fclose(fp);
fp=fopen("acc.txt","r");
while((ch=fgetc(fp))!=EOF)
{
putch(ch);
}
fclose(fp);
getch();
}
***output***
Hello
Hello

//w.a.p for read from file and print on screen//


#include<process.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("acc.txt","r");
if(fp==NULL)
{
printf("error opening file");
getch();
exit(1);
}
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
getch();}
40
***output***
hello

//w.a.p. to copy one content of one file to another//


#include<process.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1;
char ch;
fp=fopen("acc.txt","r");
if(fp==NULL)
{
printf("file not found");
getch();
exit(1);
}
fp1=fopen("abc.txt","w");
while((ch=fgetc(fp))!=EOF)
{
fputc(ch,fp1);
}
fclose(fp);
fclose(fp1);
getch();
}

//w.a.p to insert any integer in file and then put odd and even in
separate file\\
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *fp,*fp1,*fp2;
int i,n;
clrscr();
fp=fopen("abc.txt","wb");
if(fp==NULL)
{
printf("file not found");
getch();
exit(1);
}
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
putw(i,fp);
}
fclose(fp);
41
fp=fopen("abc.txt","rb");
if(fp==NULL)
{
printf("file ont found");
getch();
exit(1);
}
fp1=fopen("even.txt","wb");
if(fp1==NULL)
{
printf("file ont found");
getch();
exit(1);
}
fp2=fopen("odd.txt","wb");
if(fp2==NULL)
{
printf("file ont found");
getch();
exit(1);
}
while((i=getw(fp))!=EOF)
{
if(i%2==0)
{
putw(i,fp1);
}
else
{
putw(i,fp2);
}
}
fclose(fp);
fclose(fp1);
fclose(fp2);
fp=fopen("abc.txt","rb");
fp1=fopen("even.txt","rb");
fp2=fopen("odd.txt","rb");
while((i=getw(fp))!=EOF)
{
printf("%d\n",i);
}
while((i=getw(fp1))!=EOF)
{
printf("%d\n",i);
}
while((i=getw(fp2))!=EOF)
{
printf("%d\n",i);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
getch();}
42
***output***
enter n 10
1
2
3
4
5
6
7
8
9
10
2
4
6
8
10
1
3
5
7
9

43
44

You might also like