0% found this document useful (0 votes)
273 views

C Programs

The document contains C programs demonstrating basic programming concepts like input/output, data types, operators, conditional statements, functions etc. It includes 21 programs with explanations and sample outputs. The programs cover concepts like data type demonstration, arithmetic operations, relational and logical operators, if-else conditional statements, functions to find maximum, check even-odd, leap year etc.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views

C Programs

The document contains C programs demonstrating basic programming concepts like input/output, data types, operators, conditional statements, functions etc. It includes 21 programs with explanations and sample outputs. The programs cover concepts like data type demonstration, arithmetic operations, relational and logical operators, if-else conditional statements, functions to find maximum, check even-odd, leap year etc.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 133

I).

SIMPLE C PROGRAMS
1. Accepting text from user and printing
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(" C test program");
getch();
}

RESULT:
C test program

2.Program to demonstrate the working of fundamental datatypes


#include<stdio.h>
#include<conio.h>
void main()
{
float x;
int y;
char a;
double k;
clrscr();
x=123.456;
y=20;
a='z';
k=9.3456677;
printf("\n x=%f",x);
printf("\n y=%d",y);
printf("\n a=%c",a);
printf("\n k=%lf",k);
getch();
}

RESULT:

x=123.456001
y=20
a=z
k=9.345668

3. Program to interchange to values


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter the values of a,b \n");
scanf("%d%d",&a,&b);

temp=a;
a=b;
b=temp;

printf("a=%d\n b=%d\n ",a,b);


getch();
}

RESULT:

enter the values of a,b


5
3
a=3
b=5

4.Program to calculate simple interest


#include<stdio.h>
#include<conio.h>
void main()
{
float p,n,r;
float simple=0.0;
clrscr();
printf(" enter the values of p n r\n");
scanf("%f%f%f",&p,&n,&r);
simple=(p*n*r)/100.0;
printf("simple interest is :%f\n",simple);
getch();
}

RESULT:

enter the values of p n r


1000
2
20
simple interest is :400.000000

5.Program to calculate area and perimeter of a circle

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

void main()
{
float r,area,peri;
clrscr();
printf("\n enter any radious");
scanf("%f",&r);
area=3.14*r*r;
peri=2*3.14*r;
printf("\n area is %f",area);
printf("\n perimeter is %f",peri);
getch();
}

RESULT:
enter any radious
5

area is 78.500000
perimeter is 31.400000

6.Program to calculate the area and perimeter of triangle

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

void main()
{
float a,b,c,s,area,peri;
clrscr();
printf("\n enter the lengths of 3 sides");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
peri=a+b+c;
printf("\n area is %f",area);
printf("\n perimeter is %f",peri);
getch();
}

RESULT:
enter the lengths of 3 sides 4 5 6
area is 9.921567
perimeter is 15.000000

7.Program to find area and perimeter of a rectangle


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

void main()
{
float l,b,area,peri;
clrscr();
printf("enter the length and breadth
of rectangle\n");
scanf("%f%f",&l,&b);
area=l*b;
peri=2*(l+b);
printf("area is %f\n",area);
printf("perimeter is %f\n",peri);
getch();

RESULT:

enter the length and breadth of rectangle


23.4
43.4
area is 1015.559998
perimeter is 133.600006

II. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS

8.Program to demonstrate the working of arithmetical operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ans;
clrscr();
printf("enter any two integers");
scanf("%d%d",&a,&b);
ans=a+b;
printf("\n sum is %d",ans);
ans=a-b;
printf("\n difference is %d",ans);
ans=a*b;
printf("\n product is %d",ans);
ans=a/b;
printf("\n quotient is %d",ans);
ans=a%b;
printf("\n remainder is %d",ans);
getch();
}

RESULT:

enter any two integers10 20


sum is 30
difference is -10
product is 200
quotient is 0
remainder is 10

9.Program to demonstrate the working of increment and decrement


operators

#include<stdio.h>
#include<conio.h>
void main()
{
int a,ans;
clrscr();
a=2;
printf("\n initial value of a is %d",a);
getch();
a++;
printf("\n after a++ a is %d",a);
getch();
++a;
printf("\n after ++a a is %d",a);
getch();
ans=a++;
printf("\n with ans=a++ a is %d and
ans is %d",a,ans);
ans=++a;
printf("\n with ans=++a a is %d and
ans is %d",a,ans);
getch();
}

RESULT:

initial value of a is 2
after a++ a is 3
after ++a a is 4
with ans=a++ a is 5 and ans is 4
with ans=++a a is 6 and ans is 6

10.Program to demonstrate the working of increment and decrement operators

#include<stdio.h>
#include<conio.h>
void main()
{
int a,ans;
clrscr();
a=2;
printf("\n now a is %d",a);
ans=a++ * a++ * ++a;
printf("\n with ans=a++ * a++ * ++a
ans is %d and a is %d",ans,a);
getch();
}

RESULT:

now a is 2
with ans=a++ * a++ * ++a ans is 27 and a is 5

11.Program to find the max of two numbers by using conditional


operators

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("enter any three integers");
scanf("%d%d%d",&a,&b,&c);
max=a>b?a:b;
max=max>c?max:c;
printf("max is %d",max);
getch();
}

RESULT:

enter any three integers19 35 68


max is 68

12.Program to find the max of three integers by using conditional


operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("enter any three integers");
scanf("%d%d%d",&a,&b,&c);
max=a>b?(a>c?a:c):(b>c)?b:c;
printf("max is %d",max);
getch();
}

RESULT:

enter any three integers 24 76 48


max is 76

13.Program to find the max of three integers by using conditional


operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
printf("enter any two integers");
scanf("%d%d",&a,&b);
max=(a>b)?a:b;
printf("max no is %d",max);
getch();
}
RESULT:

enter any two integers 34 76


max no is 76

14.Program to demonstrate the working of relational operators

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter any two integers");
scanf("%d%d",&a,&b);
printf("\n %d > %d is %d",a,b,a>b);
printf("\n %d >= %d is %d",a,b,a>=b);
printf("\n %d < %d is %d",a,b,a<b);
printf("\n %d <= %d id %d",a,b,a<=b);
printf("\n %d == %d is %d",a,b,a==b);
printf("\n %d != %d is %d",a,b,a!=b);
getch();
}

RESULT:

enter any two integers24 35

24 > 35 is 0
24 >= 35 is 0
24 < 35 is 1
24 <= 35 is 1
24 == 35 is 0
24 != 35 is 1

15.Program to demonstrate the working of shortcut assignment


operators

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter any integer");
scanf("%d",&a);
printf("\n initially a= %d",a);
a+=10;
printf("\n with a+=10 a= %d\t ",a);
a-=20;
printf("\n with a-=20 a= %d",a);
a*=5;
printf("\n with a*=5 a=%d",a);
a/=10;
printf("\n with a/=10 a= %d",a);
getch();
}

RESULT:

enter any integer2

initially a= 2
with a+=10 a= 12
with a-=20 a= -8
with a*=5 a=-40
with a/=10 a= -4

16.Program to demstrate the working of bitwise operataors

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int a,b,ans;
clrscr();
printf("enter any two integers");
scanf("%u%u",&a,&b);
ans=a&b;
printf("\n %u & %u is %u",a,b,ans);
ans=a|b;
printf("\n %u | %u is %u",a,b,ans);
ans=a^b;
printf("\n %u ^ %u is %u",a,b,ans);
ans=~a;
printf("\n ones complement of %u is %u",a,ans);
ans=a>>3;
printf("\n shift right %u with 3 count is %u",a,ans);
ans=a<<3;
printf("\n shift left %u with 3 count is %u",a,ans);
getch();
}

RESULT:
enter any two integers10 20
10 & 20 is 0
10 | 20 is 30
10 ^ 20 is 30
ones complement of 10 is 65525
shift right 10 with 3 count is 1
shift left 10 with 3 count is 80

17.Program to find whether the numbers is even or odd

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter a no\n");
scanf("%d",&a);
printf("the no can not be even or odd\n");
if(a%2==0)
printf("the no %d is even",a);
else
printf("the no %d is odd",a);
getch();
}

RESULT:

enter a no
23
the no 23 is odd

enter a no
34
the no 34 is even
3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

18.Program to find the greatest of teo numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter two distinct nos\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("the no %d is greater",a);
else
printf("the no %d is greater",b);
getch();
}

RESULT:

enter two nos


24
the no 4 is greater

19.Program to check a year is leap year or not

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter the year\n");
scanf("%d",&year);
if(((year%4)==0)&&((year%100)!=0)||((year%400)==0))
printf("the year %d is leap year",year);
else
printf("the year %d is not a leap year",year);
getch();
}
RESULT:

enter the year


1884
the year 1884 is leap year

20.Program to the character ts alphabt or digit or a special


character

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
char ch;
clrscr();
printf("\n enter any character");
scanf("%c",&ch);
if((ch >= 'a' && ch <= 'z')||(ch>= 'A' && ch<= 'Z'))
printf("\n alphabet");
else
if(ch >= '0' && ch <= '9')
printf("\n digit");
else
printf("\n special character");
getch();
}

