Transcript
Page 1: Karan Comp Programs for cbse class 12

COMPUTER PROGRAMS

1.Write a program that accepts elements of a 4*4 array and displays it as 1 dimension array.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int A[4][4];

cout<<"Enter elements"<<endl;

for(int i=0;i<4;i++)

{

cout<<"Enter elements of row "<<i+1<<endl;

for(int j=0;j<4;j++)

{

cin>>A[i][j];

}

}

int B[16];

static int k=0;

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

{

for(int j=0;j<4;j++)

{

B[k]=A[j][i];

Page 2: Karan Comp Programs for cbse class 12

k++;

}

}

cout<<"Array is-"<<endl;

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

cout<<B[i];

getch();

}

Output-

Enter elements of first row

1

2

3

4

Enter elements of second row

5

6

7

8

Enter elements of third row

9

10

11

12

Enter elements of fourth row

Page 3: Karan Comp Programs for cbse class 12

13

14

15

16

Array is-15913261014371115481216

2.Write a program that accepts a three digit number and displays all possible permutations of its three digits.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n;

cout<<"Enter 3 digit no"<<endl;

cin>>n;

int a=n/100;

n=n%100;

int b=n/10;

n=n%10;

int c=n;

cout<<"Numbers-"<<endl;

cout<<a<<b<<c<<endl;

cout<<a<<c<<b<<endl;

cout<<b<<a<<c<<endl;

cout<<b<<c<<a<<endl;

Page 4: Karan Comp Programs for cbse class 12

cout<<c<<a<<b<<endl;

cout<<c<<b<<a<<endl;

getch();

}

Output-

Enter 3 digit no

123

Numbers-

123

132

213

231

312

321

3.Write a program that displays area of square, rectangle and triangle using values given by user beforehand.

#include<iostream.h>

#include<conio.h>

#include<math.h>

int area(int);

int area(int,int);

int area(int,int,int);

void main()

{

clrscr();

Page 5: Karan Comp Programs for cbse class 12

int a=2,b=3,c=4;

cout<<"Area of square="<<area(a)<<endl;

cout<<"Area of rectangle="<<area(a,b)<<endl;

cout<<"Area of triangle="<<area(a,b,c)<<endl;

getch();

}

int area(int a)

{

return(a*a);

}

int area(int a,int b)

{

return(a*b);

}

int area(int a,int b,int c)

{

int s=(a+b+c)/2;

return(sqrt(s*(s-a)*(s-b)*s-c));

}

Output-

Area of square=4

Area of rectangle=6

Area of triangle=5

4.Write a program class account with subclasses savings and current.

#include<iostream.h>

Page 6: Karan Comp Programs for cbse class 12

#include<conio.h>

#include<stdio.h>

#include<math.h>

class account

{

public:

char name[25];

int accno;

int opbal;

void read()

{

cout<<"Enter name of customer"<<endl;

gets(name);

cout<<"Enter account no"<<endl;

cin>>accno;

cout<<"Enter opening balance"<<endl;

cin>>opbal;

}

void dep()

{

int deposit;

cout<<"Enter money to be deposited"<<endl;

cin>>deposit;

opbal+=deposit;

}

Page 7: Karan Comp Programs for cbse class 12

void disp()

{

cout<<"Name of customer-";

puts(name);

cout<<"Account number-"<<accno<<endl;

cout<<"Balance="<<opbal<<endl;

}

}acc;

class savings:public account

{

public:

int take;

void withdraw()

{

cout<<"Enter ammount you wish to withdraw"<<endl;

cin>>take;

opbal-=take;

}

void ci()

{

take=take*pow((1+0.04),3)-take;

cout<<"Customer must pay interest of "<<take<<endl;

}

}sav;

Page 8: Karan Comp Programs for cbse class 12

class current:public account

{

public:

int check;

void checkbook()

{

cout<<"Enter ammount you wish to withdraw"<<endl;

cin>>check;

opbal-=check;

}

void minbal()

{

if(opbal<100)

cout<<"You must pay penalty of "<<(100-opbal)*0.04<<endl;

}

}cur;

void main()

{

clrscr();

int ch;

cout<<"Enter 1 for savings account, 2 for current account"<<endl;

cin>>ch;

if(ch==1)

{

cout<<"Enter customer details"<<endl;

Page 9: Karan Comp Programs for cbse class 12

sav.read();

cout<<"Enter 1 to deposit, 2 to withdraw"<<endl;

cin>>ch;

if(ch==1)

sav.dep();

else

{

sav.withdraw();

}

cout<<"Details-"<<endl;

sav.disp();

if(ch==2)

sav.ci();

}

else

{

cout<<"Enter customer details"<<endl;

cur.read();

cout<<"Enter 1 to deposit and 2 to withdraw"<<endl;

cin>>ch;

if(ch==1)

cur.dep();

else

{

cur.checkbook();

Page 10: Karan Comp Programs for cbse class 12

}

cout<<"Details-"<<endl;

cur.disp();

if(ch==2)

cur.minbal();

}

getch();

}

Output-

Enter 1 for savings account, 2 for current account

1

Enter customer details

Enter name of customer

Karan

Enter account no

123

Enter opening balance

1000

Enter 1 to deposit, 2 to withdraw

1

Enter money to be deposited

100

Details-

Name of customer-Karan

Account number-123

Page 11: Karan Comp Programs for cbse class 12

Balance-1100

5.Write a program that accepts a number in decimal form and displays binary form of it.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n;

int r=1;

cout<<"Enter decimal no"<<endl;

cin>>n;

while(n>0)

{

int d=n%2;

r=r*10+d;

n=n/2;

}

