You are on page 1of 39

PROGRAM 1:WAP THAT READS IN TWO INTEGERS AND DETERMINES AND PRINTS THE

FIRST IS MULTIPLE OF SECOND.

#include<stdio.h>
#include<conio.h>
void mul(int a,int b)
{ if(a>b)
{if(a%b==0)
printf("\n\nFirst number is a multiple of second");
else
printf("Not a multiple!!!");
}
else if (a<b)
{if(b%a==0)
printf("\n\nSecond number is a multiple of frist");
else
printf("Not a multiple!!!");
}
}
void main()
{int a,b,c;
clrscr();
printf("Enter the first no:");
scanf("%d",&a);
printf("Enter the second no:");
scanf("%d",&b);
mul(a,b);
getch();
}

OUTPUT

Enter the first no:45


Enter the second no:5

First number is a multiple of second


PROGRAM 2:WAP THAT ENTERS THREE DIFFERENT INTEGERS FROM THE KEYBOARD,
AND THEN PRINTS THE SUM,AVG,PRODUCT,MAX AND MIN OF THESE NOS.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{float a,b,c,l,s;
clrscr();
printf("Enter the first no:");
scanf("%f",&a);
printf("Enter the second no:");
scanf("%f",&b);
printf("Enter the third number:");
scanf("%f",&c);
printf("\n\nSum is:%f",a+b+c);
printf("\n\nAverage is:%f",(a+b+c)/3);
printf("\n\nProduct is:%f",a*b*c);
printf("\n\nMaximum is%f",max(a,max(b,c)));
printf("\n\nMinimum is%f",min(a,min(b,c)));

getch();
}

OUTPUT

Enter the first no:34


Enter the second no:12
Enter the third number:87

Sum is:133.000000

Average is:44.333333

Product is:35496.000000

Maximum is87.000000

Minimum is12.000000
PROGRAM 3:WAP THAT READS IN RADIUS OF THE CIRCLE AND PRINTS CIRCLE'S
DIAMETER, CIRCUMFERENCE AND AREA.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{float a,b,c,l,s;
clrscr();
printf("Enter the radius of the circle:");
scanf("%f",&a);
printf("\n\nDiameter is:%f ",2*a);
printf("\n\nCircumference is:%f",2*3.14*a);
printf("\n\nArea is:%f",3.14*a*a);

getch();
}

OUTPUT

Enter the radius of the circle:23

Diameter is:46.000000

Circumference is:144.440000

Area is:1661.060000
PROGRAM 4:WAP THAT ACCEPTS A CHARACTER AND PRINTS ITS DECIMAL, OCTAL
AND HEXADECIMAL EQUIVALENT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{char a,b,c,l,s;
clrscr();
printf("Enter a character:");
scanf("%c",&a);
printf("\n\n Equivalent in decimal is:%d",a);
printf("\n\n Equivalent in octal is:%o",a);
printf("\n\n Equivalent in Hexadecimal is:%X",a);

getch();
}

OUTPUT

Enter a character:P

Equivalent in decimal is:80

Equivalent in octal is:120

Equivalent in Hexadecimal is:50


PROGRAM 5:WAP TO PRINT TABLE OF NUMBERS FROM 1 TO 10 IN 2D FORM.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{int i,j,k;
clrscr();
printf("\t*****TABLE FROM 1 TO 10*****\n\n\n\n\n\n");
for(i=1;i<=10;i++)
{for(j=1;j<=10;j++)
if(i*j<=9)
printf(" %d",i*j);
else
printf(" %d",i*j);

printf("\n");
}

getch();
}

OUTPUT

*****TABLE FROM 1 TO 10*****

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
PROGRAM 6:WAP THAT READS AN INTEGER AND DETERMINES WHETHER OR NOT IT
IS A PALINDROME.

#include<conio.h>
#include<stdio.h>
void main()