RESULT:

enter any charactera

alphabet
enter any character4

digit
enter any character*

special character

21.Program to check the character is a alphabet or digit or speciak


character using a system defined functions

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\n enter any character");
scanf("%c",&ch);
if( isalpha(ch) )
printf("\n alphabet");
else
if( isdigit(ch) )
printf("\n digit");
else
printf("\n special character");
getch();
}

RESULT:

enter any charactera

alphabet
enter any character4

digit
enter any character*

special character

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

AIM:
Program to caluculate the roots of a quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,rp,ip;
clrscr();
printf("\n enter a b c values");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf(" not a QE");
else
{
d=b*b-4*a*c;
if(d>=0)
{
printf("\n roots are real");
r1= (-b +sqrt(d))/(2*a);
r2= (-b -sqrt(d))/(2*a);
printf("\n roots are %f and %f",r1,r2);
}
else
{
printf("\n roots are imaginary");
rp= -b/(2*a);
ip=sqrt(-d)/(2*a);
printf("\n roots are ( %f , %f ) and
( %f , %f )",rp,ip,rp,-ip);
}
}
getch();
}

RESULT:
enter a b c values3 5 2
roots are real
roots are -0.666667 and -1.000000
enter a b c values 1 6 3
roots are real
roots are -0.550510 and -5.449490
enter a b c values4 2 8
roots are imaginary
roots are ( -0.250000 , 1.391941 ) and ( -0.250000 , -1.391941 )

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS


AIM:
Program to calculate the total, average and grade of a student

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,tot;
float avg;
char gr;
clrscr();
printf("enter the marks of 3 subjects\n");
scanf("%d%d%d",&m1,&m2,&m3);
tot=m1+m2+m3;
avg=(float)(m1+m2+m3)/3;
if(m1<40 || m2<40 || m3 <40)
gr='F';
else
if(avg<50)
gr='E';
else
if(avg<60)
gr='D';
else
if(avg<70)
gr='C';
else
if(avg<80)
gr='B';
else
gr='A';

printf("\n total = %d",tot);


printf("\n average = %f",avg);
printf("\n Grade = %c",gr);
getch();
}

RESULT:
enter the marks of 3 subjects
89 76 57

total = 222
average = 74.000000
Grade = B

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

AIM:
Program to print the name of the month if number is given

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

void main()
{
int mno;
clrscr();
printf("\n enter any month number");
scanf("%d",&mno);
if(mno==1)
printf("\n jan");
else
if(mno==2)
printf("\n feb");
else
if(mno==3)
printf("\n mar");
else
if(mno==4)
printf("\n april");
else
if(mno==5)
printf("\n may");
else
if(mno==6)
printf("\n june");

else
if(mno==7)
printf("\n july");
else
if(mno==8)
printf("\n august");
else
if(mno==9)
printf("\n september");
else
if(mno==10)
printf("\n october");
else
if(mno==11)

printf("\n november");
else
if(mno==12)
printf("\n december");
else
printf("\n invalid month number");

getch();
}

RESULT:

enter any month number12


december
enter any month number5
may
enter any month number19
invalid month number

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS


AIM:
Program to print the month name if numberis given using the
switch case

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

void main()
{
int mno;
clrscr();
printf("\n enter any month number");
scanf("%d",&mno);
switch(mno)
{
case 1: printf("\n jan");
break;
case 2: printf("\n feb");
break;
case 3: printf("\n mar");
break;
case 4: printf("\n april");
break;
case 5: printf("\n may");
break;
case 6: printf("\n june");
break;
case 7: printf("\n july");
break;
case 8: printf("\n august");
break;
case 9: printf("\n september");
break;

case 10: printf("\n october");


break;

case 11: printf("\n november");


break;
case 12: printf("\n december");
break;
default: printf("\n enter correct month number");
}

getch();
}

RESULT:

enter any month number12

december
enter any month number5

may
enter any month number19

invalid month number


3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS
AIM:
Program to find greatest of three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter 3 nos\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("the no %d is greater\n",a);
else
printf("the no %d is greater\n",c);
}
else
if(b>c)
printf("the no %d is greater\n",b);
getch();
}

RESULT:

enter 3 nos
3
4
2
the no 4 is greater
3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

AIM:
Program to calculate the tax

#include<stdio.h>
#include<conio.h>
void main()
{
float inc,tax=0.0;
int ch;
clrscr();
printf("enter income \n");
scanf("%f",&inc);
ch=(int)(inc/10000);
switch(ch)
{
case 0:
case 1:
case 2: tax=0;
break;
case 3:
case 4: tax=inc*10/100;
break;
case 5:
case 6: tax=inc*20/100;
break;
default: tax=inc*25/100;
break;
}
printf("tax is %f\n",tax);
getch();
}

RESULT:

enter income
54321
tax is 10864.200195
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

AIM:
Program to find the sum of n natural numbers

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

RESULT:
enter the value of n
150
sum is 11325
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS
AIM:
Program to calculate the factorial of a number

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
long fact=1;
clrscr();
printf(“\n enter n integer”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
fact=fact*I;

printf("factorial is %ld\n",fact);
getch();
}

RESULT:

enter an integer 5
factorial is 120
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

AIM:
Program to check whether number is Armstrong number or not

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

void main()
{
int n,sum=0,temp,rem;
clrscr();
printf("enter a no \n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(temp==sum)
printf("the no %d is armstrong\n",temp);
else
printf("the no %d is not armstrong\n",temp);
getch();
}

RESULT:

enter a no
153
the no 153 is Armstrong
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS
AIM:
Program to write the fibonacci series up to a given number

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

void main()
{
int n;
int a=0,b=1,c;
clrscr();
printf("enter the n value \n");
scanf("%d",&n);
printf(“\n %d\t%d\t”,a,b);
c=a+b;
while(c<=n)
{
printf("%d",c);
a=b;
b=c;
c=a+b;
}
getch();
}

RESULT:

enter the n value


8
0 1 1 2 3 5 8
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

AIM:
Program to print mathematical table

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("enter n \n");
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("%d * %d = %d\n",n,i,n*i);
getch();
}

RESULT:

enter n
4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS


AIM:
Program to print the sinx serises

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,sum,term;
int i,n;
clrscr();
printf("enter the no of terms\n");
scanf("%d",&n);
printf("enter the angle in degrees x \n");
scanf("%f",&x);
x=(x*3.14)/180;
sum=x;
term=x;
for(i=1;i<=n;i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i+1));
sum+=term;
}
printf("sin valve of given angle is %f",sum);
getch();
}

RESULT:

enter the no of terms


3
enter the angle in degrees x
30
sin valve of given angle is 0.499770
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS
AIM:
Program to print the cosx serises

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,sum,term;
int i,n;
clrscr();
printf("enter the no of terms\n");
scanf("%d",&n);
printf("enter the angle in degrees x \n");
scanf("%f",&x);
x=(x*3.14)/180;
sum=1;
term=1;
for(i=1;i<=n;i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i-1));
sum+=term;
}
printf("cos valve of given angle is %f",sum);
getch();
}

RESULT:

enter the no of terms


3
enter the angle in degrees x
30
cos valve of given angle is 0.866158

4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS


AIM:
Program to find the sum of digits of a number

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

void main()
{
int n,temp,rem,ans=0;
clrscr();
printf("enter a no \n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
ans+=rem;
n=n/10;
}
printf("the sum of digits of %d is %d\n",temp,ans);
getch();
}

RESULT:

enter a no
234
the sum of digits of 234 is 9
4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

AIM:
Program to check given number is palindrome or not

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

void main()
{
int n,rev=0,temp,rem;
clrscr();
printf("enter a no \n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
printf("the no %d is a palindrome\n",temp);
else
printf("the no %d is not a palindrome\n",temp);
getch();
}

RESULT:

enter a no
121
the no 121 is a palindrome
5. PROGRAMS USING ARRAYS
AIM:
Program to find sum ao all elements in array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n,sum=0;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
sum += a[i];
printf("The sum of all elements is %d\n",sum);
getch();
return 0;
}

RESULT:

Enter array size


6
Enter any 6 elements
98 23 45 67 12 90
The sum of all elements is 335
5. PROGRAMS USING ARRAYS
AIM:
Program to find max andd min number in an array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n,min,max;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
min = a[1];
max = a[1];
for(i=0;i<n;i++)
{
if(a[i]<min)
min = a[i];
if(a[i]>max)
max = a[i];
}
printf("Minimum is %d\nMaximum is %d\n",min,max);
getch();
return 0;
}

RESULT:

Enter array size


6
Enter any 6 elements
78 34 54 12 30 125
Minimum is 12
Maximum is 125

5. PROGRAMS USING ARRAYS


AIM:
Program to reverse the array elements

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n,temp;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n/2;i++)
{
temp = a[i];
a[i] = a[n-1-i];
a[n-1-i] = temp;
}
printf("The array elements after reversing are:\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
return 0;
}

RESULT:

Enter array size