r=r/10;

cout<<"Binary="<<r;

getch();

}

Output-

Enter decimal no

2

Page 12: Karan Comp Programs for cbse class 12

Binary=10

6.Write a program class bank with two subclasses book and audio cassette.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class publication

{

public:

char title[25];

float price;

void getdata()

{

cout<<"Enter title"<<endl;

gets(title);

cout<<"Enter price"<<endl;

cin>>price;

}

void putdata()

{

cout<<"Title-";

puts(title);

cout<<"Price-"<<price<<endl;

}

}pub;

class book:public publication

Page 13: Karan Comp Programs for cbse class 12

{

public:

publication cheat;

int count;

void getdata()

{

cout<<"Enter count"<<endl;

cin>>count;

}

void putdata()

{

cout<<"Count-"<<count<<endl;

}

}bo;

class audio:public publication

{

public:

publication cheat;

float tape;

void getdata()

{

cout<<"Enter running time of tape in mins"<<endl;

cin>>tape;

}

void putdata()

Page 14: Karan Comp Programs for cbse class 12

{

cout<<"Time in minutes-"<<tape<<endl;

}

}aud;

void main()

{

clrscr();

pub.getdata();

bo.getdata();

aud.getdata();

pub.putdata();

bo.putdata();

aud.putdata();

getch();

}

Output-

Enter title

Computer Science

Enter price

200

Enter count

300

Enter running time of tape in minutes

50

Page 15: Karan Comp Programs for cbse class 12

Title-Computer Science

Price-200

Count-300

Time in minutes-50

7.Write a program to accept radius of circle and display area and circumference.

#include<iostream.h>

#include<conio.h>

class circle

{

int r;

public:

float ar,ci;

void input(int radius)

{

r=radius;

}

void calc();

float area();

float circum();

};

void circle ::calc()

{

ar=3.14*r*r;

ci=2*3.14*r;

}

Page 16: Karan Comp Programs for cbse class 12

float circle ::area()

{

return(ar);

}

float circle ::circum()

{

return(ci);

}

void main()

{

clrscr();

circle c;

int radius;

cout<<"Enter radius"<<endl;

cin>>radius;

c.input(radius);

c.calc();

cout<<"Area="<<c.area();

cout<<"Circumference="<<c.circum();

getch();

}

Output-

Enter radius

1

Area=3.14

Page 17: Karan Comp Programs for cbse class 12

Circumference=6.28

8.Write a program class employee with subclass manager.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class employee

{

public:

int empno;

char name[25];

char add[50];

char dept[25];

void input()

{

cout<<"Enter employee no"<<endl;

cin>>empno;

cout<<"Enter employee name"<<endl;

gets(name);

cout<<"Enter employee address"<<endl;

gets(add);

cout<<"Enter employee department"<<endl;

gets(dept);

}

void disp()

{

Page 18: Karan Comp Programs for cbse class 12

cout<<"Employee number-"<<empno<<endl;

cout<<"Employee name-";

puts(name);

cout<<"Employee address-";

puts(add);

cout<<"Employee department-";

puts(dept);

}

}emp;

class manager:public employee

{

public:

int no;

void ino()

{

cout<<"Enter no of employees"<<endl;

cin>>no;

}

void ono()

{

cout<<"Number of employees-"<<no<<endl;

}

}man;

void main()

{

Page 19: Karan Comp Programs for cbse class 12

clrscr();

man.input();

man.ino();

man.disp();

man.ono();

getch();

}

Output-

Enter employee number

1

Enter employee name

Karan

Enter employee address

Bangalore

Enter employee department

R&D

Enter no of employees

10

Employee number-1

Employee name-Karan

Employee address-Bangalore

Employee department-R&D

Number of employees-10

9.Write a program to demonstrate addition of data members of a class under different objects.

Page 20: Karan Comp Programs for cbse class 12

#include<iostream.h>

#include<conio.h>

class distance

{

int feet,inches;

public:

void getdata(int f,int i)

{

feet=f;

inches=i;

}

void printit()

{

cout<<"Feet="<<feet<<endl;

cout<<"Inches="<<inches<<endl;

}

distance sum(distance d2);

};

distance distance ::sum(distance d2)

{

distance d3;

d3.feet=(feet+d2.feet)+(inches+d2.inches)/12;

d3.inches=(inches+d2.inches)%12;

return d3;

}

Page 21: Karan Comp Programs for cbse class 12

void main()

{

clrscr();

distance l1,l2,total;

l1.getdata(17,6);

l2.getdata(13,8);

total=l1.sum(l2);

cout<<endl<<"Length 1-"<<endl;

l1.printit();

cout<<endl<<"Length 2-"<<endl;

l2.printit();

cout<<endl<<"Total-"<<endl;

total.printit();

cout<<endl;

getch();

}

Output-

Length 1-

Feet=17

Inches=6

Length 2-

Feet=13

Inches=8

Total-

Feet=31

Page 22: Karan Comp Programs for cbse class 12

Inches=2

10.Write a c++ program to display fibbonaci series upto n terms.

#include<iostream.h>

#include<conio.h>

void fib(int,int,int);

void main()

{

clrscr();

int n;

int a=0,b=1;

cout<<"Enter n"<<endl;

cin>>n;

cout<<a<<","<<b;

fib(n,a,b);

getch();

}

void fib(int x,int a,int b)

{

int c=0;

static int d=2;

if(d<x)

{

c=a+b;

a=b;

b=c;

Page 23: Karan Comp Programs for cbse class 12

cout<<","<<c;

d++;

fib(x,a,b);

}

}

Output-

Enter n

5