{int num,i,a=0,n=0,h;
clrscr();
printf("Enter the integer:");
scanf("%d",&num);
h=num;
do
{
a=num%10;
n=n*10+a;
num=num/10;
}while(num!=0);
if(h-n==0)
{printf("\n\n%d IS A PALINDROME",h);
}
else
{printf("\n\n%d IS NOT A PALINDROME",h);
}
getch();
}

OUTPUT

Enter the integer:12321

12321 IS A PALINDROME
PROGRAM 7:WAP THAT ACCEPTS A BINARY NUMBER AND CONVERT IT INTO
DECIMAL, OCTAL AND HEXADECIMAL EQUIVALENT.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
char c=65,m;
int a[20],h,i,j,k,s,v;
clrscr();

printf("Enter the size of the array:");


scanf("%d",&s);
printf("Enter the binary number:");
for(i=0;i<20;i++)
a[i]=0;
for(i=s-1;i>=0;i--)
{scanf("%d",&a[i]);
}

printf("\n\n The equivalent in hexa is: ");


for(j=s+4-s%4;j>=0;j=j-4)
{
{for(i=0;i<4;i++)
h+=a[j+i]*pow(2,i);
}
if(h>=10)
{
m=(h-10)+65;
printf("%c",m);

}
else
printf("%d",h);
h=0;
}
h=0;
printf("\n\n The equivalent in Decimal is:");
for(j=0;j<s;j++)
{h+=a[j]*pow(2,j);
}
printf("%d",h);
h=0;
printf("\n\n The equivalent in Octal is:");
for(j=s+3-s%3;j>=0;j=j-3)
{
for(i=0;i<3;i++)
h+=a[j+i]*pow(2,i);

printf("%d",h);
PROGRAM 8:WAP THAT READS AN INTEGER M AND A SINGLE DIGIT N AND
DETERMINES HOW MANY DIGITS IN M ARE N.

#include<conio.h>
#include<stdio.h>
void main()

{
int num,i,a=0,n=0,h;
clrscr();
printf("Enter the integer:");
scanf("%d",&num);
printf("Enter the digit:");
scanf("%d",&h);
i=num;
do
{
a=num%10;
if(a==h)
n++;
num=num/10;
}while(num!=0);
printf("THERE ARE %d \"%d\" IN %d ",n,h,i);

getch();
}

OUTPUT

Enter the integer:12131


Enter the digit:1
THERE ARE 3 "1" IN 12131
PROGRAM 9:WAP THAT READS 3 POSITIVE INTEGERS AND DETERMINES IF THEY
COULD REPRESENT THE SIDES OF A TRIANGLE.

#include<conio.h>
#include<stdio.h>
void main()

{
int a,b,c;
clrscr();
printf("Enter the fisrt side:");
scanf("%d",&a);
printf("Enter the second side:");
scanf("%d",&b);
printf("Enter the third side:");
scanf("%d",&c);

if(a+b>c&&a+c>b&&b+c>a)
printf("%d, %d, %d CAN REPRESENT SIDES OF A TRIANGLE",a,b,c);
else
printf("%d, %d, %d CAN NOT REPRESENT SIDES OF A TRIANGLE",a,b,c);
getch();
}

OUTPUT

Enter the fisrt side:3


Enter the second side:4
Enter the third side:5
3, 4, 5 CAN REPRESENT SIDES OF A TRIANGLE
PROGRAM 10:WAP TO ENCRYPT AND DECRYPT DATA IN THE GIVEN WAY.

#include<conio.h>
#include<stdio.h>
void main()

{
int a[4],i,temp1,temp2;
clrscr();
printf("ENTER THE NUMBER:");
for( i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
a[i]=(a[i]+7)%10;
temp1=a[0];
temp2=a[1];
a[0]=a[2];
a[1]=a[3];
a[2]=temp1;
a[3]=temp2;
printf("THE ENCRYPTED NUMBER IS:");
for(i=0;i<4;i++)
printf("%d",a[i]);
printf("\n\nTHE NUMBER BACK IN ORIGINAL FORM IS:");
temp1=a[0];
temp2=a[1];
a[0]=a[2];
a[1]=a[3];
a[2]=temp1;
a[3]=temp2;
for(i=0;i<4;i++)
{if(a[i]>7)
a[i]=a[i]-7;
else
a[i]+=3;
printf("%d",a[i]);
}

getch();
}

OUTPUT

ENTER THE NUMBER:1


3
2
7
THE ENCRYPTED NUMBER IS:9480
QUESTION 11:WAP TO FIND ALL PRIME DIVISORS OF NUMBER.

#include<conio.h>
#include<stdio.h>
int prime(int a)
{int i;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
break;
}

if(i>a/2)
return 1;
else
return 0;
}
void main()
{int num,i;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
for(i=2;i<=num;i++)
{if(num%i==0&& prime(i))
printf(" %d",i);
}
getch();
}

OUTPUT

Enter the number:645


3 5 43
PROGRAM 12:WAP TO COMPUT e^x BY 1+x+x^2+x^3+....

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int a)
{if(a==0)
return 1;
else
return a*fact(a-1);
}
void main()
{int a,b,i;
float res=0.0;
clrscr();
printf("Enter the x of e^x:");
scanf("%d",&a);
printf("Enter the number of terms:");
scanf("%d",&b);
for(i=0;i<b;i++)
res+=pow(a,i)/fact(i);
printf("The value of e^%d is:%f",a,res);

getch();
}