5
Enter any 5 elements
1 2 3 4 5
The array elements after reversing are:
5 4 3 2 1
5. PROGRAMS USING ARRAYS
AIM:
Program to perform linear search

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n,x,xloc = -1;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element you want to search for\n");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
xloc=i;
break;
}
}
if(xloc==-1)
printf("%d is not found in the array\n",x);
else
printf("%d is found at location %d\n",x,xloc);
getch();
return 0;
}

RESULT:

Enter array size


6
Enter any 6 elements
78 45 67 23 14 49
Enter the element you want to search for
23
23 is found at location 3
Enter array size
5
Enter any 5 elements
1 2 34 5 6
Enter the element you want to search for
89
89 is not found in the array
5. PROGRAMS USING ARRAYS
AIM:
Program to perform binary search

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n,x,xloc = -1,low,high,mid;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements in the sorted order\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element you want to search for\n");
scanf("%d",&x);
low = 0;high = n-1;
while(low<=high)
{
mid = (low+high)/2;
if(a[mid]==x)
{
xloc=mid;
break;
}
else if(x<a[mid])
high = mid - 1;
else
low = mid + 1;
}

if(xloc==-1)
printf("%d is not found in the array\n",x);
else
printf("%d is found at location %d\n",x,xloc);
getch();
return 0;
}

RESULT:

Enter array size


6
Enter any 6 elements
78 45 67 23 14 49
Enter the element you want to search for
23
23 is found at location 3

Enter array size


5
Enter any 5 elements
1 2 34 5 6
Enter the element you want to search for
89
89 is not found in the array

5. PROGRAMS USING ARRAYS


AIM:
Program to perform bubble sort

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,n,temp;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
{ for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}

}
}
printf("After sorting, the element are\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
return 0;
}

RESULT:

Enter array size


6
Enter any 6 elements
78 95 62 30 12 39
After sorting, the element are
12 30 39 62 78 95
5. PROGRAMS USING ARRAYS
AIM:
Progrtam to perform selection sort

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,n,temp,maxloc,k;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{ maxloc = 0;
for(j=0;j<=(n-1-i);j++)
{
if(a[j]>a[maxloc])
maxloc = j;
}
temp = a[n-1-i];
a[n-1-i] = a[maxloc];
a[maxloc] = temp;

}
printf("After sorting, the element are\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
return 0;
}

RESULT:
Enter array size
6
Enter any 6 elements
78 95 62 30 12 39
After sorting, the element are
12 30 39 62 78 95
5. PROGRAMS USING ARRAYS
AIM:
Program to read and print a matrix elements

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],i,j,n,m;
clrscr();
printf("Enter matrix size\n");
scanf("%d%d",&m,&n);
printf("Enter matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe Matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
getch();
return 0;
}

RESULT:

Enter matrix size


2
3
Enter matrix elements
1
2
3
4
5
6

The Matrix is
1 2 3
4 5 6

5. PROGRAMS USING ARRAYS


AIM:
Program to perform transepose of a matrix

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],i,j,n,m,temp;
clrscr();
printf("Enter matrix size\n");
scanf("%d%d",&m,&n);
printf("Enter matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("\nThe Matrix is\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=i+1;j<n;j++)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}

printf("\nThe Matrix after transpose,is\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%4d",a[i][j]);
printf("\n");
}
getch();
return 0;
}

RESULT:

Enter matrix size


2
3
Enter matrix elements
1
2
3
4
5
6

The Matrix is
1 2 3
4 5 6

The Matrix after transpose,is


1 4
2 5
3 6
5. PROGRAMS USING ARRAYS

AIM:
Program to add two matrices

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,n,m,p,q;
clrscr();
printf("Enter first matrix size\n");
scanf("%d%d",&m,&n);
printf("Enter second matrix size\n");
scanf("%d%d",&p,&q);

if(m!=p || n!=q)
printf("Size mismatch, Addition is not possible\n");
else
{
printf("Enter first matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter second matrix elements\n");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\nAddition of two Matrices is\n");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j] = a[i][j] + b[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d",c[i][j]);
printf("\n");
}
}
getch();
return 0;
}

RESULT:

Enter first matrix size


2
3
Enter second matrix size
2
3
Enter first matrix elements
1
2
3
4
5
6

Enter second matrix elements


6
5
4
3
2
1

Addition of two Matrices is


7 7 7
7 7 7

Enter first matrix size


2
3

Enter second matrix size


4
3

Size mismatch, Addition is not possible

5. PROGRAMS USING ARRAYS


AIM:
Program to multiply two matrices

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,n,m,p,q;
clrscr();
printf("Enter first matrix size\n");
scanf("%d%d",&m,&n);
printf("Enter second matrix size\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("Not compatible, Multiplication is not possible\n");
else
{
printf("Enter first matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter second matrix elements\n");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\nProduct of two Matrices is\n");

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{ c[i][j] = 0;
for(k=0;k<n;k++)
c[i][j] += a[i][k] * b[k][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%4d",c[i][j]);
printf("\n");
}
}
getch();
return 0;
}

RESULT:

Enter first matrix size


2
3
Enter second matrix size
4
3
Not compatible, Multiplication is not possible
Enter first matrix size
2
3

Enter second matrix size


3
1

Enter first matrix elements


1
2
3
4
5
6

Enter second matrix elements


1
2
3
Product of two Matrices is
14
32

5. PROGRAMS USING ARRAYS


AIM:
Program to count occurrences of charaters and special
characters

#include<stdio.h>
#include<conio.h>
int main()
{
char ch,str[80];
int i,nl,nt,nd,nc,ns;
nl = nt = nd = ns = nc = 0;
clrscr();
printf("Enter a character string,to stop enter EOF \n");
i=0;
while((ch=getchar())!=EOF)
{
str[i] = ch;
i++;
}
str[i] = '\0';
printf("\nThe entered string is\n %s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='\n')
nl++;
else if(str[i]==' ')
ns++;
else if(str[i]=='\t')
nt++; else if(str[i]>='0' && str[i]<='9')
nd++;
else
nc++;
}
printf("\nNo. of characters = %d\n",nc);
printf("No. of digits = %d\n",nd);
printf("No. of newlines = %d\n",nl);
printf("No. of tabs = %d\n",nt);
printf("No. of spaces = %d\n",ns);

getch();
return 0;
}

RESULT:

Enter a character string,to stop enter EOF


43u04 jrir

59jgiogfg jhvcvsopf

wrkjrr23-054328324
^Z

The entered string is


43u04 jrir

59jgiogfg jhvcvsopf

wrkjrr23-054328324

No. of characters = 28
No. of digits = 17
No. of newlines = 5
No. of tabs = 7
No. of spaces = 3

6. PROGRAMS USING FUNCTIONS


AIM:
Program to calculate factorial of an integer

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
long ans,fact(int n);
clrscr();
printf("Enter an integer\n");
scanf("%d",&n);
ans = fact(n);
printf("Factorial of %d is %ld\n",n,ans);
getch();
return 0;
}

long fact(int n)
{
int i;
long product=1;
for(i=1;i<=n;i++)
product *= i;
return product;
}

RESULT:

Enter an integer
10
Factorial of 10 is 3628800
Enter an integer
5
Factorial of 5 is 120
6. PROGRAMS USING FUNCTIONS
AIM:
Program to calculate ncr and npr

#include<stdio.h>
#include<conio.h>
int main()
{
int n,r;
long ans;
long fact(int n);
clrscr();
printf("Enter n and r values\n");
scanf("%d%d",&n,&r);
printf("%dC%d is %d\n",n,r,ncr(n,r));
printf("%dP%d is %d\n",n,r,npr(n,r));
getch();
return 0;
}

long fact(int n)
{
int i;
long product=1;
for(i=1;i<=n;i++)
product *= i;
return product;
}
int ncr(int n,int r)
{
return fact(n)/(fact(n-r)*fact(r));
}
int npr(int n,int r)
{
return fact(n)/fact(n-r);
}

RESULT:
Enter n and r values
10
3
10C3 is 120
10P3 is 720

6. PROGRAMS USING FUNCTIONS


AIM:
Program to calculate factorial of an integer

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
long ans;
long fact(int n);
clrscr();
printf("Enter an integer\n");
scanf("%d",&n);
ans = fact(n);
printf("Factorial of %d is %ld\n",n,ans);
getch();
return 0;
}

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

RESULT:
Enter an integer
10
Factorial of 10 is 3628800

Enter an integer
5
Factorial of 5 is 120

6. PROGRAMS USING FUNCTIONS


AIM:
Program to print prime numbers in the given range

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n;
void primerange(int,int);
clrscr();
printf("Enter the range\n");
scanf("%d%d",&m,&n);
printf("The prime numbers in the given range are\n");
primerange(m,n);
getch();
return 0;
}

void primerange(int m,int n)


{
int i,j;
for(i=m;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i % j == 0)
break;
}
if(i==j)
printf("%d\n",i);
}
}

RESULT:

Enter the range


1
50
The prime numbers in the given range are
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
6. PROGRAMS USING FUNCTIONS
AIM:
Program to print Armstrong numbers in the given range

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int m,n;
void armrange(int,int);
clrscr();
printf("Enter the range\n");
scanf("%d%d",&m,&n);
printf("The armstrong numbers in the given range are\n");
armrange(m,n);
getch();
return 0;
}
void armrange(int m,int n)
{
int i,j,sum,rem,num;
for(i=m;i<=n;i++)
{
sum=0;
num = i;
while(num != 0)
{ rem = num % 10;
num = num / 10;
sum +=pow(rem,3);
}
if(i==sum)
printf("%d\n",i);
}
}