0,1,1,2,3

11.Write a program to accept two numbers and display their hcf.

#include<iostream.h>

#include<conio.h>

int a,b;

int hcf(int x)

{

if(a%x==0 && b%x==0)

return x;

else

return(hcf(x-1));

}

void main()

{

clrscr();

cout<<"Enter a and b"<<endl;

cin>>a>>b;

Page 24: Karan Comp Programs for cbse class 12

cout<<”Hcf=”<<hcf(a);

getch();

}

Output-

Enter a and b

8

12

Hcf=4

12.Write a program to find hcf and gcd of two numbers.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a,b;

int hcf,lcm;

cout<<"Enter 2 numbers"<<endl;

cin>>a>>b;

for(int i=1;i<=a;i++)

{

if(a%i==0 && b%i==0)

hcf=i;

}

for(int j=a;j<=a*b;j++)

{

Page 25: Karan Comp Programs for cbse class 12

if(j%a==0 && j%b==0)

{

lcm=j;

break;

}

}

cout<<"HCF="<<hcf<<endl;

cout<<"LCM="<<lcm;

getch();

}

Output-

Enter 2 numbers

8

12

HCF=4

LCM=24

13.Write a program to accept a string and display number of characters in it.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

char str[100];

cout<<"Enter string"<<endl;

cin.getline(str,100);

Page 26: Karan Comp Programs for cbse class 12

for(int i=0;i<100;i++)

{

if(str[i]=='\0')

break;

}

cout<<"No of characters="<<i<<endl;

getch();

}

Output-

Enter string

Computers

No of characters=9

14.Write a program to display following pattern-

1

2 3

4 5 6

7 8 9 10

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int s=0;

for(int i=1;i<=5;i++)

Page 27: Karan Comp Programs for cbse class 12

{

for(int k=4;k>=i;k--)

cout<<" ";

for(int j=1;j<i;j++)

{

s++;

cout<<s<<" ";

}

cout<<endl;

}

getch();

}

Output-

1

2 3

4 5 6

7 8 9 10

15.Write a program class person with subclass spouse.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class person

{

public:

char name[25];

Page 28: Karan Comp Programs for cbse class 12

int phone;

person()

{

phone=1234;

}

void get()

{

cout<<"Enter person name"<<endl;

gets(name);

cout<<"Enter phone no"<<endl;

cin>>phone;

}

void disp()

{

cout<<"Person name-";

puts(name);

cout<<"Phone no-"<<phone<<endl;

}

};

class spouse:public person

{

public:

char spousename[25];

void getsp()

{

Page 29: Karan Comp Programs for cbse class 12

cout<<"Enter spouse name"<<endl;

gets(spousename);

}

void disps()

{

cout<<"Person name-";

puts(name);

cout<<"Phone number-"<<phone<<endl;

cout<<"Spouse name-";

puts(spousename);

}

}s;

void main()

{

clrscr();

cout<<"Enter data for person"<<endl;

s.get();

s.disp();

cout<<"Enter spouse data"<<endl;

s.getsp();

s.disps();

getch();

}

Output-

Page 30: Karan Comp Programs for cbse class 12

Enter data of person

Enter person name

Amit

Enter phone no

1234

Person name-Amit

Phone no-1234

Enter spouse data

Enter spouse name

Aditi

Person name-Amit

Phone no-1234

Spouse name-Aditi

16.Write a program to accept a number and display prime numbers up to it.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n,c;

cout<<"Enter n greater than 5"<<endl;

cin>>n;

while(n<=5)

{

cout<<"Greater than 5"<<endl;

Page 31: Karan Comp Programs for cbse class 12

cin>>n;

}

for(int i=2;i<=n;i++)

{

c=0;

for(int j=1;j<=i;j++)

{

if(i%j==0)

c++;

}

if(c==2)

cout<<i<<endl;

}

getch();

}

Output-

Enter n greater than 5

6

2

3

5

17.Write a program to display following series-

s=1 + 1/2! + 1/3!...+1/n!

#include<iostream.h>

#include<conio.h>

Page 32: Karan Comp Programs for cbse class 12

void main()

{

clrscr();

int n;

double s=1,t=1;

cout<<"Enter n"<<endl;

cin>>n;

for(int i=1;i<=n;i++)

{

t*=i;

s+=1/t;

}

cout<<"Sum="<<s;

getch();

}

Output-

Enter n

2

Sum=2.5

18.Write a program to demonstrate how the compiler handles functions with same name but different parameters.

#include<iostream.h>

#include<conio.h>

double si(int p,int t,int r=3);

void main()

Page 33: Karan Comp Programs for cbse class 12

{

clrscr();

int p=1000,r=4,t=2;

cout<<endl<<"Simple interest="<<si(p,t,r)<<endl;

cout<<"Default simple interest="<<si(p,t)<<endl;

getch();

}

double si(int p,int t,int r)

{

return((p*t*r)/100);

}

Output-

Simple interest=80

Default simple interest=60

19.Write a program to accept student details and display them.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class student

{

public:

char name[25];

int age;

void readdata()

{

Page 34: Karan Comp Programs for cbse class 12

cout<<"Enter name"<<endl;

gets(name);

cout<<"Enter age"<<endl;

cin>>age;

}

void display()

{

cout<<"Name of student-";

puts(name);

cout<<"Age-"<<age<<endl;

}

};

class primarystudent:public student

{

public:

char activity[25];

int nohrs;

void readprimary()

{

cout<<"Enter activity"<<endl;

gets(activity);

cout<<"Enter number of hours spent in activity"<<endl;

cin>>nohrs;

}

void displayprimary()

Page 35: Karan Comp Programs for cbse class 12

{

cout<<"Activity-";

puts(activity);

cout<<"Number of hours spent-"<<nohrs<<endl;

}

}prime;