OUTPUT

Enter the x of e^x:2


Enter the number of terms:13
The value of e^2 is:3.776980
PROGRAM 13:WAP TO COMPUTE FIRST N TERMS OF A FIBONACCI SERIES.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fibo(int a)
{if(a==1)
return 0;
else if(a==2)
return 1;
else
return fibo(a-1)+fibo(a-2);
}
void main()
{int a,b,i;

clrscr();
printf("Enter the number of terms:");
scanf("%d",&a);
printf("THE FIBONNACI SERIES IS:");
for(i=1;i<=a;i++)
printf(" %d",fibo(i));

getch();
}

OUTPUT

Enter the number of terms:14


THE FIBONNACI SERIES IS: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
PROGRAM 14:WAP TO DETERMINE WHETEHER A NUMBER IS ARMSTRONG NO.

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{int a,t,n,re=0;

clrscr();
printf("Enter the number: ");
scanf("%d",&a);
t=a;
do
{
n=a%10;
re=re+pow(n,3);
a=a/10;
}while(a!=0);
if(t==re)
printf("%d IS AN ARMSTRONG NUMBER",t);
else

printf("%d IS NOT AN ARMSTRONG NUMBER",t);


getch();
}

OUTPUT

Enter the number: 153


153 IS AN ARMSTRONG NUMBER
PROGRAM 15:WAP TO PRINT ALL PRIME NOS BETWEEN 1 AND 1000.

#include<conio.h>
#include<stdio.h>
int prime(int a)
{int i;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
break;
}

if(i>a/2)
return 1;
else
return 0;
}
void main()
{int num,i;
clrscr();
for(i=1;i<=1000;i++)
{if(prime(i))
printf("%d\t",i);
}
getch();
}

OUTPUT

1 2 3 5 7 11 13 17 19
23
29 31 37 41 43 47 53 59 61
67
71 73 79 83 89 97 101 103 107
109
113 127 131 137 139 149 151 157 163
167
173 179 181 191 193 197 199 211 223
227
229 233 239 241 251 257 263 269 271
277
281 283 293 307 311 313 317 331 337
347
349 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 503 509 521
523
541 547 557 563 569 571 577 587 593
599
PROGRAM 16:WAP TO DETERMINE IF A NUMBER IS PERFECT NO.

#include<conio.h>
#include<stdio.h>
void per(int a)
{int i,s=0;
for(i=1;i<a;i++)
{if(a%i==0)
s+=i;
}
if(a==s)
printf("%d IS A PERFECT NUMBER",a);
else
printf("%d IS NOT A PERFECT NUMBER",a);

}
void main()

{
int a;
clrscr();
printf("ENTER THE NUMBER:");
scanf("%d",&a);
per(a);
getch();
}