RESULT:
Enter the range
1
1000
The armstrong numbers in the given range are
1
153
370
371
407
6. PROGRAMS USING FUNCTIONS
AIM:
Program to swap two integers

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int m,n;
void swap(int*,int*);
clrscr();
printf("Enter any two integers\n");
scanf("%d%d",&m,&n);
printf("The integers before swapping: m= %d, n = %d\n",m,n);
swap(&m,&n);
printf("The integers after swapping: m = %d,n = %d\n",m,n);
getch();
return 0;
}

void swap(int *m,int *n)


{
int temp;
temp = *m;
*m = *n;
*n = temp;
}

RESULT:

Enter any two integers


20
30
The integers before swapping: m= 20, n = 30
The integers after swapping: m = 30,n = 20

6. PROGRAMS USING FUNCTIONS


AIM:
Program to print fibonacci series up to a given number

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
void fibseries(int);
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
printf("The fibonacci series upto %d\n",n);
fibseries(n);
getch();
return 0;
}

void fibseries(int n)
{
int fib1=0,fib2=1,fib3=1;
printf("%d\n%d\n",fib1,fib2);
while(fib3<=n)
{
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
}
RESULT:
Enter the number
20
The fibonacci series upto 20
0
1
1
2
3
5
8
13
6. PROGRAMS USING FUNCTIONS

AIM:
Program to print nth fibonacci number

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
int fib(int);
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
printf("The %dth fibonacci number = %d\n",n,fib(n));
getch();
return 0;
}

int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fib(n-1)+fib(n-2);
}

RESULT:

Enter the number


5
The 5th fibonacci number = 3

Enter the number


20
The 20th fibonacci number = 4181

6. PROGRAMS USING FUNCTIONS


AIM:
Program to find GCD of two numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n;
int gcd(int,int);
clrscr();
printf("Enter any two integers\n");
scanf("%d%d",&m,&n);
printf("The GCD of %d and %d is = %d\n",m,n,gcd(m,n));
getch();
return 0;
}

int gcd(int m,int n)


{
if(n==0)
return m;
else
return gcd(n,m%n);

RESULT:
Enter any two integers
125
36
The GCD of 125 and 36 is = 1

Enter any two integers


24
8
The GCD of 24 and 8 is = 8
6. PROGRAMS USING FUNCTIONS
AIM:
Program to find sum of all elements in an array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n;
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The sum of all elements is %d\n",arraysum(a,n));
getch();
return 0;
}
int arraysum(int a[],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
sum += a[i];
return sum;
}
RESULT:

Enter array size


6
Enter any 6 elements
98
23
45
67
12
90
The sum of all elements is 335

6. PROGRAMS USING FUNCTIONS


AIM:
Program to perform bubble sort using function

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,n;
void bubblesort(int [],int);
clrscr();
printf("Enter array size\n");
scanf("%d",&n);
printf("Enter any %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubblesort(a,n);
printf("After sorting, the elements are\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
return 0;
}

void bubblesort(int a[],int n)


{
int i,j,temp;
for(i=1;i<=n-1;i++)
{ for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}

}
}
}
RESULT:

Enter array size


6
Enter any 6 elements
78
95
62
30
12
39
After sorting, the element are
12 30 39 62 78 95

6. PROGRAMS USING FUNCTIONS


AIM:
Program to multiply two numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,n,m,p,q;
void matrixmul();
clrscr();
printf("Enter first matrix size\n");
scanf("%d%d",&m,&n);
printf("Enter second matrix size\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("Not compatible, Multiplication is not possible\n");
else
{
printf("Enter first matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter second matrix elements\n");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
matrixmul(a,b,c,m,n,q);
printf("\nProduct of two Matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%4d",c[i][j]);
printf("\n");
}
}
getch();
return 0;
}

void matrixmul(int a[][5],int b[][5],int c[][5],int m,int n,int q)