class secondarystudent:public student

{

public:

char combo[25];

void readsecondary()

{

cout<<"Enter combination of student"<<endl;

gets(combo);

}

void displaysecondary()

{

cout<<"Combination of student-";

puts(combo);

}

}sec;

class equipment:public secondarystudent

{

public:

char namee[25];

Page 36: Karan Comp Programs for cbse class 12

int roll;

void readequip()

{

cout<<"Enter name of equipment"<<endl;

gets(namee);

cout<<"Enter roll no of equipment"<<endl;

cin>>roll;

}

void displayequip()

{

cout<<"Name of equipment-";

puts(namee);

cout<<"Roll no-"<<roll<<endl;

}

}equip;

class book:public student

{

public:

char bookname[25];

int nopgs;

char author[25];

void readb()

{

cout<<"Enter name of book"<<endl;

gets(bookname);

Page 37: Karan Comp Programs for cbse class 12

cout<<"Enter number of pages"<<endl;

cin>>nopgs;

cout<<"Enter author name"<<endl;

gets(author);

}

void displayb()

{

cout<<"Name of book-";

puts(bookname);

cout<<"Number of pages-"<<nopgs<<endl;

cout<<"Author-";

puts(author);

}

}bo;

void main()

{

clrscr();

int ch;

cout<<"Enter book details"<<endl;

bo.readb();

cout<<"Enter 1 if primary student, 2 if secondary"<<endl;

cin>>ch;

if(ch==1)

{

cout<<"Enter basic details"<<endl;

Page 38: Karan Comp Programs for cbse class 12

prime.readdata();

cout<<"Enter specific details"<<endl;

prime.readprimary();

cout<<"Book details-"<<endl;

bo.displayb();

cout<<"Student details-"<<endl;

prime.display();

prime.displayprimary();

}

else

{

cout<<"Enter basic details"<<endl;

equip.readdata();

cout<<"Enter specific details"<<endl;

equip.readsecondary();

cout<<"Enter equipment details"<<endl;

equip.readequip();

cout<<"Book details-"<<endl;

bo.displayb();

cout<<"Student details-"<<endl;

equip.display();

equip.displaysecondary();

cout<<"Equipment details"<<endl;

equip.displayequip();

}

Page 39: Karan Comp Programs for cbse class 12

getch();

}

Output-

Enter book details

Enter name of book

Computer Science

Enter number of pages

200

Enter author name

Sumita Arora

Enter 1 if primary student,2 if secondary

1

Enter basic details

Enter name

Amit

Enter age

9

Enter specific details

Enter activity

Football

Enter number of hours spent in activity

3

Book details-

Name of book-Computer Science

Number of pages-200

Page 40: Karan Comp Programs for cbse class 12

Author-Sumita Arora

Student details-

Name of student-Amit

Age-9

Activity-Football

Number of hours spent-3

20.Write a program to accept two numbers and swap them.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a,b;

cout<<"Enter numbers to be swapped"<<endl;

cin>>a>>b;

cout<<"Original numbers"<<a<<","<<b<<endl;

a=a+b;

b=a-b;

a=a-b;

cout<<"Swapped numbers="<<a<<","<<b<<endl;

getch();

}

Output-

Enter numbers to be swapped

2

Page 41: Karan Comp Programs for cbse class 12

3

Original numbers-2,3

Swapped numbers-3,2

21.Write a program that accepts date, month and year, and checks if it is a valid date.

#include<iostream.h>

#include<conio.h>

#include<ctype.h>

void date(int,int,int);

void main()

{

clrscr();

int day,month,year;

char c;

cout<<"Enter day"<<endl;

cin>>day;

cout<<"Enter month"<<endl;

cin>>month;

cout<<"Enter year"<<endl;

cin>>year;

date(day,month,year);

getch();

Page 42: Karan Comp Programs for cbse class 12

}

void date(int d,int m,int y)

{

int val=1;

int type=0;

int thirty[]={4,6,9,11,0,0,0};

int thirone[]={1,3,5,7,8,10,12};

int feb[]={2,0,0,0,0,0,0};

int t1=0,t2=0,t3=0;

if(m>12)

val=0;

if(d==0)

val=0;

for(int i=0;i<7;i++)

{

if(thirty[i]==m)

t1=1;

if(thirone[i]==m)

t2=1;

if(feb[i]==m)

t3=1;

}

if(t1==1)

{

if(d>30)

Page 43: Karan Comp Programs for cbse class 12

val=0;

}

else if(t2==1)

{

if(d>31)

val=0;

}

else

{

if(d>28 && y%4!=0)

val=0;

else if(d>29 && y%4==0)

val=0;

}

if(val==1)

cout<<"Date is valid"<<endl;

else

cout<<"Date is invalid"<<endl;

if(y%4==0)

cout<<"Leap year"<<endl;

else

cout<<"Not leap year"<<endl;

}

Output-

Enter day

Page 44: Karan Comp Programs for cbse class 12

15

Enter month

6

Enter year

2014

Date is valid

Not a leap year

22.Write a program to demonstrate function overloading.

#include<iostream.h>

#include<conio.h>

#include<math.h>

int vol(int);

int vol(int,int);

int vol(int,int,int);

void main()

{

clrscr();

int s=2,r=3,h=4;

cout<<"Volume of cube="<<vol(s)<<endl;

cout<<"Volume of cylinder="<<vol(r,h)<<endl;

cout<<"Volume of cuboid="<<vol(s,r,h)<<endl;

getch();

}