OUTPUT

ENTER THE NUMBER:6


6 IS A PERFECT NUMBER
PROGRAM 17:WAF THAT REVERSES A NUMBER.

#include<conio.h>
#include<stdio.h>
int reverse(int num)
{ int a=0,h,n=0;
h=num;
do
{
a=num%10;
n=n*10+a;
num=num/10;
}while(num!=0);
return n;
}

void main()

{
int num,i,a=0,n=0,h;
clrscr();
printf("Enter the integer:");
scanf("%d",&num);
n=reverse(num);
printf("\n\nTHE REVERSE OF %d is: %d ",num,n);

getch();
}

OUTPUT

Enter the integer:432

THE REVERSE OF 432 is: 234


PROGRAM 18:WRITE A RECURSIVE FUNCTION TO FIND LENGTH OF A STRING.

#include<conio.h>
#include<stdio.h>
int len(char a[],int i)
{if(a[i]=='\0')
return 0;
else
return len(a,i+1)+1;
}
void main()
{char a[20],c;

int l;
clrscr();
gets(a);
l=len(a,0);
printf("THE LENGTH OF THE STRING IS:%d",l);
getch();
}

OUTPUT

PRIYANKA GOYAL
THE LENGTH OF THE STRING IS:14
PROGRAM 19:WRITE A RECURSIVE FUNCTION TO FIND Nth TERM OF THE
FIBONACCI SERIES.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fibo(int a)
{if(a==1)
return 0;
else if(a==2)
return 1;
else
return fibo(a-1)+fibo(a-2);
}
void main()
{int a,b,i;

clrscr();
printf("Enter the term:");
scanf("%d",&a);
printf("THE %d TERM OF THE FIBONNACI SERIES IS %d",a,fibo(a));

getch();
}

OUTPUT

Enter the term:12


THE 12 TERM OF THE FIBONNACI SERIES IS 89
PROGRAM 20:WRITE A RECURSIVE FUNCTION TO COMPUTE AVERAGE OF N NO

#include<stdio.h>
#include<conio.h>
#include<math.h>
float avg(float n1,float n2,float a[])
{if (n1==n2 )
return a[n1];
else
{
return (a[n1]+(n2-n1)*avg(n1+1,n2,a))/(n2-n1+1); }
}
void main()
{float a[20];
int b,i;

clrscr();
printf("ENTER THE NUMBER OF TERMS:");
scanf("%d",&b);
for(i=1 ;i<=b;i++)
scanf("%f",&a[i]);
printf("THE AVERAGE OF THE NUMBERS IS:%f",avg(1,b,a));

getch();
}

OUTPUT

ENTER THE NUMBER OF TERMS:4


1
2
3
4
THE AVERAGE OF THE NUMBERS IS:2.500000
PROGRAM 21:WRITE A RECURSIVE FUNCTION TO FIND GCD OF A NUMBER.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int gcd(int m,int n)
{if (n==m )
return n;
else if(m>n)
return gcd(m-n,n);
else if(m<n);
return gcd(n,m);

}
void main()
{int n,m;

clrscr();
printf("ENTER THE FIRST NUMBER:");
scanf("%d",&m);
printf("ENTER THE SECOND NUMBER:");
scanf("%d",&n);

printf("THE GCD OF THE NUMBERS IS:%d",gcd(m,n));

getch();
}

OUTPUT

ENTER THE FIRST NUMBER:7


ENTER THE SECOND NUMBER:35
THE GCD OF THE NUMBERS IS:7
PROGRAM 22:WAF TO CALCULATE DISTANCE BETWEEN 2 POINTS.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float dist(float x1,float y1,float x2,float y2)
{return(sqrt(pow(x2-x1,2)+pow(y2-y1,2)));
}
void main()
{ float x1,y1,x2,y2;
clrscr();
printf("ENTER THE FIRST POINT:");
scanf("%f%f",&x1,&y1);
printf("ENTER THE SECOND POINT:");
scanf("%f%f",&x2,&y2);

printf("THE DISTANCE BETWEEN THESE POINTS IS:%f",dist(x1,y1,x2,y2));

getch();
}

