Sie sind auf Seite 1von 34

1.

Banking Program

#include<iostream> #include<string.h> using namespace std;

class bankaccount {

public: char name[20]; long accno; int acctype; double balance;

int assign(); int deposit(); int withdraw(); int display();

};

int main() {

bankaccount obj;

int bankaccount::assign() { cout<<"enter name\naccount no.\n1:savings acccount\t2:current account"; gets(obj.name);

cin>>obj.accno>>obj.acctype; balance=0.0; } int bankaccount::deposit() { cout<<"enter amount to be deposited"; cin>>obj.balance; } int bankaccount::withdraw() { cout<<"current account balance:"<<balance<<endl; cout<<"enter amount to be withdrawn"; double withdraw; cin>>withdraw; if(withdraw<=obj.deposit) obj.balance-=withdraw; else cout<<"insufficient balance"; } int bankaccount::display() { puts(obj.name); cout<<"account balance:"<<obj.balance<<endl; } return 0; }

2.To check whether a number is Armstrong number or not #include<iostream.h> #include<conio.h>

void main() { int Number,Temp,b=0; cout<<endl<<"Enter any number to check"; cin>>Number; Temp=Number; int P; while(Temp>0) { P=Temp%10; b=b P*P*P; Temp=Temp/10; } if(b==Number) { Cout<<endl<<"Armstrong no"; } else { cout<<"Not an armstrong no"; } getch(); } 3.To check whether a given number is palindrome or not #include<iostream.h> #include<conio.h> void main()

{ clrscr(); long int n,rev=0,m,num; cout<<"please enter a five digit no.: "; cin>>n; num=n; while(n>0) { m=n%10; rev=rev*10+m; n=n/10; } cout<<rev<<endl; if (num==rev) cout<<"Number is a palindrome"; else cout<<"Not a palindrome"; getch(); } 4. An electricity board charges the following rates to domestic users to discourage
the wastage of electricity. For the first 100 units: 60 P/unit. For the next 200 units: 80 P/unit. Beyond 300 units: 90 P/unit. All users are charged a minimumof Rs 50. If the total amount is more than Rs300 then additional surcharge of 15% is added. Write a program to read the names of users and number of units consumed and print the total charges with names of consumers.

#include<iostream.h> #include<conio.h>

void main() { clrscr(); cout<<"\n\n\n\tElectricity Board Charges\n"; cout<<"\n\tTo Discourage Large Consumption of energy\n\n";char name[20]; cout<<"\n\nEnter USER name :-"; cin>>name; cout<<"\n\nEnter Number of Units Consumed:-"; float unit; cin>>unit; float tc; if(unit<=100) tc=unit*0.60; else if(unit<=300) tc=unit*0.80; else tc=unit*0.90; float surchase=0; if(tc>300) surchase=tc*0.15; float total_cost; total_cost = 50 + surchase + tc; cout<<"\n\nYOUR BILL AMOUNT IS "<< total_cost; getch(); } 5. To add two objects

#include<iostream>

using namespace std; class data { int a,b; public: int sum; void getdata(int a,int b); void display(); }; void data::display() { cout<<"sum of a and b="<<sum; } void data::getdata(int a,int b) { sum=a+b; } int main() { data d; d.getdata(20,11); d.display(); return 0; }

6.to check whether a number is strong or not #include<iostream> int main(){ int num,i,f,r,sum=0,temp;

cout << "Enter a number: "; cin >> num;

temp=num; while(num){ i=1,f=1; r=num%10;

while(i<=r){ f=f*i; i++; } sum=sum+f; num=num/10; } if(sum==temp) cout << temp << " is a strong number"; else cout << temp << " is not a strong number";

return 0; }

7a)Swap numbers using call by reference