int vol(int s)

{

Page 45: Karan Comp Programs for cbse class 12

return(s*s*s);

}

int vol(int r,int h)

{

return(3.14*r*r*h);

}

int vol(int s,int r,int h)

{

return(s*r*h);

}

Output-

Volume of cube=8

Volume of cylinder=113

Volume of cuboid=24

23.Write a program to accept worker details and display them along with monthly salary.

#include<iostream.h>

#include<conio.h>

class worker

{

char wname[25];

float hrwrk,wgrate;

float totalwage;

float calcwage();

public:

Page 46: Karan Comp Programs for cbse class 12

void in_data();

void out_data();

}wor;

float worker ::calcwage()

{

return(hrwrk*wgrate);

}

void worker ::in_data()

{

cout<<"Enter name of worker"<<endl;

cin>>wname;

cout<<"Enter hours worked"<<endl;

cin>>hrwrk;

cout<<"Enter hourly wage"<<endl;

cin>>wgrate;

totalwage=calcwage();

}

void worker ::out_data()

{

cout<<"Name of worker-"<<wname<<endl;

cout<<"Hours worked-"<<hrwrk<<endl;

cout<<"Hourly wage-"<<wgrate<<endl;

cout<<"Total wage-"<<totalwage<<endl;

}

void main()

Page 47: Karan Comp Programs for cbse class 12

{

clrscr();

wor.in_data();

wor.out_data();

getch();

}

Output-

Enter name of worker

Karan

Enter hours worked

24

Enter hourly wage

0

Name of worker-Karan

Hours worked-24

Hourly wage-0

Total wage-0

24.Write a program that accepts two numbers, sets the smaller as 0 and displays them.

#include<iostream.h>

#include<conio.h>

void zerosmall(int &,int &);

void main()

{

clrscr();

Page 48: Karan Comp Programs for cbse class 12

int a,b;

cout<<"Enter 2 numbers"<<endl;

cin>>a>>b;

zerosmall(a,b);

cout<<"The 2 numbers are-"<<a<<","<<b<<endl;

getch();

}

void zerosmall(int &a,int &b)

{

if(a<b)

a=0;

else

if(a>b)

b=0;

else

{}

}

Output-

Enter 2 numbers

2

3

The 2 numbers are-0,3

25.RECORD:Write a function program with sum() as function with 2 arguments-

double x and int n.The function should return a value of type

double and find sum of the following series-

Page 49: Karan Comp Programs for cbse class 12

1 + x/1! + x^3/2! +x^5/3!...+x^2n-1/n!

#include<iostream.h>

#include<conio.h>

double sum(double,int);

void main()

{

clrscr();

double x;

int n;

cout<<"Enter x and n"<<endl;

cin>>x>>n;

cout<<"Sum of series="<<sum(x,n)<<endl;

getch();

}

double sum(double x,int n)

{

double sum=1,temp=1/x;

for(int i=1;i<=n;i++)

{

temp*=x*x/i;

sum+=temp;

}

return(sum);

}

Output-

Page 50: Karan Comp Programs for cbse class 12

Enter x and n

2

3

Sum of series=12.333333

26.RECORD:Declare a structure to represent a complex number having real

and imaginary parts. Write functions to add, subtract, multiply

and divide the two complex numbers.

#include<iostream.h>

#include<conio.h>

struct complex

{

double r,i;

}n1,n2,n3;

void add()

{

n3.r=n1.r+n2.r;

n3.i=n1.i+n2.i;

}

void sub()

{

n3.r=n1.r-n2.r;

n3.i=n1.i-n2.i;

}

void mul()

{

Page 51: Karan Comp Programs for cbse class 12

n3.r=(n1.r*n2.r)-(n1.i*n2.i);

n3.i=(n1.r*n2.i)+(n1.i*n2.r);

}

void div()

{

n2.i*=-1;

n3.r=(n1.r*n2.r)-(n1.i*n2.i);

n3.i=(n1.r*n2.i)+(n1.i*n2.r);

double den=(n2.r*n2.r)+(n2.i*n2.i);

n3.r/=den;

n3.i/=den;

}

void main()

{

clrscr();

char ch;

int real,imag;

cout<<"Enter real and complex part of first number"<<endl;

cin>>n1.r>>n1.i;

cout<<"Enter real and complex part of second number"<<endl;

cin>>n2.r>>n2.i;

cout<<"Enter + for addition, - for subtraction, * for multiplication, / for division"<<endl;

cin>>ch;

switch(ch)

{

Page 52: Karan Comp Programs for cbse class 12

case '+':cout<<"Addition"<<endl;

add();

break;

case '-':cout<<"Subtraction"<<endl;

sub();

break;

case '*':cout<<"Multiplication"<<endl;

mul();

break;

case '/':cout<<"Division"<<endl;

div();

break;

default:cout<<"Invalid input"<<endl;

n3.r=0,n3.i=0;

}

if(n3.i>0)

cout<<"Number="<<n3.r<<"+"<<n3.i<<"i";

else

cout<<"Number="<<n3.r<<n3.i<<"i";

getch();

}

Output-

Enter real and complex part of first number

1

2

Page 53: Karan Comp Programs for cbse class 12

Enter real and imaginary part for second number

3

4

Enter + for addition, - for subtraction, * for multiplication, / for division

+

Addition

Number=4+6i

27.RECORD:From a 2D array A[4][4] write a program to prepare

a 1D array B[16] that will have all the elements of A

if they are stored in column major form.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int A[4][4],B[16];

for(int i=0;i<4;i++)

