C++ & Java : Assignment 2
Here are the two programs required for the 2nd assignment.
1) Program to calculate area:
#include
#include
#include
#include
void area(int a) // Square
{
int x;
x=a*a;
cout << "\nArea of square = " << x << " Sq.Units\n";
}
void area(int a, int b) //Rectangle
{
int x;
x=a*b;
cout << "\nArea of rectangle = " << x << " Sq.Units\n";
}
void area(float p, int a) //Circle
{
float x;
x=p*a*a;
cout << "\nArea of circle = " << x << " Sq.Units\n";
}
int main()
{
int choice,a,b;
float p=3.14;
cout << "Enter 1 for area of square\n";
cout << "Enter 2 for area of circle\n";
cout << "Enter 3 for area of rectangle\n";
cin >> choice;
if(choice==1)
{
cout << "\nEnter side of the square: ";
cin >> a;
area(a);
}
else if(choice==2)
{
cout << "\nEnter radius of the circle:";
cin >> a;
area(p,a);
}
else if(choice==3)
{
cout << "\nEnter length of the triangle:";
cin >> a;
cout << "\nEnter breadth of the triangle:";
cin >> b;
area(a,b);
}
else
{ cout << "\nInvalid choice"; }
getch();
return 0;
}
2) Program to create account and operate it
#include
#include
#include
#include
class account
{
char name[30];
int acc_no;
char type[5];
int balance;
public:
void init_value(void);
void deposit(void);
void withdraw(void);
void details(void);
};
void account::init_value(void)
{
cout << "Enter the name of the depositor: ";
cin >> name;
cout << "\nEnter the initial amount in the account: ";
cin >> balance;
cout << "Enter the type of account" << "\n'sav' for saving account" << "\n'sal' for salary account\n";
cin >> type;
}
void account::deposit(void)
{
int dep_amount;
cout << "\nEnter the amount you want to deposit: ";
cin >> dep_amount;
balance = balance + dep_amount;
}
void account::withdraw(void)
{
int wit_amount;
cout << "\nEnter the amount you want to withdraw: ";
cin >> wit_amount;
if(balance>wit_amount)
balance = balance - wit_amount;
else
cout << "\nYou do not have enough balance in your acount";
}
void account::details(void)
{
cout << "\nName: " << name;
cout << "\nBalance: " << balance;
cout << "\nAcc. Type: " << type;
}
int main()
{
account account1;
int choice;
account1.init_value();
do
{
cout << "\n\n\nEnter 1 to deposit money in the account";
cout << "\nEnter 2 to withdraw money from the account";
cout << "\nEnter 3 to check the details of your account";
cout << "\nEnter 4 to exit\n";
cin >> choice;
if (choice==1)
account1.deposit();
else if (choice==2)
account1.withdraw();
else if (choice==3)
account1.details();
else
exit;
}
while(choice!=4);
getch();
return 0;
}
1) Program to calculate area:
#include
#include
#include
#include
void area(int a) // Square
{
int x;
x=a*a;
cout << "\nArea of square = " << x << " Sq.Units\n";
}
void area(int a, int b) //Rectangle
{
int x;
x=a*b;
cout << "\nArea of rectangle = " << x << " Sq.Units\n";
}
void area(float p, int a) //Circle
{
float x;
x=p*a*a;
cout << "\nArea of circle = " << x << " Sq.Units\n";
}
int main()
{
int choice,a,b;
float p=3.14;
cout << "Enter 1 for area of square\n";
cout << "Enter 2 for area of circle\n";
cout << "Enter 3 for area of rectangle\n";
cin >> choice;
if(choice==1)
{
cout << "\nEnter side of the square: ";
cin >> a;
area(a);
}
else if(choice==2)
{
cout << "\nEnter radius of the circle:";
cin >> a;
area(p,a);
}
else if(choice==3)
{
cout << "\nEnter length of the triangle:";
cin >> a;
cout << "\nEnter breadth of the triangle:";
cin >> b;
area(a,b);
}
else
{ cout << "\nInvalid choice"; }
getch();
return 0;
}
2) Program to create account and operate it
#include
#include
#include
#include
class account
{
char name[30];
int acc_no;
char type[5];
int balance;
public:
void init_value(void);
void deposit(void);
void withdraw(void);
void details(void);
};
void account::init_value(void)
{
cout << "Enter the name of the depositor: ";
cin >> name;
cout << "\nEnter the initial amount in the account: ";
cin >> balance;
cout << "Enter the type of account" << "\n'sav' for saving account" << "\n'sal' for salary account\n";
cin >> type;
}
void account::deposit(void)
{
int dep_amount;
cout << "\nEnter the amount you want to deposit: ";
cin >> dep_amount;
balance = balance + dep_amount;
}
void account::withdraw(void)
{
int wit_amount;
cout << "\nEnter the amount you want to withdraw: ";
cin >> wit_amount;
if(balance>wit_amount)
balance = balance - wit_amount;
else
cout << "\nYou do not have enough balance in your acount";
}
void account::details(void)
{
cout << "\nName: " << name;
cout << "\nBalance: " << balance;
cout << "\nAcc. Type: " << type;
}
int main()
{
account account1;
int choice;
account1.init_value();
do
{
cout << "\n\n\nEnter 1 to deposit money in the account";
cout << "\nEnter 2 to withdraw money from the account";
cout << "\nEnter 3 to check the details of your account";
cout << "\nEnter 4 to exit\n";
cin >> choice;
if (choice==1)
account1.deposit();
else if (choice==2)
account1.withdraw();
else if (choice==3)
account1.details();
else
exit;
}
while(choice!=4);
getch();
return 0;
}
0 Comments:
Post a Comment
<< Home