{
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{ c[i][j] = 0;
for(k=0;k<n;k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}

RESULT:
Enter first matrix size
2
3
Enter second matrix size
4
3

Not compatible, Multiplication is not possible


Enter first matrix size
2
3
Enter second matrix size
3
1
Enter first matrix elements
1
2
3
4
5
6
Enter second matrix elements
1
2
3

Product of two Matrices is


14
32
6. PROGRAMS USING FUNCTIONS
AIM:
Program to reverse the contents of the given string

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

void str_rev(char *s)


{
int i,j;
char temp;
i=0;
j=0;
while(s[j+1]!='\0')
j++;
while(i<j)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
}

void main()
{
char a[20];
clrscr();
printf("enter any string");
gets(a);
str_rev(a);
printf("reverse string is %s",a);
getch();
}

RESULT:
enter any string vasavi
reverse string is ivasav
6. PROGRAMS USING FUNCTIONS
AIM:
Program to find the length of the given string by using user defined
function

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

int str_len(char *s)


{
int i;
i=0;
while(s[i] !='\0')
i++;
return (i);
}

void main()
{
char a[20];
int ans;
clrscr();
printf("enter any string");
gets(a);
ans=str_len(a);
printf("\n length of %s is %d",a,ans);
getch();
}

RESULT:

enter any string bala krishna

length of bala krishna is 14

6. PROGRAMS USING FUNCTIONS


AIM:
Program to copy the contents of the given string to anpther by
susing user defined function
#include<stdio.h>
#include<conio.h>

void str_copy(char *d,char *s)


{
int i;
i=0;
while(s[i]!='\0')
{
d[i]=s[i];
i++;
}
d[i]='\0';
return;
}

void main()
{
char a[20],b[20];
clrscr();
printf("enter any string");
gets(a);
str_copy(b,a);
printf("\n destination string is %s ",b);
getch();
}

RESULT:

enter any string vasavi

destination string is vasavi

6. PROGRAMS USING FUNCTIONS


AIM:
Program to append the contents of the given string to another by
using user defined function

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

void str_cat(char *d,char *s)


{
int i,j;
i=0;
while(d[i]!='\0')
i++;
j=0;
while(s[j]!='\0')
{
d[i]=s[j];
i++;
j++;
}
d[i]='\0';
return;
}

void main()
{
char a[20],b[20];
clrscr();
printf("enter any string");
gets(a);
printf("enter another string");
gets(b);
str_cat(b,a);
printf("\n destination string is %s ",b);
getch();
}
RESULT:
enter any string vasavi
enter another stringcollege
destination string is college vasavi
6. PROGRAMS USING FUNCTIONS
AIM:
Program to compare the contents of given two strings by using user
defined functions
#include<stdio.h>
#include<conio.h>

int str_cmp(char *a, char *b)


{
int i=0;
while(a[i]!='\0' && b[i]!='\0')
{
if(a[i]!=b[i])
return (a[i]-b[i]);
i++;
}
return(a[i]-b[i]);
}

void main()
{
char a[20],b[20];
int ans;
clrscr();
printf("enter any string");
gets(a);
printf("enter another string");
gets(b);
ans=str_cmp(a,b);
printf("\n the difference between %s and
%s is %d",a,b,ans);
getch();
}

RESULT:

enter any string rama


enter another string krishna

the difference between rama and krishna is 7

7. PROGRAMS USING POINTERS


AIM:
Program to reverse the ontents of the given string by using user
defined functions with pointers
#include<stdio.h>
#include<conio.h>

void str_rev(char *s)


{
char *p,*q;
char temp;
p=s;
q=s;
while(*(q+1) !='\0')
q++;
while(p<q)
{
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
}

void main()
{
char a[20];
clrscr();
printf("enter any string");
gets(a);
str_rev(a);
printf("reverse string is %s",a);
getch();
}

RESULT:

enter any string ramana


reverse string is anamar
7. PROGRAMS USING POINTERS
AIM:
Program to find the length of the given string by using user defined
functions with pointers

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

int str_len(char *s)


{
char *p;
p=s;
while(*p !='\0')
p++;
return (p-s);
}

void main()
{
char a[20];
int ans;
clrscr();
printf("enter any string");
gets(a);
ans=str_len(a);
printf("\n length of %s is %d",a,ans);
getch();
}

RESULT:

enter any string bala krishna

length of bala krishna is 14

7. PROGRAMS USING POINTERS


AIM:
Program to copy the contents of the given string to another by
using user defined functions with pointers

#include<stdio.h>
#include<conio.h>
void str_copy(char *d,char *s)
{
char *p,*q;
p=s;
q=d;
while(*p !='\0')
{
*q = *p;
p++;
q++;
}
*q ='\0';
return;
}

void main()
{
char a[20],b[20];
clrscr();
printf("enter any string");
gets(a);
str_copy(b,a);
printf("\n destination string is %s ",b);
getch();
}

RESULT:

enter any string vasavi

destination string is vasavi


7. PROGRAMS USING POINTERS
AIM:
Program to append the contents of the given string to another by
using user defined function

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

void str_cat(char *d,char *s)


{
char *p,*q;
p=s; q=d;
while(*q!='\0')
q++;

while(*p !='\0')
{
*q=*p;
p++;
q++;
}
*q ='\0';
return;
}
void main()
{
char a[20],b[20];
clrscr();
printf("enter any string");
gets(a);
printf("enter another string");
gets(b);
str_cat(b,a);
printf("\n destination string is %s ",b);
getch();
}
RESULT:

enter any string abcd


enter another stringpqrs

destination string is pqrs abcd


7. PROGRAMS USING POINTERS
AIM:
Program to compare the contents of given two strings by using user
defined functions

#include<stdio.h>
#include<conio.h>
int str_cmp(char *a, char *b)
{
char *p,*q;
p=a;
q=b;
while(*p !='\0' && *q!='\0')
{
if(*p != *q)
return (*p - *q);
p++;
q++;
}
return(*p - *q);
}
void main()
{
char a[20],b[20];
int ans;
clrscr();
printf("enter any string");
gets(a);
printf("enter another string");
gets(b);
ans=str_cmp(a,b);
printf("\n the difference between %s and
%s is %d",a,b,ans);
getch();
}
RESULT:

enter any stringrama


enter another stringramana
the difference between rama and ramana is -110
8.PROGRAMS USING STRINGS
AIM:
Proram to abbreviate the contens of the given string

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

main()
{
char s[100];
int i;
clrscr();
printf("\n Enter a String\n");
gets(s);
i=0;
while(s[i]== ' ')
i++;
putchar(s[i]);
putchar(' ');
while(s[i]!='\0')
{
if(s[i]==' ' && s[i+1] != ' ')
{
putchar(s[i+1]);
putchar(' ');
}
i++;
}
getch();
}

RESULT:

enter a string Vasavi College Engg

VCE

8.PROGRAMS USING STRINGS


AIM:
Program to erform the arithmetical operations on rational numbers
using structures
#include<stdio.h>
#include<conio.h>

struct rational
{
int nr;
int dr;
};

main( )
{
struct rational r1,r2,ans;
clrscr();
printf("\n Enter NR and DR of firtst rational number");
scanf("%d%d",&r1.nr,&r1.dr);
printf("\n Enter NR and DR of second rational number");
scanf("%d%d",&r2.nr,&r2.dr);

/* Addition*/
ans.nr=r1.nr*r2.dr + r1.dr* r2.nr;
ans.dr= r1.dr* r2.dr;
printf("\n Sum is %d / %d",ans.nr,ans.dr);

/* substraction*/
ans.nr=r1.nr*r2.dr - r1.dr* r2.nr;
ans.dr= r1.dr* r2.dr;
printf("\n Difference is %d / %d",ans.nr,ans.dr);

/* Multiplication*/
ans.nr=r1.nr*r2.nr;
ans.dr= r1.dr* r2.dr;
printf("\n product is %d / %d",ans.nr,ans.dr);

/* Division*/
ans.nr=r1.nr*r2.dr;
ans.dr= r1.dr* r2.nr;
printf("\n division RESULT: is %d / %d",ans.nr,ans.dr);

getch();
}

RESULT:
Enter NR and DR of firtst rational number
23

Enter NR and DR of second rational number


14

Sum is 11 / 12
Difference is 5 / 12
product is 2 / 12
division RESULT: is 8 / 3

8.PROGRAMS USING STRINGS


AIM:
Program to perform the arithmetical operations on complex
numbers using structures

#include<stdio.h>
#include<conio.h>
struct complex
{
float rp;
float ip;
};

main()
{
struct complex c1,c2,ans;
clrscr();
printf("\n Enter RP and IP of first complex number");
scanf("%f%f",&c1.rp,&c1.ip);
printf("\n Enter RP and IP of second complex number");
scanf("%f%f",&c2.rp,&c2.ip);
/* addition*/
ans.rp= c1.rp + c2.rp;
ans.ip= c1.ip + c2.ip;
printf(" Sum is ( %f , %f )",ans.rp, ans.ip);
/* Substraction*/
ans.rp= c1.rp - c2.rp;
ans.ip= c1.ip - c2.ip;
printf(" Sum is ( %f , %f )",ans.rp, ans.ip);
getch();
}

RESULT:
Enter RP and IP of first complex number
12

Enter RP and IP of second complex number


34
Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )

8.PROGRAMS USING STRINGS


AIM:
Program to perform the arithmetical operations on rational numbers
by using structures and functions

#include<stdio.h>
#include<conio.h>
struct rational
{
int nr;
int dr;
};
typedef struct rational rat;

rat sum( rat r1, rat r2)


{
rat ans;
ans.nr=r1.nr*r2.dr + r1.dr* r2.nr;
ans.dr= r1.dr* r2.dr;
return ans;
}

rat sub( rat r1, rat r2)


{
rat ans;
ans.nr=r1.nr*r2.dr - r1.dr* r2.nr;
ans.dr= r1.dr* r2.dr;
return ans;
}

rat mul( rat r1, rat r2)


{
rat ans;
ans.nr=r1.nr*r2.nr;
ans.dr= r1.dr* r2.dr;
return ans;
}
rat div( rat r1, rat r2)
{
rat ans;
ans.nr=r1.nr*r2.dr;
ans.dr= r1.dr* r2.nr;
return ans;
}

main()
{

rat r1,r2,ans;
clrscr();
printf("\n Enter NR and DR of firtst rational number");
scanf("%d%d",&r1.nr,&r1.dr);
printf("\n Enter NR and DR of second rational number");
scanf("%d%d",&r2.nr,&r2.dr);

/* Addition*/
ans=sum(r1,r2);
printf("\n sum is %d / %d",ans.nr,ans.dr);

/* substraction*/
ans=sub(r1,r2);
printf("\n Difference is %d / %d",ans.nr,ans.dr);

/* Multiplication*/
ans=mul(r1,r2);
printf("\n product is %d / %d",ans.nr,ans.dr);

/* Division*/
ans=div(r1,r2);
printf("\n division RESULT: is %d / %d",ans.nr,ans.dr);

getch();
}

RESULT:

Enter NR and DR of firtst rational number


12

Enter NR and DR of second rational number


3
4
sum is 10 / 8
Difference is -2 / 8
product is 3 / 8
division RESULT: is 4 / 6

8.PROGRAMS USING STRINGS


AIM:
Program to perform arithmetical operations on complex numbers by
usinmg structures and functions

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

struct complex
{
float rp;
float ip;
};
typedef struct complex comp;

comp sum ( comp c1, comp c2)


{
comp ans;
ans.rp= c1.rp + c2.rp;
ans.ip= c1.ip + c2.ip;
return ans;
}

comp sub ( comp c1, comp c2)


{
comp ans;
ans.rp= c1.rp - c2.rp;
ans.ip= c1.ip - c2.ip;
return ans;
}

main()
{
struct complex c1,c2,ans;
clrscr();
printf("\n Enter RP and IP of first complex number");
scanf("%f%f",&c1.rp,&c1.ip);
printf("\n Enter RP and IP of second complex number");
scanf("%f%f",&c2.rp,&c2.ip);

/* addition*/
ans=sum(c1,c2);
printf(" Sum is ( %f , %f )",ans.rp, ans.ip);
/* Substraction*/
ans=sub(c1,c2);
printf(" Sum is ( %f , %f )",ans.rp, ans.ip);

getch();
}

RESULT:

Enter RP and IP of first complex number


12

Enter RP and IP of second complex number


34
Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )
9.PROGRAMS USING STRUCTURES & UNIONS
AIM:
Program to generate student records with total and average by
taking 3 subjects marks as input

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

struct student
{
int sno;
char name[20];
int m1,m2,m3;
int tot;
float avg;
};

main()
{
struct student s[10];
int n,i;
clrscr();
printf("\n Enter no of students");
scanf("%d",&n);
printf("\n Enter no name and marks of 3 subjects of
%d students",n);
for(i=0;i<n ; i++)
{
fflush(stdin);
scanf("%d",&s[i].sno);
fflush(stdin);
gets(s[i].name);
fflush(stdin);
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
if(i<n-1)
printf("\n next student\n \n");
}

for(i=0;i<n;i++)
{
s[i].tot=s[i].m1+ s[i].m2 +s[i].m3;
s[i].avg=(float)s[i].tot/3;
}
printf("\n STUDENT REPORTS ARE\n\n\n");

for( i=0; i<n ; i++)


{

printf("\n \n \n");
printf("\n ROLL NO = %d",s[i].sno);
printf("\n NAME = %s",s[i].name);
printf("\n TOTAL = %d",s[i].tot);
printf("\n AVERAGE = %f",s[i].avg);
}
getch();
}

RESULT:

STUDENT REPORTS ARE

ROLL NO = 1001
NAME = rama
TOTAL = 265
AVERAGE = 88.333336

ROLL NO = 1002
NAME = krishna
TOTAL = 247
AVERAGE = 82.333336

ROLL NO = 1003
NAME = ranga
TOTAL = 190
AVERAGE = 63.333332

9.PROGRAMS USING STRUCTURES & UNIONS


AIM:
Program to calculate the total, average of a given student using
pointers to tructures

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

struct student
{
int sno;
char name[20];
int m1,m2,m3;
int tot;
float avg;
};

main()
{
struct student s,*p;
clrscr();
p=&s;
printf("\n Enter no name and marks of 3 subjects
of a student\n");
fflush(stdin);
scanf("%d",&p->sno);
fflush(stdin);
gets(p->name);
fflush(stdin);
scanf("%d%d%d",&p->m1,&p->m2,&p->m3);

p->tot=p->m1+ p->m2 +p->m3;


p->avg=(float)p->tot/3;

printf("\n STUDENT REPORT IS\n\n");

printf("\n ROLL NO = %d",p->sno);


printf("\n NAME = %s",p->name);
printf("\n TOTAL = %d",p->tot);
printf("\n AVERAGE = %f",p->avg);

getch();
}

RESULT:

Enter no name and marks of 3 subjects of a student


1001
rama
90 89 87

STUDENT REPORT IS
ROLL NO = 1001
NAME = rama
TOTAL = 266
AVERAGE = 88.666664
9.PROGRAMS USING STRUCTURES & UNIONS
AIM:
Program toprint tomorrows date if today’s darte is given

#include<stdio.h>
#include<conio.h>
struct date
{
int dd;
int mm;
int yy;
};
void tommorrow( struct date *p)
{
int month[]={ 31,28,31,30,31,30,31,31,30,31,30,31};
if((p->yy % 4 == 0 && p->yy % 100 != 0) ||
(p->yy % 400 == 0))
month[1]=29;
if(p->dd<1 || p->dd > month[p->mm - 1] ||
p->mm < 1 || p->mm > 12 || p->yy < 0)
printf("\n Invalid date");
else
{
p->dd++;
if(p->dd > month[p->mm - 1])
{
p->dd=1;
p->mm++;
if(p->mm >12)
{
p->mm=1;
p->yy++;
}
}
}
}

main()
{
struct date d,*p=&d;
clrscr();
printf("\n Enter today date");
scanf("%d%d%d",&p->dd,&p->mm,&p->yy);
tommorrow(p);
getch();
}
RESULT:

Enter today date


125 2007

10. Programs using Files


AIM:
Program to create a file

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("text.dat","w");
printf("Enter text ,to stop press Ctrl-Z\n");
while((ch=getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);

}
RESULT:

Enter text ,to stop press Ctrl-Z


This is my first program in C
language to create a file.
Bye.
To see the contents of this file
open text.dat in any
editor program.
^Z
File Saved on disk

text.dat:
This is my first program in C
language to create a file.
Bye.
To see the contents of this file
open text.dat in any
editor program.
10. PROGRAMS USING FILES
AIM:
Program to print the contens of the file on the monitor

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("text.dat","r");
if(fp==NULL)
printf("No such file\n");
else
{
printf("File contents are:\n");
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
fclose(fp);
}
getch();
}

RESULT:

Given text.dat
File contents are:
This is my first program
in C to create a file with some text.
Bye
To see the contents of the file open text.dat
in any editor.

Given example.dat
No such file

10. PROGRAMS USING FILES


AIM:
Program to copy one file in to another
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("example.dat","r");
if(fp==NULL)
printf("No such file\n");
else
{
printf("File contents are:\n");
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
fclose(fp);
}
getch();
}

RESULT:

source file: text.dat


destination file: example.dat
file copied
10. PROGRAMS USING FILES
AIM:
Program to create a file of students records

#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
int marks;
};
void main()
{
int i,n;
struct student s;
FILE *fp;
clrscr();
printf("How many records you want to store in the file\n");
scanf("%d",&n);
printf("Enter any %d student records\n",n);
printf("For each record enter rollnumber,name and marks\n");
fp = fopen("student.dat","wb");
for(i=1;i<=n;i++)
{
printf("enter record\n");
scanf("%d",&s.rno);
fflush(stdin);
gets(s.name);
scanf("%d",&s.marks);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
printf("file created\n");
getch();
}

RESULT:

How many records you want to store in the file


2
Enter any 2 student records
For each record enter rollnumber,name and marks
enter record
100
rama rao
600
enter record
200
sita kumari
500
file created

10. PROGRAMS USING FILES


AIM:
Program to display student records by reading from a file

#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
int marks;
};
void main()
{
int i,n;
struct student s;
FILE *fp;
clrscr();
fp = fopen("student.dat","rb");
while(fread(&s,sizeof(s),1,fp)==1)
{
printf("%d\t%s\t%d\n",s.rno,s.name,s.marks);
}
fclose(fp);
getch();
}

RESULT:

100 rama rao 600


200 sita kumari 500
11. Simple C++ Programs
AIM:
Program to print average of two numbers
#include<iostream.h>
int main()
{
float n1,n2,sum,avg;
cout<<"enter two numbers";
cin>>n1;
cin>>n2;
sum=n1+n2;
avg=sum/2;
cout<<"sum= "<<sum<<"\n";
cout<<"Average ="<<avg<<"\n";
return 0;
}

Result:

input: 10 20

30 15
11. Simple C++ Programs

AIM:
Program to demonstrate scope resolution operator

#include<iostream.h>
int m=10;
int main()
{
int m=20;
{
int k=m;
int m=30;
cout<<"we are in inner block";
cout<<"k= "<<k<<"\n";
cout<<"m= "<<m<<"\n";
cout<<"::m= "<<::m<<"\n";
}
cout<<"we are in outer block";
cout<<"m= "<<m<<"\n";
cout<<"::m= "<<::m<<"\n";

return 0;
}

Result:

we are in inner block 20 30 10

we are in outer block 20 10


11. Simple C++ Programs
AIM:
Program to demonstrate multiple and division operation using
inline functions

#include<iostream.h>
inline float mul(float x,float y)
{
return(x*y);
}
inline float div(float x,float y)
{
return(x/y);
}
int main()
{
float a=12.34;
float b=9.82;
cout<<mul(a,b)<<"\n";
cout<<div(a,b)<<"\n";

return 0;
}

Result:

121.17,1.25
11. Simple C++ Programs
AIM:
Program to calculate of volume of cube, cylinder, rectangular box
using Function overloading

// function overloading
#include<iostream.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
cout<<volume(10)<<"\n";
cout<<volume(2.5,8)<<endl;
cout<<volume(100l,75,15)<<"\n";
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
}
long volume(long l,int b,int h)
{
return(l*b*h);
}

Result:

Output: 1000,157,112500
11. Simple C++ Programs

AIM:
Program to find maximum number using function template
#include<iostream.h>
#include<conio.h>
template<class T>
T maximum(T a,T b,T c)
{
T m=a;
if(b>m)
m=b;
if(c>m)
m=c;
return m;
}
int main()
{
int i1,i2,i3;
clrscr();
cout<<"enter i1,i2,i3";
cin>>i1>>i2>>i3;
cout<<maximum(i1,i2,i3)<<"\n";
float f1,f2,f3;
cout<<"enter f1,f2,f3";
cin>>f1>>f2>>f3;
cout<<maximum(f1,f2,f3)<<endl;
char c1,c2,c3;
cout<<"enter c1,c2,c3";
cin>>c1>>c2>>c3;
cout<<maximum(c1,c2,c3)<<"\n";
return 0;
}

Result:
input:10 20 30
output:30
input:3.2 4.6 2.4
output:4.6
input:a w c
output:w
12. Program using classes and objects
AIM:
Program to demonstrate classes objects

#include<iostream.h>
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<"number:"<<number<<"\n";
cout<<"cost:"<<cost<<endl;
}
};
void item :: getdata(int a,float b)
{
number=a;
cost=b;
}
int main()
{
item x;
cout<<"\n object x"<<"\n";
x.getdata(100,299.95);
x.putdata();
item y;
cout<<"\n object y"<<"\n";
y.getdata(200,175.50);
y.putdata();
return 0;
}