OUTPUT

ENTER THE FIRST POINT:


1
2
ENTER THE SECOND POINT:
3
4
THE DISTANCE BETWEEN THESE POINTS IS:2.828427
PROGRAM 23:WAP TO SEARCH AN ELEMENT USING BINARY SEARCH.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{int a[20],m,n,i,j,f,l,s;
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&m);
f=0;l=m-1;
printf("ENTER THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("ENTER THE ELEMENT TO BE SEARCHED:");
scanf("%d",&n);
while(l>=f)
{
s=(f+l)/2;
if(a[s]==n)
{printf("NUMBER FOUND AT:%d",(s+1));
break;
}
else
if(a[s]>n)
{l=s-1;
}
else if(a[s]<n)
f=s+1;
}

getch();
}

OUTPUT

ENTER THE SIZE OF THE ARRAY:5


ENTER THE ARRAY:1
3
5
7
9
ENTER THE ELEMENT TO BE SEARCHED:5
NUMBER FOUND AT:3
PROGRAM 24:WAP TO FIND FREQUENCY OF EACH NUMBER IN AN ARRAY.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{int a[20],m,c[20],lar=0,i;
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&m);
printf("ENTER THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
{if(a[i]>lar)
lar=a[i];
}
for(i=0;i<=lar;i++)
c[i]=0;
for(i=0;i<m;i++)
c[a[i]]++;
for(i=0;i<=lar;i++)
if(c[i]!=0)
printf("%d COMES %d TIMES IN THE ARRAY\n",i,c[i]);

getch();
}

OUTPUT

ENTER THE SIZE OF THE ARRAY:5


ENTER THE ARRAY:1
2
1
2
1
1 COMES 3 TIMES IN THE ARRAY
2 COMES 2 TIMES IN THE ARRAY
PROGRAM 25:WAP TO CONCATENATE TWO STRINGS.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[20],b[20],c[20],*a1,*b1,*c1;
int i=0,j=0;
clrscr();
puts("Enter the first string:");
gets(a);
puts("Enter the second string:");
gets(b);
for(a1=a;*a1!='\0';a1++)
{c[i]=*a1;
i++;
}

for(b1=b;*b1!='\0';b1++)
{c[i]=*b1;
i++;
}
printf("\n\nTHE RESULT AFTER CONCATENATION IS:");
for(c1=c;*c1!='\0';c1++)
{printf("%c",*c1);
}

getch();
}

OUTPUT

Enter the first string:


PRIYANKA
Enter the second string:
GOYAL

THE RESULT AFTER CONCATENATION IS:PRIYANKA GOYAL


PROGRAM 26:WAP TO COMPARE TWO STRINGS.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[20],b[20],c[20],*a1,*b1,*c1;
int i=0,j;
clrscr();
puts("Enter the first string:");
gets(a);
puts("Enter the second string:");
gets(b);
for(a1=a,b1=b;*a1!='\0',*b1!='\0';a1++,b1++)
{if(*a1>*b1)
{
printf("%s IS GREATER",a);
goto s;
}
else if(*a1<*b1)
{
printf("%s IS GREATER",b);
goto s;
}

printf("BOTH ARE EQUAL");


s:
getch();
}

OUTPUT

Enter the first string:


PRIYANKA
Enter the second string:
GOYAL
PRIYANKA IS GREATER
PROGRAM 27:WAP TO FIND LENGTH OF A STRING USING POINTERS.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[20],b[20],c[20],*a1,*b1,*c1;
int i=0,j;
clrscr();
puts("Enter the string:");
gets(a);
for(a1=a;*a1!='\0';a1++)
{i++;
}
printf("THE LENGTH OF THE STRING IS:%d",i);
getch();
}

OUTPUT

Enter the string:


