xii computer programs
TRANSCRIPT
//Fibonacci Series#include<iostream.h>#include<conio.h>void prime (int a){ int i, flag;
if ((a= =0)||(a= =1))cout<<"Neither prime nor composite.";
else{ flag=0;
for (i=2; i<a/2; i++)if (a%i= =0){ flag=1;
break;}if (flag)
cout<<"It is a composite number.";else if (!flag)
cout<<"It is a prime number.";}
}void fibonacci (int n){ int i, a=0, b=1, c=0;
if (n= =0)cout<<"\nInvalid input.";
else if (n= =1){ cout<<"\nThe first element of the Fibonacci series is:";
cout<<"\n\t"<<a<<"\t\t";prime (a);
}else if (n>1){ cout<<"\nThe first "<<n<<" elements of the Fibonacci series are:";
cout<<"\n\t"<<a<<"\t\t";prime (a);cout<<"\n\t"<<b<<"\t\t";prime (b);for (i=3; i<=n; i++){ c=a+b;
cout<<"\n\t"<<c<<"\t\t";prime (c);a=b;b=c; }
}}void main ( ){ clrscr( );
int x=0;char ch;do{ cout<<"\n\n\t\t\tFIBONACCI SERIES";
cout<<"\n\t\t\t****************";cout<<"\n\nEnter the number of elements: ";cin>>x;cout<<"\n";fibonacci(x);cout<<"\nDo you want to continue? (Y/N): ";cin>>ch;
}while ((ch=='Y')||(ch=='y'));getch ( );
}
Sample Output :-
FIBONACCI SERIES*****************
Enter the number of elements: 10
The first 10 elements of the Fibonacci series are:0 Neither prime nor composite.1 Neither prime nor composite.1 Neither prime nor composite.2 It is a prime number.3 It is a prime number.5 It is a prime number.8 It is a composite number.13 It is a prime number.21 It is a composite number.34 It is a composite number.
Do you want to continue? (Y/N): n
//Electricity Bill
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class bill
{
char name[20], city[20], street[30];
int units, houseno;
float charge;
public:
void input( )
{
cout<<"\nEnter the name of the customer: ";
gets(name);
cout<<"Enter the house number of the customer: ";
cin>>houseno;
cout<<"Enter the city of the customer: ";
gets(city);
cout<<"Enter the street of the customer: ";
gets(street);
cout<<"Enter the number of units consumed: ";
cin>>units;
calc( );
}
void calc( )
{
if (units<=200)
charge=units*2.5;
else if (units<=300)
charge=((units-200)*3.0+500);
else if (units<=400)
charge=((units-300)*3.5+800);
else
charge=((units-400)*4.0+1150);
}
void output( )
{
cout<<"\nName: "<<name;
cout<<"\nHouse number: "<<houseno;
cout<<"\nCity: "<<city;
cout<<"\nStreet: "<<street;
cout<<"\n\nTotal units: "<<units;
cout<<"\nTotal charges: "<<charge;
}
};
void main( )
{
clrscr( );
bill x[30];
int n, i;
cout<<"\n\t----------------ELECTRICITY BILL----------------\n";
cout<<"\nEnter the number of customers: ";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"\nEnter the details of customer "<<i+1<<"\n";
x[i].input( );
}
for(i=0; i<n; i++)
{
cout<<"\n\t\t\tELECTRICITY BILL";
cout<<"\n\t\t\t~~~~~~~~~~~~~~~~~\n";
cout<<"The bill for customer "<<i+1<<" is:\n";
x[i].output( );
}
getch( );
}
Sample Output :-
----------------ELECTRICITY BILL----------------
Enter the number of customers: 2
Enter the details of customer 1
Enter the name of the customer: Nitheesh PrabuEnter the house number of the customer: 88Enter the city of the customer: ChennaiEnter the street of the customer: Krishnamachari NagarEnter the number of units consumed: 200
Enter the details of customer 2
Enter the name of the customer: Krishna KumarEnter the house number of the customer: 55Enter the city of the customer: ChennaiEnter the street of the customer: Alapakkam RoadEnter the number of units consumed: 400
ELECTRICITY BILL ~~~~~~~~~~~~~~~~~The bill for customer 1 is:
Name: Nitheesh PrabuHouse number: 88City: ChennaiStreet: Krishnamachari Nagar
Total units: 200Total charges: 500
ELECTRICITY BILL ~~~~~~~~~~~~~~~~~The bill for customer 2 is:
Name: Krishna KumarHouse number: 89City: ChennaiStreet: Alapakkam Road
Total units: 400Total charges: 1150
//Telephone Bill
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class addr
{
int block;
char street[30], city[20];
public:
void input( )
{
cout<<"Enter the address:\n";
cout<<"Enter the block number: ";
cin>>block;
cout<<"Enter the street: ";
gets(street);
cout<<"Enter the city: ";
gets(city);
}
void output( )
{
cout<<"\nAddress: ";
cout<<block<<", "<<street<<", "<<city;
}
};
class telephonebill
{
char name[30];
addr address;
long phoneno;
int calls, rent;
float charge, surcharge, total, subtotal;
public:
telephonebill( )
{ rent=500; }
void input( )
{
cout<<"\nEnter the name: ";
gets(name);
cout<<"Enter the phone number: ";
cin>>phoneno;
address.input( );
cout<<"Enter the number of calls: ";
cin>>calls;
calcbill( );
}
void calcbill( )
{
charge=0.0;
if(calls<=150)
charge=0.0;
else if(calls<=300)
charge=(calls-150)*1.0;
else if(calls<=450)
charge=150+((calls-300)*1.5);
else
charge=375+((calls-450)*2.0);
surcharge=0.2*charge;
total=rent+surcharge+charge;
}
void output( )
{
cout<<"\nName: "<<name;
cout<<"\nPhone number: "<<phoneno;
cout<<"\nNumber of calls: "<<calls;
address.output( );
cout<<"\nBill: "<<total;
}
};
void main( )
{
clrscr( );
int n, i;
telephonebill t[20];
cout<<"\n\t----------------TELEPHONE BILL----------------\n";
cout<<"\nEnter the number of customers: ";
cin>>n;
for(i=0; i<n; i++)
{ cout<<"\nEnter the details of customer "<<i+1<<":";
t[i].input( );
}
for(i=0; i<n; i++)
{ cout<<"\n\t\t\tTELEPHONE BILL";
cout<<"\n\t\t\t****************";
cout<<"\nCustomer "<<i+1<<":";
t[i].output( );
}
getch( );
}
Sample Output :-
----------------TELEPHONE BILL----------------
Enter the number of customers: 2
Enter the details of customer 1:Enter the name: NaveenEnter the phone number: 23456789Enter the address:Enter the block number: 121Enter the street: Kesari NagarEnter the city: MumbaiEnter the number of calls: 400
Enter the details of customer 2:Enter the name: MuraliEnter the phone number: 22111550Enter the address:Enter the block number: 3Enter the street: M.K.Gandhi NagarEnter the city: HyderabadEnter the number of calls: 150
TELEPHONE BILL****************
Customer 1:Name: NaveenPhone number: 23456789 Number of calls: 400Address: 121, Kesari Nagar, Mumbai Bill: 860
TELEPHONE BILL****************
Customer 2:Name: MuraliPhone number: 22111550 Number of calls: 150Address:3, M.K.Gandhi Nagar, Hyderabad
Bill: 500//Interest Calculation#include<iostream.h>#include<math.h>#include<conio.h>float t;class interest{
float pr, r, n, a, i;public:
interest(float pr1, float r1){
pr=pr1; r=r1;n=t;i=(pr*r*n)/100;disp( );
}interest(float pr1, float r1, float t1){
pr=pr1; r=r1; n=t1;a=(pow((1+r/100),n))*pr;i=a-pr;disp( );
}void disp( ){
cout<<"\nCalculated interest is: "<<i;}
};void main( ){
clrscr( );float pr1, r1;int s;char ch;cout<<"\nEnter the principal amount: ";cin>>pr1;cout<<"Enter the rate: ";cin>>r1;cout<<"Enter the duration: ";cin>>t;do{
cout<<"\n1.Simple Interest\n2.Compound Interest\n";cout<<"\nEnter your choice: ";cin>>s;switch(s)
{case 1: { interest sint(pr1,r1);
break; }case 2: { interest cint(pr1,r1,t);
break; }default: { cout<<"\nInvalid choice";
break; }}
cout<<"\n\nDo you want to continue? (Y/N): ";cin>>ch;
}while((ch=='y')||(ch=='Y'));getch( );
}
Sample Output :-
Enter the principal amount: 5000Enter the rate: 5Enter the duration: 10
1.Simple Interest2.Compound Interest
Enter your choice: 1
Calculated interest is: 2500
Do you want to continue? (Y/N): y
1.Simple Interest2.Compound Interest
Enter your choice: 2
Calculated interest is: 3144.47
Do you want to continue? (Y/N): n
//Employee Details#include<iostream.h>#include<stdio.h>#include<conio.h>struct date{
int dd, mm, yy;};class person{
protected:char name[20];date dob;char eq[20];
public:void accept( ){
cout<<"\nEnter the name: ";gets(name);cout<<"Enter the date of birth (DD/MM/YYYY): ";cin>>dob.dd>>dob.mm>>dob.yy;cout<<"Enter the educational qualification: ";gets(eq);
}void output( ){
cout<<"\nName: "<<name;cout<<"\nDate of Birth: "<<dob.dd<<"/"<<dob.mm<<"/"<<dob.yy;cout<<"\nEducational Qualification: "<<eq;
}};class emp:public person{
protected:date doj;int ec;float sal;
public:void accept2( ){
accept( );cout<<"\nEnter the employee code: ";cin>>ec;cout<<"Enter the date of joining (DD/MM/YYYY): ";cin>>doj.dd>>doj.mm>>doj.yy;cout<<"Enter the salary: ";cin>>sal;
}void output2( ){
output( );cout<<"\nEmployee code: "<<ec;cout<<"\nDate of Joining: "<<doj.dd<<"/"<<doj.mm<<"/"<<doj.yy;cout<<"\nSalary: "<<sal;
}
};
class manager:public emp{
int no;char title[30];public:
void in( ){
accept2( );cout<<"\nEnter the number of subordinates: ";cin>>no;cout<<"Enter the designation: ";gets(title);
}void out( ){
output2( );cout<<"\nNumber of subordinates: "<<no;cout<<"\nDesignation: "<<title;
}};void main( ){
clrscr( );emp e;manager m;cout<<"\nEnter the employee details:\n";e.accept( );cout<<"\nEnter the manager details:\n";m.in( );cout<<"\n\n\t\tEMPLOYEE DETAILS";cout<<"\n\t\t****************\n";e.output( );cout<<"\n\n\t\tMANAGER DETAILS";cout<<"\n\t\t***************\n";m.out( );getch( );
}
Sample Output :- Enter the employee details:
Enter the name: KaishEnter the date of birth (DD/MM/YYYY): 15 04 1997Enter the educational qualification: B.Ed.
Enter the manager details:Enter the name: ManojEnter the date of birth (DD/MM/YYYY): 18 06 1977Enter the educational qualification: M.Sc.
Enter the employee code: 5Enter the date of joining (DD/MM/YYYY): 01 01 2005Enter the salary: 500000
Enter the number of subordinates: 10Enter the designation: Project Manager
EMPLOYEE DETAILS *******************
Name: KaishDate of Birth: 15/4/1997Educational Qualification: B.Ed.
MANAGER DETAILS ******************
Name: ManojDate of Birth: 18/6/1977Educational Qualification: M.Sc.Employee code: 5Date of Joining: 1/1/2005Salary: 500000Number of subordinates: 10Designation: Project Manager
//Student Mark Processing
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<conio.h>
struct student
{ char name[30];
float math, phy, eng, che, csc, rank, tot;
}temp;
void output (student s[ ], int n)
{ int i;
cout<<"\n\n\n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Name"<<setw(10)<<"Maths"<<setw(10)<<"Physics"
<<setw(10)<<"Chemistry"<<setw(8)<<"C.Sc."<<setw(8)<<"English"<<setw(7)<<"Total\n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
for(i=0; i<n; i++)
{ cout<<s[i].name<<setw(7)<<s[i].math<<setw(7)<<s[i].phy<<setw(10)<<s[i].che
<<setw(10)<<s[i].csc<<setw(7)<<s[i].eng<<setw(7)<<s[i].tot;
}
cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
void input (student s[ ], int n)
{ int i;
for(i=0; i<n; i++)
{ cout<<"\nEnter the details of student "<<i+1<<":\n";
cout<<"\nEnter the name: ";
gets(s[i].name);
cout<<"\nEnter the marks:\n";
cout<<"Maths: ";
cin>>s[i].math;
cout<<"Physics: ";
cin>>s[i].phy;
cout<<"Chemistry: ";
cin>>s[i].che;
cout<<"English: ";
cin>>s[i].eng;
cout<<"Computer Science: ";
cin>>s[i].csc;
s[i].tot=s[i].math+s[i].phy+s[i].che+s[i].eng+s[i].csc;
}
}
void main( )
{ clrscr( );
int x;
student s[20];
cout<<"\n\t\t\tSTUDENT MARK PROCESSING";
cout<<"\n\t\t\t***************************";
cout<<"\nEnter the number of students: ";
cin>>x;
input (s,x);
output (s,x);
getch( );
}
Sample Output :-
STUDENT MARK PROCESSING ****************************Enter the number of students: 1
Enter the details of student 1:
Enter the name: Nitheesh
Enter the marks:Maths: 100Physics: 100Chemistry: 100English: 100Computer Science: 100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Name Maths Physics Chemistry C.Sc. English Total~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Nitheesh 100 100 100 100 100 500~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~