Result:

100 299.95
200 175.50
12. Program using classes and objects
AIM:
Program to show the working of static data members

#include<iostream.h>
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount(void)
{
cout<<"count: ";
cout<<count<<"\n";
}
};
int item :: count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<" after reading data\n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}
Results:

0,0,0
3,3,3
12. Program using classes and objects
AIM:
Program to demonstrate working of static member function

#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"<<code<<"\n";
}
static void showcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int test :: count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
Result:

1,2,3
12. Program using classes and objects
AIM:
Program to demonstrate working friend functions

#include<iostream.h>
class ABC;
class XYZ
{
int x;
public:
void setvalue(int i){x=i;}
friend void max(XYZ,ABC);
};
class ABC
{
int a;
public:
void setvalue(int i){a=i;}
friend void max(XYZ,ABC);
};
void max(XYZ m,ABC n)
{
if(m.x>=n.a)
cout<<m.x;
else
cout<<n.a;
}
int main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
return 0;
}

Result:

20
12. Program using classes and objects
AIM:
Program to calculate addition of two complex numbers using friend
function

#include<iostream.h>
class complex
{
float x,y;
public:
void input(float real,float imag)
{
x=real;
y=imag;
}
friend complex sum(complex,complex);
void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void complex::show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
complex A,B,C;
A.input(3.1,5.65);
B.input(2.75,1.2);
C=sum(A,B);
cout<<"A=";A.show(A);
cout<<"B = ";B.show(B);
cout<<"c=";C.show(C);
return 0;
}
Result:

5.85+i6.85
12. Program using classes and objects
AIM:
Program to calculate addition of two complex numbers using copy
constructors and friend function

#include<iostream.h>
class complex
{
float x,y;
public:
complex(){}
complex(float a){x=y=a;}
complex(float real,float imag)
{x=real;y=imag;}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
complex A(2.7,3.5);
complex B(1.6);
complex C;
C=sum(A,B);
cout<<"A=";show(A);
cout<<"B=";show(B);
cout<<"C=";show(C);
complex p,q,r;
p=complex(2.5,3.9);
q=complex(1.6,2.5);
r=sum(p,q);
cout<<endl;
cout<<"p=";show(p);
cout<<"q=";show(q);
cout<<"r=";show(r);
return 0;
}

Result:

4.3+i5.1,4.1+i6.4
12. Program using classes and objects
AIM:
Program to demonstrate working of constructors
#include<iostream.h>
class code
{
int id;
public:
code(){}
code(int a) { id=a;}
code(code & x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"\n id of A:";A.display();
cout<<"\n id of B:";B.display();
cout<<"\n id of C:";C.display();
cout<<"\n id of D:";D.display();
return 0;
}

Result:
100,100,100,100

12. Program using classes and objects


AIM:
Program to demonstrate working of string operations
#include<iostream.h>
#include<string.h>

class String
{
char *name;
int length;
public:
String()
{
length=0;
name=new char[length+1];
}
String(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display(void)
{ cout<< name <<"\n";}
void join(String &a,String &b);
};
void String::join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
};
int main()
{
char *first="Ravi";
String name1(first),name2("Prasad"),name3("rajesh"),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
return 0;
}

Result:
raviprasad,raviprasadrajesh
12. Program using classes and objects
AIM:
Program to read elements into a matrix and search for an element in
a matrix
#include<iostream.h>
class matrix
{
int **p;
int d1,d2;
public:
matrix(int x,int y);
void get_element(int i,int j,int value)
{
p[i][j]=value;
}
int & put_element(int i,int j)
{
return p[i][j];
}
};
matrix :: matrix(int x,int y)
{
d1=x;
d2=y;
p=new int *[d1];
for(int i=0;i<d1;i++)
p[i]=new int[d2];
}
int main()
{
int m,n;
cout<< "enter size of matrix";
cin>>m>>n;
matrix A(m,n);
cout<<"enter matrix elements";
int i,j,value;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
cin>>value;
A.get_element(i,j,value);
}
cout<<endl;
cout<<A.put_element(1,2);
return 0;
}

Result:

input:3 3
input:1 2 3 4 5 6 7 8 9
input:1,2

12. Program using classes and objects


AIM:
Program to demonstrate working of destructor in a class
#include<iostream.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n no of objects created"<<count;
}
~alpha()
{
cout<<"\n no of objects destroyed"<<count;
count--;
}
};
int main()
{
alpha A1,A2,A3,A4;
{
cout<<"enter block";
alpha A5;
}
{
cout<<"enter block2";
alpha A6;
}
return 0;
}

Result:
object created:1,2,3,4,5
destroyed:5
created:5
destroyed:5
destroyed:4,3,2,1
13. Program on operator overloading
AIM:
Program t demonstrate working of unary minus operator

#include<iostream.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<" ";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}

int main()
{
space S;
S.getdata(10,-20,30);
cout<<"S:";
S.display();
-S;
cout<<"S:";
S.display();
return 0;
}

Result:
10 -20 30

-10 20 -30
13. Program on operator overloading
AIM:
Program to demonstrate overloading of + operator

#include<iostream.h>
class complex
{
float x;
float y;
public:
complex(){ }
complex(float real,float imag)
{ x=real; y=imag;}
complex operator+(complex);
void display(void);
};

complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
complex C1,C2,C3;
C1=complex(2.5,3.5);
C2=complex(1.6,2.7);
C3=C1+C2;

cout<<"C1=";C1.display();
cout<<"C2=";C2.display();
cout<<"C3=";C3.display();
return 0;
}
Result:
c1=2.5+ j 3.5
c2=1.6+j2.7
c3=4.1+j6.2
13. Program on operator overloading
AIM:
Program to demonstrate overloading operators using friend
functions

#include<iostream.h>
const size=3;
class vector
{
int v[size];
public:
vector();
vector(int *x);
friend vector operator*(int a,vector b);
friend vector operator*(vector b,int a);
friend istream & operator>>(istream &,vector &);
friend ostream & operator>>(ostream &,vector &);
};
vector::vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector::vector(int *x)
{
for(int i=0;i<size;i++)
v[i]=x[i];
}
vector operator *(int a,vector b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator *(vector b,int a)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=b.v[i]*a;
return c;
}
istream & operator>>(istream &din,vector &b)
{
for(int i=0;i<size;i++)
din>>b.v[i];
return(din);
}
ostream & operator<<(ostream &dout,vector &b)
{

dout<<"("<<b.v[0];
for(int i=1;i<size;i++)
dout<<","<<b.v[i];
dout<<")";
return(dout);
}

int x[size]={2,4,6};
int main()
{
vector m;
vector n=x;
cout<<"Enter elements of vector m"<<"\n";
cin>>m;
cout<<"\n";
cout<<m;
vector p,q;
p=2*m;
q=n*2;
cout<<"\n";
// cout<<"p="<<p<<"\n";
// cout<<"q="<<q<<"\n";
cout<<p;
cout<<q;
return 0;
}
//input:5 10 15

Result:

m(5,10,15)
p(10,20,30)
q(4,8,12)

13. Program on operator overloading


AIM:
Program to demonstrate overloading of operator on strings using
friend function
#include<string.h>
#include<iostream.h>
class string
{
char *p;
int len;
public:
string(){len=0;p=0;}
string(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
string(const string & s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}
~string(){delete p;}
friend string operator+(const string &s,const string &t)
{
string temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
}
friend int operator<=(const string &s,const string &t);
friend void show(const string s);
};
int operator<=(const string &s,const string &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m<=n) return(1);
else return(0);
}
void show(const string s)
{
cout<<s.p;
}
int main()
{
string s1="ravi";
string s2="prasad";
string s3="rajesh";
string t1,t2,t3;
t1=s1;
t2=s2;
t3=s1+s3;
cout<<"\n t1=";show(t1);
cout<<"\n t2=";show(t2);
cout<<"\n";
cout<<"\n t3=";show(t3);
cout<<"\n\n";
if(t1<=t3)
{
show(t1);
cout<<"smaller than";
show(t3);
cout<<"\n";
}
else
{
show(t3);
cout<<"smaller than";
show(t1);
cout<<"\n";
}
return 0;
}

Result:

ravi
prasad
ravirajesh
ravi smaller than ravirajesh
14. Program on inheritance
AIM:
Program to demonstrate working of single inheritance using public
as access specifier

#include<iostream.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D : public B
{
int c;
public:
void mul(void);
void display(void);
};
void B :: get_ab()
{
a=5;b=10;
}
int B::get_a()
{
return a;
}
void B:: show_a()
{
cout<<"a="<<a<<"\n";
}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<"a="<<get_a()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();

d.display();
d.b=20;
d.mul();
d.display();
return 0;
}

Result:

a=5
a=5
b=10
c=50
a=5
b=20
c=100
14. Program on inheritance
AIM:
Program to demonstrate single inheritance using private as access
specifier

#include<iostream.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D : private B
{
int c;
public:
void mul(void);
void display(void);
};
void B :: get_ab()
{
cout<<"enter values for a and b";
cin>>a>>b;
}
int B::get_a()
{
return a;
}
void B:: show_a()
{
cout<<"a="<<a<<"\n";
}
void D::mul()
{
get_ab();
c=b*get_a();
}
void D::display()
{
show_a();
// cout<<"a="<<get_a()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
int main()
{
D d;
// d.get_ab();
d.mul();
// d.show_a();
d.display();
// d.b=20;
d.mul();
d.display();
return 0;
}

Result:

input 5 10
output 5 10 50
input 12 20
output 12 20 240
14. Program on inheritance
AIM:
Program to demonstrate multilevel inheritance

#include<iostream.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student::get_number(int a)
{
roll_number=a;
}
void student :: put_number()
{
cout<<"rollnumber:"<<roll_number<<"\n";
}
class test : public student
{
protected:
float sub1,sub2;
public:
void get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void put_marks()
{
cout<<"marks in sub1:"<<sub1<<"\n";
cout<<"marks in sub2:"<<sub2<<"\n";
}
};
class result :public test
{
float total;
public:
void display()
{
total=sub1+sub2;
put_number();
put_marks();
cout<<"total="<<total<<"\n";
}
};

int main()
{
result student1;
student1.get_number(111);
student1.get_marks(75.0,59.8);
student1.display();
return 0;
}

Result:

rollnumber:111
marks in sub1 75
marks in sub2:59.5
total:134.5
14. Program on inheritance
AIM:
Program to demonstrate multiple inheritances
#include<iostream.h>
class M
{
protected :
int m;
public :
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
cout<<"m*n"<<m*n<<"\n";
}
};
void M :: get_m(int x)
{
m=x;
}
void N :: get_n(int y)
{
n=y;
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}