PRIYANKA GOYAL
THE LENGTH OF THE STRING IS:14
PROGRAM 28:WAP TO FIND FREQUENCY OF EACH CHARACTER IN A STRING.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[20],*a1;
int i=0,j=0,b[26];
clrscr();
for(i=0;i<26;i++)
b[i]=0;
puts("Enter the string:");
gets(a);
for(a1=a;*a1!='\0';a1++)
{j=(int)(*a1);
b[j-97]++;
}
for(i=0;i<26;i++)
{if(b[i]>0)
printf("%c =%d\n",i+65,b[i]);
}
getch();
}

OUTPUT

Enter the string:


priyanka
A =2
I =1
K =1
N =1
P =1
R =1
Y =1
PROGRAM 29:WAP TO COUNT NUMBER OF WHITESPACES IN A STRING.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[100],*a1;
int i=0,j=0,b[26];
clrscr();
for(i=0;i<26;i++)
b[i]=0;
puts("Enter the string:");
gets(a);
for(a1=a;*a1!='\0';a1++)
{if(*a1==' ')
j++;
}
printf("THE NUMBER OF WHITE SPACES ARE:%d",j);

getch();
}

OUTPUT

Enter the string:


HARRY POTTER AND THE DEATHLY HALLOWS
THE NUMBER OF WHITE SPACES ARE:5
PROGRAM 30:WAP TO IMPLEMENT ALL ARITHMETIC OPERATIONS ON RATIONAL
NUMBERS.