#include<iostream.h> void swap(int &iNum1, int &iNum2); void main() { int iVar1, iVar2; cout<<"Enter two numbers "<<endl; cin>>iVar1; cin>>iVar2; swap(iVar1, iVar2); cout<<"In main "<<iVar1<<" "<<iVar2<<endl; } void swap(int &iNum1, int &iNum2) { int iTemp; iTemp = iNum1; iNum1 = iNum2; iNum2 = iTemp; cout<<"In swap "<<iNum1<<" "<<iNum2<<endl; } 7b) Swap numbers using call by value #include<iostream.h> void swap(int iNum1, int iNum2); void main() { int iVar1, iVar2; cout<<"Enter two numbers "<<endl; cin>>iVar1; cin>>iVar2; swap(iVar1, iVar2); } void swap(int iNum1, int iNum2) { int iTemp; iTemp = iNum1; iNum1 = iNum2; iNum2 = iTemp; cout<<"In swap "<<iNum1<<" "<<iNum2<<endl; }

8.Function Overloading

#include <iostream>

using namespace std;

long add(long, long); float add(float, float);

int main() { long a, b, x; float c, d, y;

cout << "Enter two integers\n"; cin >> a >> b;

x = add(a, b);

cout << "Sum of integers: " << x << endl;

cout << "Enter two floating point numbers\n"; cin >> c >> d;

y = add(c, d);

cout << "Sum of floats: " << y << endl;

return 0; }

long add(long x, long y) { long sum;

sum = x + y;

return sum; }

float add(float x, float y) { float sum;

sum = x + y;

return sum; } 9. To swap private member values of class by using friend function #include<iostream.h> #include<conio.h> class XYZ; class ABC; {

int a; public; void setdata(int m) { a=m; } Friend void exchange(ABC & X,XYZ &W); void display data() { cout<"a= "<<a; } }; class XYZ; class XYZ; { int b; public: void setdata(int n) { b=n; } friend void exchange(ABC&X,XYZ &W); void displaydata() { cout<<"b= "<<b; } };

void exchange(ABC & value 1,XYZ & value 2) { int temp; temp=value 1.a; value 1.a=value 2.b; value 2.b=temp; }; void main() { ABC m; m.setdata(10); XYZ n; n.setdata(20); cout<<"values before exchange"; m.display data(); n.display data(); exchange(m,n); cout<<"values after exchange"; m.display data(); n.display data(); } 10. WAP to print the count of items sold and remaining stock separately, in a shopping mall (in
a day). Create a class shop having member functions sold_items and stock_remaining. Use static member data wherever required.(using static members) #include<iostream> using namespace std;

class shop {

int sold; int left; static int total; public: void sold_items() { cout<<"Enter the number of sold items"<<endl; cin>>sold; }

void stock_remaining() { left=total-sold; }

void display() { cout<<"Count of items sold"<<sold<<endl; cout<<"Remaining items are:"<<left<<endl; }

}; int shop::total=1000;

static void showtotal() { cout<<"Total"; }

int main() { shop items; items.sold_items();

items.stock_remaining(); items.display(); return 0; } 11.write the same program without using static members #include<iostream> using namespace std;

class shop { int sold; int left; int total; public: void sold_items() { cout<<"Enter the number of sold items"<<endl; cin>>sold; }

void stock_remaining() { total=1000; left=total-sold; } void showtotal() { cout<<"Total"; } void display() { cout<<"Count of items sold"<<sold<<endl; cout<<"Remaining items are:"<<left<<endl;

}; int main() { shop items; items.sold_items(); items.stock_remaining(); items.display(); return 0; }

12. To check whether a string is palindrome or not #include <iostream> #include <string>

using namespace std;

int main() { char str[100];

cout << "Enter word :"; cin >> str; int x = strlen(str)-1; for(int i = 0; i <= x; i++) { if (str[i] == str[x-i]) { continue;

} else { cout<<"Not a palidrome"<<endl; return 0; } }

cout << "Indeed Palidrome"<<endl; return 0; } 13. Create payroll system using single inheritance #include<iostream>

#include<cstring>

using namespace std;

class employee

public:

string name;

double yoj;

int age;

void getdata();

};

class salary : public employee

{ double basic;

double allowance;

double bonus;

public :

//void get();

void display();

double tot();

};

void employee ::

getdata()

cout << "enter the name" << endl; cin>> name;

cout << "enter year of joining" << endl; cin>> yoj; cout << "enter the age " << endl; cin >> age;

void salary :: display()

double sal = tot();

cout << name << endl << yoj << endl << age << endl;