{

cout<<"Enter elements of row no."<<i+1<<endl;

for(int j=0;j<4;j++)

{

cin>>A[i][j];

}

}

int k=0;

Page 54: Karan Comp Programs for cbse class 12

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

{

for(int j=0;j<4;j++)

{

B[k]=A[j][i];

k++;

}

}

cout<<"Single dimension array is-"<<endl;

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

cout<<B[i]<<",";

getch();

}

Output-

Enter elements of first row

1

2

3

4

Enter elements of second row

5

6

7

8

Enter elements of third row

Page 55: Karan Comp Programs for cbse class 12

9

10

11

12

Enter elements of fourth row

13

14

15

16

Array is-1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16

28.RECORD:Write two overloaded functions to find GCD of two numbers amongst

which one function would be normal, other recursive.

#include<iostream.h>

#include<conio.h>

int gcd(int a,int b);

int gcd(int n);

int c,d;

void main()

{

clrscr();

int a,b;

int ch;

cout<<"Enter the two numbers"<<endl;

cin>>a>>b;

Page 56: Karan Comp Programs for cbse class 12

cout<<"Enter 1 for normal function, 2 for recursive function"<<endl;

cin>>ch;

if(ch==1)

cout<<"GCD="<<gcd(a,b);

else if(ch==2)

{

c=a,d=b;

cout<<"GCD="<<gcd(a);

}

else

cout<<"Invalid input"<<endl;

getch();

}

int gcd(int a,int b)

{

int g=0;

for(int i=1;i<=a;i++)

{

if(a%i==0 && b%i==0)

g=i;

}

return(g);

}

int gcd(int n)

{

Page 57: Karan Comp Programs for cbse class 12

if(n==1)

return 1;

else if(c%n==0 && d%n==0)

return n;

else

return(gcd(n-1));

}

Output-

Enter the 2 numbers

8

12

Enter 1 for normal function, 2 for recursive

1

GCD=4

29.RECORD:Write an overloaded function power() having two versions. The first

version takes double n and int p and returns a double value. The

second takes int n and int p and returns int value. Use a default value

2 for p in case p is omitted in function call.

#include<iostream.h>

#include<conio.h>

#include<math.h>

double power(double,int p=2);

int power(int,int p=2);

void main()

{

Page 58: Karan Comp Programs for cbse class 12

clrscr();

double n;

int p;

int c,ch;

cout<<"Enter n"<<endl;

cin>>n;

cout<<"Enter 1 if you wish to enter p"<<endl;

cin>>c;

if(c==1)

{

cout<<"Enter p"<<endl;

cin>>p;

}

cout<<"Enter 1 for n^p, 2 for n*n*n....p times"<<endl;

cin>>ch;

if(c==1 && ch==1)

cout<<"Result="<<power(n,p);

else if(c!=1 && ch==1)

cout<<"Result="<<power(n);

else if(c==1 && ch==2)

{

n=(int)n;

cout<<"Result="<<power(n,p);

}

else if(c!=1 && ch==2)

Page 59: Karan Comp Programs for cbse class 12

{

n=(int)n;

cout<<"Result="<<power(n);

}

else

cout<<"Invalid choice"<<endl;

getch();

}

double power(double n,int p)

{

double temp=pow(n,p);

return temp;

}

int power(int n,int p)

{

int temp=1;

for(int i=1;i<=p;i++)

{

temp*=n;

}

return temp;

}

Output-

Enter n

2

Page 60: Karan Comp Programs for cbse class 12

Enter 1 if you wish to enter p

0

Enter 1 for n^p, 2 for n*n*n...p times

2

Result=4

30.RECORD:Write two versions of an overloaded function sum(). The first version

accepts and int array and returns sum of all elements. The second

version accepts an int array and a character. It returns sum of even

elements if character is 'E', sum of odd elements if character is 'O',

and 0 if any other character.

#include<iostream.h>

#include<conio.h>

int sum(int ar[5]);

int sum(int ar[5],char);

void main()

{

clrscr();

int ar[5];

char ch;

cout<<"Enter 5 elements for the array"<<endl;

for(int i=0;i<5;i++)

cin>>ar[i];

cout<<"Enter 1 for sum of even or odd elements"<<endl;

cin>>ch;

Page 61: Karan Comp Programs for cbse class 12

if(ch=='1')

{

cout<<"Enter E for even, O for odd"<<endl;

cin>>ch;

cout<<"Sum="<<sum(ar,ch);

}

else

cout<<"Sum="<<sum(ar);

getch();

}

int sum(int ar[5])

{

int s=0;

for(int i=0;i<5;i++)

{

s+=ar[i];

}

return s;

}

int sum(int ar[5],char ch)

{

int e=0,o=0;

for(int i=0;i<5;i++)

{

if(ar[i]%2==0)

Page 62: Karan Comp Programs for cbse class 12

e+=ar[i];

else

o+=ar[i];

}

if(ch=='E')

return e;

else if(ch=='O')

return o;

else

return 0;

}

Output-

Enter 5 elements for the array

1

2

3

4

5

Enter 1 for sum of even or odd elements

0

Sum=15

31.RECORD:Write a program to keep count of objects created using static members.

#include<iostream.h>

#include<conio.h>

Page 63: Karan Comp Programs for cbse class 12

class counter

{

static int c;

public:

counter()

{

c++;

}

void display()

{

cout<<"Number of objects created="<<c<<endl;

}

};

int counter ::c=0;

void main()