Result:

output:m=10
n=20
m*n=200
15. Program on polymorphism
AIM:
Program to demonstrate working of virtual base class
#include<iostream.h>
class student
{
protected:
int roll_number;
public :
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"rollnumber :"<<roll_number<<"\n";
}
};
class test : virtual public student
{
protected:
float p1,p2;
public:
void get_marks(float x,float y)
{
p1=x;
p2=y;
}
void put_marks(void)
{
cout<<"marks"<<"\n"<<"part1:"<<p1<<"\npart2:"<<p2<<endl;
}
};
class sports : public virtual student
{
protected :
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sport wt:"<<score<<"\n";
}
};
class result: public test,public sports
{
float total;
public:
void display(void)
{
total=p1+p2+score;
put_number();
put_marks();
put_score();
cout<<"total score:"<<total<<"\n";
}
};
int main()
{
result s1;
s1.get_number(678);
s1.get_marks(77.5,57.5);
s1.get_score(8.0);
s1.display();
return 0;
}

Result:
rollno:678
marks obtained:77.5 57.5
sports wt:8
total score:143
15. Program on polymorphism
AIM:
Program to demonstrate working of constructors in derived class

#include<iostream.h>
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"alpha initialized\n";
}
void show_x(void)
{
cout<<"x="<<x<<"\n";
}
};
class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"beta initialized";
}
void show_y(void)
{
cout<<"y="<<y<<"\n";
}
};
class gamma : public alpha,public beta
{
int m,n;
public:
gamma(int a,float b,int c,int d): alpha(a),beta(b)
{
m=c;
n=d;
}
void show_mn(void)
{
cout<<"m="<<m<<"\n"<<"n="<<n<<"\n";
}
};
int main()
{
gamma g(5,10.27,20,30);
cout<<"\n";
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

Result:

beta initialized
alpha initialized
gamma initialized
x=5
y=10.27
m=20
n=30
15. Program on polymorphism
AIM:
Program to demonstrate working of pointers to objects
#include<iostream.h>
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show(void)
{
cout<<"code:"<<code<<"\n";
cout<<"price:"<<price<<"\n";
}
};
const int size=2;
int main()
{
item *p=new item[size];
item *d=p;
int x,i;
float y;
for(i=0;i<size;i++)
{
cout<<"enter input";
cin>>x>>y;
p->getdata(x,y);
p++;
}
for(i=0;i<size;i++)
{
cout<<"item:"<<i+1<<"\n";
d->show();
d++;
}
return 0;
}

Result:
40 500
50 600
40 500
50 600
15. Program on polymorphism

AIM:
Program to demonstrate working of ‘this’ pointer
#include<iostream.h>
#include<string.h>
class person
{
char name[20];
float age;
public:
person(char *s,float a)
{
strcpy(name,s);
age=a;
}
person & person::greater(person & x)
{
if(x.age>=age)
return x;
else
return *this;
}
void display(void)
{
cout<<"name:"<<name<<"\n";
cout<<"age:"<<age<<"\n";
}
};
int main()
{
person p1("ravi",78.90);
person p2("prasad",56.49);
person p3("rajesh",34.67);
person p=p1.greater(p3);
cout<<"elder person is:\n";
p.display();
p=p1.greater(p2);
cout<<"elder person is:\n";
p.display();
return 0;
}

Result:
elder person is:
ravi 78.9
elder person is:
ravi 78.9

15. Program on polymorphism


AIM:
Program to demonstrate working of virtual functions
#include<iostream.h>
class Base
{
public:
void display() {cout<<"\n Display base";}
virtual void show() { cout<<"\n show base";}
};
class Derived:public Base
{
public:
void display() { cout<<"\n Display derived";}
void show() { cout<<"\n show derived";}
};
int main()
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base \n";
bptr=&B;
bptr->display();
bptr->show();
cout<<"\n\n bptr points to Derived\n";
bptr=&D;
bptr->display();
bptr->show();
return 0;
}
Result:

bptr points to base


display base
show base
bptr points to derived
display base
show derived */
15. Program on polymorphism
AIM:
Program to demonstrate working of runtime polymorphism
#include<iostream.h>
#include<string.h>
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display(){ }
};
class book:public media
{
int pages;
public:
book(char *s,float a,int p):media(s,a)
{
pages=p;
}
void display();
};
class tape:public media
{
float time;
public:
tape(char *s,float a,float t):media(s,a)
{
time=t;
}
void display();
};
void book::display()
{
cout<<"\n Title:"<<title;
cout<<"\n Pages:"<<pages;
cout<<"\n Price:"<<price;
}
void tape::display()
{
cout<<"\n Title:"<<title;
cout<<"\n play time:"<<time<<"mins";
cout<<"\n price:"<<price;
}
int main()
{
char *title=new char[30];
float price,time;
int pages;

cout<<"\n ENTER BOOK DETAILS\n";


cout<<"Title:";cin>>title;
cout<<"Price:";cin>>price;
cout<<"Pages:";cin>>pages;
book book1(title,price,pages);

cout<<"\n ENTER TAPE DETAILS\n";


cout<<"Title:";cin>>title;
cout<<"Price:";cin>>price;
cout<<"Play time(mins):";cin>>time;
tape tape1(title,price,time);
media* list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n MEDIA DETAILS";
cout<<".......BOOK......";
list[0]->display();
cout<<"\n......TAPE....";
list[1]->display();
return 0;
}

Result:
ENTER BOOK DETAILS
Tile: AAA
Price :255
Pages:100

ENTER TAPE DETIALS


Tile : BBB
Price : 75
Play time (min): 60

MEDIA DETAILS
….. BOOK….

Tile: AAA
Price: 255
Pages: 100

…..TAPE…..

Tile: BBB
Price: 75
Play time (min): 60

16. Template programs


AIM:
Program to demonstrate working of vector operations using
templates
#include<iostream.h>
const size=3;
template<class T>
class vector
{
T* v;
public:
vector()
{
v=new T[size];
for(int i=0;i<size;i++)
v[i]=0;
}
vector(T* a)
{
for(int i=0;i<size;i++)
v[i]=a[i];
}
T operator*(vector &y)
{
T sum=0;
for(int i=0;i<size;i++)
sum+=this->v[i]*y.v[i];
return sum;
}
};
int main()
{
int x[3]={1,2,3};
int y[3]={4,5,6};
vector <int>v1;
vector<int>v2;
v1=x;
v2=y;
int R=v1*v2;
cout<<"R="<<R<<"\n";
return 0;
}

Result:

r=32
16. Template programs
AIM:
Program to demonstrate working of bubble sort using template
functions
#include<iostream.h>
template<class T>
void bubble(T a[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template<class X>
void swap(X &a,X &b)
{
X temp=a;
a=b;
b=temp;
}
int main()
{
int x[5]={10,50,30,40,20};
float y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5);
bubble(y,5);
cout<<"Stored x-array:";
for(int i=0;i<5;i++)
cout<<x[i]<<" ";
cout<<endl;

cout<<"Stored y-array:";
for(int j=0;j<5;j++)
cout<<y[j]<<" ";
cout<<endl;
return 0;
}

Result:

10 20 30 40 50
1.1 2.2 3.3 4.4 5.5
16. Template programs
AIM:
Program to calculate roots of equations using templates
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
template<class T>
void roots(T a,T b,T c)
{
T d=b*b-4*a*c;
if(d==0)
{
cout<<"R1=R2="<<-b/(2*a)<<endl;
}
else if(d>0)
{ cout<<"Roots are real\n";
float R=sqrt(d);
float R1=(-b+R)/(2*a);
float R2=(-b-R)/(2*a);
cout<<"R1="<<R1<<"and";
cout<<"R2="<<R2<<endl;
}
else
{ cout<<"Roots are complex\n";
float R1=-b/(2*a);
float R2=sqrt(-d)/(2*a);
cout<<"Real part="<<R1<<endl;
cout<<"Imaginary part="<<R2;
cout<<endl;
}
}
int main()
{
cout<<"Integer coefficients\n";
roots(1,-5,6);
cout<<"\n Float coefficients\n";
roots(1.5,3.6,5.0);
return 0;
}

Result:
Roots are real
R1 = 2.5
R2 = 2.5
Roots are complex
Real part = -1.2
Imaginary part = 1.375
17. Exception handling
AIM:
Program to demonstrate working of exception handling
#include<iostream.h>
void divide(int x,int y,int z)
{
cout<<"\n we are inside the function\n";
if((x-y)!=0)
{
int R=z/(x-y);
cout<<"Result="<<R<<"\n";
}
else
{
throw(x-y);
}
}
int main()
{
try
{
cout<<"we are inside the try block\n";
divide(10,20,30);
divide(10,10,20);
}
catch(int i)
{
cout<<"caught the exception\n";
}
return 0;
}

Result:

result =-3
caught the exception

You might also like