#include<conio.h>
#include<stdio.h>
struct rational
{int num;
int deno;
};
rational add(rational &o1,rational &o2)
{ struct rational o3;
int a,b;
o3.deno=o1.deno*o2.deno;
a=o2.num*o1.deno;
b=o1.num*o2.deno;
o3.num=a+b;
return o3;
}
rational mul(rational &o1,rational &o2)
{struct rational o3;

o3.deno=o1.deno*o2.deno;
o3.num=o1.num*o2.num;
return o3;
}
rational sub(rational &o1,rational &o2)
{struct rational o3;
int a,b;
o3.deno=o1.deno*o2.deno;
a=o2.num*o1.deno;
b=o1.num*o2.deno;
o3.num=b-a;
return o3;
}
rational div(rational &o1,rational &o2)
{struct rational o3;
o3.deno=o1.deno*o2.num;
o3.num=o1.num*o2.deno;
return o3;
}
void main()
{struct rational o1,o2,o3;
int l;
clrscr();
printf("Enter the numerator of first number:");
scanf("%d",&o1.num);
printf("Enter the denominator of first number:");
scanf("%d",&o1.deno);
printf("Enter the numerator of second number:");
scanf("%d",&o2.num);
printf("Enter the denominator of second number:");
PROGRAM 31:WAP TO IMPLEMENT ALL ARITHMETIC OPERATIONS ON COMPLEX
NUMBERS.

#include<conio.h>
#include<math.h>
#include<stdio.h>
struct complex
{float real;
float img;
};
complex add(complex c1,complex c2)
{struct complex c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}
complex sub(complex c1,complex c2)
{struct complex c3;
c3.real=c1.real-c2.real;
c3.img=c1.img-c2.img;
return c3;
}

complex mul(complex c1,complex c2)


{struct complex c3;
c3.real=c1.real*c2.real-c1.img*c2.img;
c3.img=c1.real*c2.img+c1.img*c2.real;
return c3;
}
complex div(complex &c1,complex &c2)
{
struct complex c3,c4;
c2.img=0-c2.img;
c3=mul(c1,c2);
c4.real=c3.real/(pow(c2.real,2)+pow(c2.img,2));
c4.img=c3.real/(pow(c2.real,2)+pow(c2.img,2));

return c4;
}
void main()
{struct complex c1,c2,c3;
clrscr();
printf("Enter the real and imagionary parts of first complex
number:");
scanf("%f%f",&c1.real,&c1.img);
printf("Enter the real and imagionary parts of second complex
number:");
scanf("%f%f",&c2.real,&c2.img);
c3=add(c1,c2);
printf("\nSUM IS:%f+i(%f))",c3.real,c3.img);
c3=sub(c1,c2);
printf("\nDIFFERENCE IS:%f+i(%f))",c3.real,c3.img);
PROGRAM 32:WAP TO SEARCH AN ELEMENT USING LINEAR SEARCH.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{int a[20],m,n,i,j;
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&m);
printf("ENTER THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("ENTER THE ELEMENT TO BE SEARCHED:");
scanf("%d",&n);
for(i=0;i<m;i++)
{if(a[i]==n)
{printf("ELEMENT FOUND AT:%d",(i+1)) ;
goto p;
}
}
printf("ELEMENT NOT FOUND");

p:
getch();
}

OUTPUT

ENTER THE SIZE OF THE ARRAY:5


ENTER THE ARRAY:1
2
4
5
6
ENTER THE ELEMENT TO BE SEARCHED:5
ELEMENT FOUND AT:4
PROGRAM 33:WAP THAT READS DATE IN dd/mm/yyyy FORMAT AND PRINTS IN dd
MONTH yyyy FORMAT.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{char a[20],*a1;
int i=0,j=0,k=0;
clrscr();
printf("ENTER THE DATE IN DD/MM/YYYY FROMAT:");
gets(a);
for(a1=a;*a1!='/';*a1++)
i=i*10+(((int)*a1)-48);
a1++;
for(;*a1!='/';*a1++)
j=j*10+(((int)*a1)-48);
a1++;
for(;*a1!='\0';*a1++)
k=k*10+(((int)*a1)-48);
printf("\n\n%d ",i);
switch(j)
{case 1:printf("JANUARY");
break;
case 2:printf("FEBURARY");
break;
case 3:printf("MARCH");
break;
case 4:printf("APRIL");
break;
case 5:printf("MAY");
break;
case 6:printf("JUNE");
break;
case 7:printf("JULY");
break;
case 8:printf("AUGUST");
break;
case 9:printf("SEPTEMBER");
break;
case 10:printf("OCTOBER");
break;
case 11:printf("NOVEMBER");
break;
case 12:printf("DECEMBER");
break;
}
printf(" %d",k);
getch();
}
PROGRAM 34:WAF THAT RETURNS SMALLEST OF THREE NUMBERS.

#include<stdio.h>
#include<conio.h>
int min(int a,int b)
{if(a>b)
return b;
else
return a;
}

void main()
{int a,b,c,minimum;
clrscr();
printf("ENTER THE FIRST NUMBER:");
scanf("%d",&a);
printf("ENTER THE SECOND NUMBER:");
scanf("%d",&b);
printf("ENTER THE THIRD NUMBER:");
scanf("%d",&c);
minimum=min(a,min(b,c));
printf("\n\nTHE MINIMUM OF THESE NUMBERS IS:%d",minimum);
getch();
}

OUTPUT

ENTER THE FIRST NUMBER:4


ENTER THE SECOND NUMBER:5
ENTER THE THIRD NUMBER:6

THE MINIMUM OF THESE NUMBERS IS:4


PROGRAM 35:WRITE A RECURSIVE FUNCTION TO COMPUTE nCr.

#include<stdio.h>
#include<conio.h>
int fact(int n)
{if(n==0)
return 1;
else
return fact(n-1)*n;
}
int comb(int n,int k)
{if(n==k)
return 1;
else if(k==1)
return n;
else return comb(n-1,k-1)+comb(n-1,k);
}
void main()
{int n,k,com;
clrscr();
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("ENTER THE VALUE OF K:");
scanf("%d",&k);
com=comb(n,k);
printf("\n\nTHE VALUE OF nCk IS:%d",com);
getch();
}

OUTPUT

ENTER THE VALUE OF N:7


ENTER THE VALUE OF K:5

THE VALUE OF nCk IS:21


PROGRAM 36:WRITE A RECURSIVE FUNCTION TO IMPLEMENT TOWER OF HANOI.

#include<stdio.h>
#include<conio.h>
int hanoi(int n)
{if(n==1)
return 1;
else return 2*hanoi(n-1)+1;
}
void main()
{int n,com;
clrscr();
printf("ENTER THE NUMBER OF DISKS:");
scanf("%d",&n);
com=hanoi(n);
printf("THE NUMBER OF MOVES REQUIRED ARE:%d",com);
getch();
}

OUTPUT

ENTER THE NUMBER OF DISKS:10


THE NUMBER OF MOVES REQUIRED ARE:1023
PROGRAM 37:WAP TO SORT NUMBERS USING SELECTION SORT.

#include<stdio.h>
#include<conio.h>
void selsort(int *A,int size)
{int i,j,small,pos,tmp;
for( i=0;i<size;i++)
{small=*(A+i);
pos=i;
for(j=i+1;j<size;j++)
{if(*(A+j)<small)
{small=*(A+j);
pos=j;
}
}
tmp=*(A+i);
*(A+i)=*(A+pos);
*(A+pos)=tmp;
printf("\nArray after pass- %d \n",i+1);
for(j=0;j<size;j++)
printf(" %d",*(A+j));
}
}
void main()
{int AR[50],N,i;
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d",&AR[i]);
selsort(AR,N);
printf("\n\nTHE SORTED ARRAY IS:\n");
for(i=0;i<N;i++)
printf(" %d",AR[i]);
getch();
}

OUTPUT

ENTER THE SIZE OF THE ARRAY:7


6
8
2
5
1
3
4

Array after pass- 1


1 8 2 5 6 3 4
PROGRAM 38:WAP TO SORT NUMBERS USING BUBBLE SORT.

#include<stdio.h>
#include<conio.h>
void bubble(int ar[],int size)
{int tmp,ctr=0,i,k,j;
for(i=0;i<size;i++)
{for(j=0;j<size-1-i;j++)
{if(ar[j]>ar[j+1])
{tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp;
}
printf("\nArray after iteration -%d\n",++ctr);
for(k=0;k<size;k++)
printf(" %d",ar[k]);
}
}
}

void main()
{int AR[50],N,i;
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d",&AR[i]);
bubble(AR,N);
printf("\n\nTHE SORTED ARRAY IS:\n");
for(i=0;i<N;i++)
printf(" %d",AR[i]);
getch();
}

OUTPUT

ENTER THE SIZE OF THE ARRAY:6


1
5
2
3
6
7

Array after iteration -1


1 5 2 3 6 7
Array after iteration -2
1 2 5 3 6 7
Array after iteration -3
1 2 3 5 6 7
PROGRAM 39:WAP TO ADD, SUBTRACT, MULTIPLY TWO MATRICES USING 1D ARRAY.

# include<stdio.h>
#include<conio.h>
void main()
{int a[100],b[100],c2[100],i,j,k,r,c,r1,c1,h;
clrscr();
printf("ENTER THE NUMBER OF ROWS AND COLUMNS FOR FIRST MATRIX:");
scanf("%d%d",&r,&c);
printf("ENTER THE NUMBER OF ROWS AND COLUMNS FOR SECOND MATRIX:");
scanf("%d%d",&r1,&c1);
printf("ENTER FIRST MATRIX ROW WISE:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[c*i+j]);

printf("ENTER SECOND MATRIX ROW WISE:");


for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&b[c1*i+j]);
if(r1==r&&c1==c)
{printf("SUM :\n");
for(i=0;i<r;i++)
{for(j=0;j<c;j++)
printf(" %d",a[c*i+j]+b[c*i+j]);
printf("\n");
}

printf("DIFFERENCE :\n");
for(i=0;i<r;i++)
{for(j=0;j<c;j++)
printf(" %d",a[c*i+j]-b[c*i+j]);
printf("\n");
}

if(r1==c||r==c1)
{h=c1;
printf("PRODUCT :\n");
for(i=0;i<r;i++)
for(j=0;j<c1;j++)
{c2[h*i+j]=0;
for(k=0;k<c;k++)
c2[h*i+j]+=a[c*i+k]*b[h*k+j];

}
for(i=0;i<r;i++)
{printf("\n");

You might also like