{

clrscr();

char ch;

cout<<"Enter 1,2,3 to create 1,2,3 objects respectively"<<endl;

cin>>ch;

if(ch=='1')

{

counter A;

cout<<"Object called A created"<<endl;

A.display();

Page 64: Karan Comp Programs for cbse class 12

}

else if(ch=='2')

{

counter B;

counter C;

cout<<"Objects called B and C created"<<endl;

C.display();

}

else if(ch=='3')

{

counter D;

counter E;

counter F;

cout<<"Objects called D,E,F created"<<endl;

F.display();

}

else

cout<<"Invalid input"<<endl;

getch();

}

Output-

Enter 1,2,3 to create 1,2,3 objects respectively

1

Object called A created

Number of objects created=1

Page 65: Karan Comp Programs for cbse class 12

32.RECORD:Write a program to perform various operations on a string class without

using language supported biult-in string functions. Operations are read

a string, display, reverse, copy to empty, concatenate.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class string

{

char str[25],str2[25],r[25],cop[25],con[50];

public:

read();

disp();

rev();

copy();

concat();

}s;

string ::read()

{

cout<<"Enter string"<<endl;

gets(str);

}

string ::disp()

{

cout<<"String=";

Page 66: Karan Comp Programs for cbse class 12

puts(str);

cout<<endl;

}

string ::rev()

{

for(int i=0;str[i]!='\0';i++);

for(int k=0,j=i-1;j>=0;j--,k++)

{

r[k]=str[j];

}

cout<<"Reversed string=";

puts(r);

}

string ::copy()

{

for(int i=0;i<25;i++)

cop[i]=str[i];

cout<<"Copied string=";

puts(cop);

cout<<endl;

}

string ::concat()

{

cout<<"Enter second string"<<endl;

gets(str2);

Page 67: Karan Comp Programs for cbse class 12

for(int i=0;str[i]!='\0';i++);

for(int j=0;str2[j]!='\0';j++);

for(int k=0;k<i;k++)

con[k]=str[k];

for(int l=0;l<j;l++)

con[l+i]=str[l];

cout<<"Concatenated strings=";

puts(cop);

}

void main()

{

clrscr();

s.read();

s.disp();

s.rev();

s.copy();

s.concat();

getch();

}

Output-

Enter string

Computer

String=Computer

Reversed string=retupmoC

Copied string=Computer

Page 68: Karan Comp Programs for cbse class 12

Enter second string

Science

Concatenated strings=ComputerScience

33.RECORD:Define a class Applicant that accepts admission no, name, aggregate

marks, calculates grade and displays all the details.

#include<iostream.h>

#include<conio.h>

class Applicant

{

long int ANo;

char Name[25];

float Agg;

char Grade;

void GradeMe()

{

if(Agg>=80)

Grade='A';

else if(Agg<80 && Agg>=65)

Grade='B';

else if(Agg<65 && Agg>=50)

Grade='C';

else

Grade='D';

}

public:

Page 69: Karan Comp Programs for cbse class 12

void ENTER()

{

cout<<"Enter Admission number"<<endl;

cin>>ANo;

cout<<"Enter name"<<endl;

cin>>Name;

cout<<"Enter Aggregate marks"<<endl;

cin>>Agg;

GradeMe();

}

void RESULT()

{

cout<<"Admission number="<<ANo<<endl;

cout<<"Name="<<Name<<endl;

cout<<"Aggregate marks="<<Agg<<endl;

cout<<"Grade="<<Grade<<endl;

}

}A;

void main()

{

clrscr();

A.ENTER();

A.RESULT();

getch();

}

Page 70: Karan Comp Programs for cbse class 12

Output-

Enter Admission number

1

Enter name

Karan

Enter Aggregate marks

100

Admission number=1

Name=Karan

Aggregate marks=100

Grade=A

34.RECORD:Define a class ticbooth which counts number of people who pass by and

whether they buy a ticket or not, and displays all the details.

#include<iostream.h>

#include<conio.h>

class ticbooth

{

int totp;

double totm;

public:

ticbooth()

{

totp=0,totm=0;

}

Page 71: Karan Comp Programs for cbse class 12

void incp()

{

totp++;

}

void incm()

{

totm=totm+2.5;

}

void display()

{

cout<<"Number of people who visited="<<totp<<endl;

cout<<"Amount collected="<<totm<<endl;

}

void ticket()

{

cout<<"Tickets sold="<<totm/2.5<<endl;

}

}t;

void main()

{

clrscr();

cout<<"Ticket booth"<<endl;

char ch='0',buy;

while(ch=='0')

{

Page 72: Karan Comp Programs for cbse class 12

cout<<"A person passes by"<<endl;

cout<<"Enter 1 if he buys a ticket"<<endl;

cin>>buy;

if(buy=='1')

{

t.incp();

t.incm();

}

else

t.incp();

cout<<"Enter 0 to continue"<<endl;

cin>>ch;

}

t.display();

t.ticket();

getch();

}

Output-

Ticket booth

A person passes by

Enter 1 if he buys a ticket

1

Enter 0 to continue

0

Page 73: Karan Comp Programs for cbse class 12

A person passes by

Enter 1 if he buys a ticket

0

Enter 0 to continue

1

Number of people who visited=2

Amount collected=2.5

Tickets sold=1

35.RECORD:Define a class TravelPlan, use constructor to initialise plan code,

place, number of travellers and number of buses. Allow user to enter the

values, calculate number of buses needed and display the information.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

class TravelPlan