cout << "the salary of "<< name << " is " << sal;

double salary :: tot()

cout << "enter the basic pay" << endl;

cin >> basic; cout << "enter the total allowance" << endl; cin >> allowance;

cout << "enter the bonus if any" << endl; cin >> bonus ;

double sum = basic + allowance + bonus ;

return sum;

int main()

{ salary s;

s.getdata();

s.display();

return 0;

} 14.To construct a pattern #include<iostream>

using namespace std;

int main()

{ char c='A';

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

for(int j=5;j>i;j--)

cout<<" ";

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

cout<<c<<" ";

c++;

cout<<endl;

return 0;

15.Remove duplicates from the string #include<iostream> #include<string> #include<cstring> using namespace std; main () { int i; int j=0; string s; cout << "enter the string " << endl; cin >> s; int l = s.length(); for(i=0;i<l;i++) { for(j=i+1;j<l;)

{ if(s[j]==s[i]) { for(int k=j;k<l;k++) s[k]=s[k+1]; l--; } else j++; } } cout << "the string after removing duplicates is" << endl << s; }

16. To print a spiral #include <iostream>

#define R 3

#define C 6

using namespace std;

void spiralPrint(int m, int n, int a[R][C])

int i, k = 0, l = 0;

while (k < m && l < n)

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

cout<< a[k][i];

k++;

for (i = k; i < m; ++i)

cout<< a[i][n-1];

n--;

if ( k < m)

for (i = n-1; i >= l; --i)

cout<< a[m-1][i];

m--;

if (l < n)

{ for (i = m-1; i >= k; --i)

cout<< a[i][l];

l++;

int main()

{ i nt a[R][C] = { {1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18} };

spiralPrint(R, C, a);

return 0;

} 17.To print pattern 2 #include<iostream>

using namespace std;

int main()

char c='A';

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

for(int j=5;j>i;j--)

cout<<" ";

for(int k=1;k<=i*2-1;k++)

cout<<c;

c++;

cout<<endl;

return 0;

} 18. //WAP to print student's status of academic and sport activities by forming the three classes namely student, sports. Use multiple inheritance and assume the required data.

#include<iostream>

#include<cstring>

using namespace std;

class acad

public:

string branch;

float cg;

void getdata();

};

class sport

public : string sport;

int year; void get();

};

class student :public acad, public sport

string name = "saurabh";

public :

void display();

};

void acad :: getdata()

cout << "enter the branch" << endl; cin>> branch;

cout << "enter the cgpa " << endl; cin >> cg;

void sport :: get()

cout << "enter the sport name " << endl; cin>> sport;

cout << "enter the year " << endl; cin >> year;

void student :: display()

cout<<endl << name << endl << branch << endl << year << endl << cg << endl << sport << endl;

int main() {

student s;

s.getdata(); s.get();

s.display();

return 0;

} 19. Program to calculate the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class student_id { int rno; char name[20]; public: void read_id()

{ cout<<"\n\nEnter the Name of the Student : "; gets(name); cout<<"\n\nEnter the Roll No. : "; cin>>rno; } void display_nr() { cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : "; puts(name); cout<<"\n\nROLL NO. : "<<rno; } }; class marks:public student_id { public: int i,mark[3]; void read_m() { read_id(); for(i=0;i<3;i++) { cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : "; cin>>mark[i]; } } void display_m()

{ display_nr(); cout<<"\n\n\tMarks Secured "; for(i=0;i<3;i++) cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i]; } }; class sports { public: int sm; void read_sportm() { cout<<"\n\nEnter the marks in SPORTS out of 10 : "; cin>>sm; } }; class percentage:public marks,public sports { public: float total,prcntge; void calculate() { read_m(); read_sportm(); total=0; for(i=0;i<3;i++)

{ total+=mark[i]; } total+=sm; prcntge=(total/310)*100; } void display_totp() { display_m(); cout<<"\n\nTOTAL = "<<total; cout<<"\n\nPERCENTAGE = "<<prcntge; } }; void main() { int cont; percentage pc; clrscr(); do { pc.calculate(); clrscr(); pc.display_totp(); cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)"; cin>>cont; }while(cont==1); getch();