{

long int PlanCode;

char Place[25];

int Number_of_travellers;

int Number_of_buses;

public:

TravelPlan()

{

PlanCode=1001;

Page 74: Karan Comp Programs for cbse class 12

strcpy(Place,"Agra");

Number_of_travellers=5;

Number_of_buses=1;

}

void NewPlan()

{

cout<<"Enter plan code"<<endl;

cin>>PlanCode;

cout<<"Enter place"<<endl;

gets(Place);

cout<<"Enter number of travellers"<<endl;

cin>>Number_of_travellers;

if(Number_of_travellers<20)

Number_of_buses=1;

else if(Number_of_travellers>=20 && Number_of_travellers<40)

Number_of_buses=2;

else

Number_of_buses=3;

}

void ShowPlan()

{

cout<<"Plan code="<<PlanCode<<endl;

cout<<"Place=";

puts(Place);

cout<<"Number of travellers="<<Number_of_travellers<<endl;

Page 75: Karan Comp Programs for cbse class 12

cout<<"Number of buses="<<Number_of_buses<<endl;

}

}tp;

void main()

{

clrscr();

tp.NewPlan();

tp.ShowPlan();

getch();

}

Output-

Enter plan code

1

Enter place

Bangalore

Enter number of travellers

10

Plan code=1

Place=Bangalore

Number of travellers=10

Numbers of buses=1

36.RECORD:Define a class Serial with information like serial code, title, duration

and no of episodes. Initialise using constructors and member functions

and then display.

#include<iostream.h>

Page 76: Karan Comp Programs for cbse class 12

#include<conio.h>

#include<stdio.h>

class Serial

{

int Serialcode;

char Title[20];

float Duration;

int Noofepisodes;

public:

Serial()

{

Duration=30;

Noofepisodes=10;

}

void Newserial()

{

cout<<"Enter serial code"<<endl;

cin>>Serialcode;

cout<<"Enter title"<<endl;

gets(Title);

}

void Otherentries(float d,int n)

{

Duration=d;

Noofepisodes=n;

Page 77: Karan Comp Programs for cbse class 12

}

Dispdata()

{

cout<<"Serial code="<<Serialcode<<endl;

cout<<"Title=";

puts(Title);

cout<<"Duration="<<Duration<<endl;

cout<<"No of episodes="<<Noofepisodes<<endl;

}

}s;

void main()

{

clrscr();

float d;

int n;

s.Newserial();

cout<<"Enter duration and no of episodes"<<endl;

cin>>d>>n;

s.Otherentries(d,n);

s.Dispdata();

getch();

}

Output-

Enter serial code

1

Page 78: Karan Comp Programs for cbse class 12

Enter title

Tom and Jerry

Enter duration and no of episodes

30

100

Serial code=1

Title=Tom and Jerry

Duration=30

No of episodes=100

37.RECORD:Define a class Clothing to accept code, type, size and material, calculate

price and display.

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class Clothing

{

char Code[25],Type[25];

int Size;

char Material[25];

float Price;

Calc_Price()

{

if(!strcmp(Material,"COTTON"))

Page 79: Karan Comp Programs for cbse class 12

{

if(!strcmp(Type,"TROUSER"))

Price=1500;

else if(!strcmp(Type,"SHIRT"))

Price=1200;

else

{}

}

else

{

if(!strcmp(Type,"TROUSER"))

Price=1500-1500*0.25;

else if(!strcmp(Type,"SHIRT"))

Price=1200-1200*0.25;

else

{}

}

}

public:

Clothing()

{

strcpy(Code,"NOT ASSIGNED");

strcpy(Type,"NOT ASSIGNED");

strcpy(Material,"NOT ASSIGNED");

Size=0;

Page 80: Karan Comp Programs for cbse class 12

Price=0;

}

void Enter()

{

cout<<"Enter code, type, size, material"<<endl;

gets(Code);

gets(Type);

cin>>Size;

gets(Material);

Calc_Price();

}

void Show()

{

cout<<"Code=";

puts(Code);

cout<<"Type=";

puts(Type);

cout<<"Size="<<Size<<endl;

cout<<"Material=";

puts(Material);

cout<<"Price="<<Price<<endl;

}

}c;

void main()

{

Page 81: Karan Comp Programs for cbse class 12

clrscr();

c.Enter();

c.Show();

getch();

}

Output-

Enter code, type, size, material

1

SHIRT

42

COTTON

Code=1

Type=SHIRTSize=42

Material=COTTON

Price=1200

38.RECORD:Write a function to accept time of 2 clocks in hours, minutes, seconds

and output time since last struck 12 in seconds and difference between

the two in seconds.

#include<iostream.h>

#include<conio.h>

#include<math.h>

void time(int h1,int m1,int s1,int h2,int m2,int s2);

void main()

Page 82: Karan Comp Programs for cbse class 12

{

clrscr();

int h1,m1,s1,h2,m2,s2;

cout<<"Enter first clock's time in hours, minutes,seconds"<<endl;

cin>>h1>>m1>>s1;

cout<<"Enter second clock's time in hours, minutes, seconds"<<endl;

cin>>h2>>m2>>s2;

time(h1,m1,s1,h2,m2,s2);

getch();

}

void time(int h1,int m1,int s1,int h2,int m2,int s2)

{

int t1=h1*3600+m1*60+s1;

int t2=h2*3600+m2*60+s2;

cout<<"Seconds since first clock struck 12="<<t1<<endl;

cout<<"Seconds since second clock struck 12="<<t2<<endl;

int t3=(fabs)(t1-t2);

cout<<"Difference between 2 clocks in seconds="<<t3<<endl;

}

Output-

Enter first clock's time in hours, minutes, seconds

1

10

20

Enter second clocks' time in hours, minutes, seconds

Page 83: Karan Comp Programs for cbse class 12

3

20

4

Seconds since first clock struck 12=4220

Seconds since second clock struck 12=12004

Difference between two clocks in seconds=7784