Programming fundamentals
MID TERM ASSIGNMENT (ADP(cs))
ATZAZ ALI KHAWAR AND AHSAN TARIQ
ROLL NO 56 AND 03
SUBMITT TO PROF. ABUL HASSAN
HudsenTech
Program no 1:
//Write a program that encourages the user to enter two fractions, and then
//displays their sum in fractional form.
#include<iostream>
using namespace std;
void main()
{
int a, b, c, d;
char s, opr;
cout << "Enter first fraction: ";
cin >> a >> s >> b;
cout << "Enter the operator:";
cin >> opr;
cout << "Enter second fraction:";
cin >> c >> s >> d;
switch (opr)
{
case '+':
cout << "Adition:" << endl;
cout << a << "/" << b << opr << c << "/" << d << "=" << (a*d) +
(b*c) << "/" << b*d << endl;
break;
case'-':
cout << "Subtraction:" << endl;
cout<< a << "/" << b << opr << c << "/" << d << "=" << (a*d) +
(b*c) << "/" << b*d << endl;
break;
case'*':
cout << "Multiplication: "<<endl;
cout << a << "/" << b << opr << c << "/" << d << a*c << "/" << b*d
<< endl;
break;
case'/':
cout << "Division:" << endl;
cout << a << "/" << b << opr << c << "/" << d << "=" << a*c << "/"
<< b*d << endl;
break;
default:
cout << "Invalid operator" << endl;
break;
}
system("pause");
}
Program no 2:
Write a program that prompts the user to input the coordinates of
a line which lie on
the circumference of a circle is the diameter of the circle. Write a program
to
compute the area of the circle. (Hint. Use distance formula for calculating
the
diameter of a circle.)
#include<iostream>
using namespace std;
void main()
{
int x1, y1, x2, y2;
double radius, diameter, area;
cout << "Enter x1:" << endl;
cin >> x1;
cout << "Enter y1:" << endl;
cin >> y1;
cout << "Enter x2:" << endl;
cin >> x2;
cout << "Enter y2:" << endl;
cin >> y2;
diameter = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));
radius = diameter / 2;
area = 3.14*radius*radius;
area = 3.14*radius*radius;
cout << "Area of the circle is=" << area << endl;
cout << "Diameter of the circle=" << diameter << endl;
cout << "Radius of the circle=" << radius << endl;
system("pause");
}
Program No 3 :
Title
Program Description: Write a C++ Program that Input (x,y) coordinate of
a point in the coordinate plane, then determines where it lies,
on x-axis or y-axis or in any of the four quadrants.
*/
#include<iostream>
using namespace std;
void main()
{
int x, y;
cout << "Enter The X Quadrant" << endl;
cin >> x;
cout << "Enter The Y Quadrant" << endl;
cin >> y;
if ((x>0)&&(y>0))
{
cout << "The points lies between 1st Quadrant" << endl;
}
else if ((x<0)&&(y>0))
{
cout << "The points lies between the 2nd Quadrant" << endl;
}
else if ((x>0)&&(y<0))
{
cout << "The points lies between 3rd Quadrant" << endl;
}
else if ((x<0)&&(y<0))
{
cout << "The points lies between 4th Quadrant" << endl;
}
else
{
cout << "The points lies on the origion" << endl;
}
{
}
system("pause");
}
®®®®
Program no 4:
/*In a right triangle, the square of the length of one side is equal to the
sum of the
squares of the lengths of the other two sides. Write a program that prompts
the user
to enter the lengths of three sides of a triangle and then outputs a message
indicating whether the triangle is a right triangle.*/
#include<iostream>
using namespace std;
void main()
{
float hyp, base, alt;
cout << "Enter the Hyp value" << endl;
cin >> hyp;
cout << "Enter the Base value" << endl;
cin >> base;
cout << "Enter the Alt value" << endl;
cin >> alt;
base = base*base;
hyp = hyp*hyp;
alt = alt*alt;
if (hyp == base + alt)
{
cout << "The angle is right triangle" << endl;
}
else
{
cout << "The angle is not right tiangle" << endl;
}
system("pause");
}
Program no 5
Write a program that mimics a calculator. The program should take as input two
integers and the operation to be performed. It should then output the numbers, the
operator, and the result. (For division, if the denominator is zero, output an
appropriate message.) Some sample outputs follow:
#include<iostream>
using namespace std;
void main()
{
double a, b, c;
char op;
cout << "Enter the 1st number:" << endl;
cin >> a;
cout << "Enter the 2nd number:" << endl;
cin >> b;
cout << "Enter the Arithmetic operators:" << endl;
cin >> op;
if (op == '+')
{
c = a + b;
cout << a << op << b << "=" << c << endl;
}
else if (op == '-')
{
c = a - b;
cout << a << op << b << "=" << c << endl;
}
else if (op == '*')
{
c = a*b;
cout << a << op << b << "=" << c << endl;
}
else if (op == '/')
{
if (b == 0)
{
cout << "Cannot be divided by zero." << endl;
}
else
{
c = a / b;
cout << a << op << b << "=" << c << endl;
}
}
else
{
cout << "Invalid operator." << endl;
}
system("pause");
}
Program:6
//A bank in your town updates its customers’ accounts at the end of each
month.The bank offers two types of
//accounts : savings and checking.Every customer must maintain a minimum
balance.If a customer’s balance falls
//below the minimum balance, there is a service charge of $10.00 for savings
accounts and $25.00 for checking
//accounts.If the balance at the end of the month is at least the minimum
balance, the account receives interest
//as follows : a.Savings accounts receive 4 % interest.b.Checking accounts
with balances of up to $5, 000 more
//than the minimum balance receive 3 % interest; otherwise, the interest is 5
% .Write a program that reads a
//customer’s account number(int type), account type(char; s for savings, c for
checking), minimum balance that
//the account should maintain, and current balance.The program should then
output the account number, account type,
//current balance, and an appropriate message.Test your program by running it
five times, using the following data :
//46728 S 1000 2700
//87324 C 1500 7689
//79873 S 1000 800
//89832 C 2000 3000
//98322 C 1000 750
#include<iostream>
using namespace std;
int main()
{
int anum, min, cur, newbal;
char atype;
cout << "Enter Account Number please.." << endl;
cin >> anum;
cout << "Enter S for Saving and C for Checking please.." << endl;
cin >> atype;
cout << "Enter Minimum Balance please" << endl;
cin >> min;
cout << "Enter Current Balance please.." << endl;
cin >> cur;
if (cur<min)
{
if (atype == 'S' || atype == 's')
{
newbal = cur - 10;
cout << "Acc Balance is less then minimun Balance. we have
cuted the service Charges of $10";
cout << endl << "Your New Balance is: $" << newbal;
}
else if (atype == 'C' || atype == 'c')
{
newbal = cur - 25;
cout << "Acc Balance is less then minimun Balance. we have
cuted the service Charges of $25";
cout << endl << "Your New Balance is: $" << newbal;
}
else
{
"You Enter Wrong Type of Account";
}
}
else if (cur>min)
{
if (atype == 'S' || atype == 's')
{
newbal = cur + ((cur * 4) / 100);
cout << "Acc Balance is more then minimun Balance. we have
awarded you $" << ((cur * 4) / 100);
cout << endl << "Your New Balance is: $" << newbal;
}
else if (atype == 'C' || atype == 'c')
{
if (cur >= 5000)
{
newbal = cur + ((cur * 3) / 100);
cout << "Acc Balance is more then minimun Balance. we
have awarded you $" << ((cur * 3) / 100);
cout << endl << "Your New Balance is: $" << newbal;
}
else
{
newbal = cur + ((cur * 5) / 100);
cout << "Acc Balance is more then minimun Balance. we
have awarded you $" << ((cur * 5) / 100);
cout << endl << "Your New Balance is: $" << newbal;
}
}
else
{
cout << "You Enter Wrong Type of Account.";
}
}
system("pause");
}
Program No 7:
/*Calculate Electricity Bill by taking units from user and calculate the
total bill according to the following criteria:
<=100 units RS.4/units
> 100 and <=300 units RS.4.50/units
>300 and <=500 units RS.4.75/units
>500 units RS.5/units
Add fuel surcharge 20% and Govt. Tax 10% on bill to calculate the total bill
and
display it.*/
#include<iostream>
using namespace std;
void main()
{
double bill, units;
cout << "enter the units=";
cin >> units;
if (units > 500)
{
bill = units * 5;
}
else if (units > 300)
{
bill = units*4.75;
}
else if (units >= 100)
{
bill = units*4.50;
}
else
{
bill = units * 4;
}
bill = bill + (bill*20.0 / 100.0);
bill = bill + (bill*10.0 / 100.0);
cout << "your bill is" << bill << endl;
system("pause");
}
Program no 8:
/*A palindrome is a number or a text phrase that reads the same
backward as forward.
For example, each of the following five-digit integers is a palindrome: 12321,
55555,
45554 and 11611. Write a C++ program that reads in a five-digit integer and
determines whether it is a palindrome.*/
#include <iostream>
using namespace std;
void main()
{
int n, a, b, c, d, e;
cout << "Type Down Five Numbers: " << endl;
cin >> n;
a = n / 10000;
b = n / 1000 % 10;
c = n / 100 % 10;
d = n / 10 % 10;
e = n % 10;
if (a == e && b == d)
{
cout << "This is a palindrome!\n" << endl;
}
else
{
cout << "No it's not a palindrome....!\n" << endl;
}
system("pause");
}
Program no 9:
//an insurance company follows following rules to calculate premium.
//(1) if a person's health is excellent and the person is between25 and 35
years of age and lives in a city and is a male then the premium is ra. 4 per
thousand and his policy
//amount cannot exceed rs. 2 lakhs.
//(2) if a person satisfies all the above conditions except that the sex is
female then the premium is rs. 3 per thousand and her policy amount and cannot
exceed rs. 1 lakh.
//(3) if a person's health is poor and the person is between25 and 35 years
of age and lives in a village and is a male then the premium is ra. 6 per
thousand and his policy
//amount cannot exceed rs.10,000.
//(4) in all other cases the person is not ensured.
//write a programme to output whether the person should be insured or
not,his/her premium rate and maximum amount for which he/she can be insured.
#include<iostream>
using namespace std;
void main()
{
char health, location, gender;
int age;
cout << "Enter 'e' for excellent health and 'p' for poor health..." <<
endl;
cin >> health;
cout << "Enter 'c' for city and 'v' for village..." << endl;
cin >> location;
cout << "Enter 'm'for male and 'f' for female..." << endl;
cin >> gender;
cout << "Enter the age plzz..." << endl;
cin >> age;
if ((health=='e')&&(location=='c')&&(gender=='m')&&(age>=25 && age<=35))
{
cout << "Congrats,The person is insured.The premium is Rs.4 per
thousand his policy can't exceed Rs.2 lakhs.." << endl;
}
else if ((health == 'e') && (location == 'c') && (gender == 'f') && (age
>= 25 && age <= 35))
{
cout<<"Congrats,The person is insured.The premium is Rs.3 per
thousand his policy can't exceed Rs.1 lakh.." << endl;
}
else if ((health == 'p') && (location == 'v') && (gender == 'm') && (age
>= 25 && age <= 35))
{
cout << "Congrats,The person is insured.The premium is Rs.6 per
thousand his policy can't exceed Rs.10,000" << endl;
}
else
{
cout << "Sorry,The person is not insured" << endl;
}
system("pause");
}
Program No 10;
/*Write a program that takes balance of a user’s account as input. It should
then ask
the user how much amount he wants to withdraw from his account. The program
should take this amount as input and deduct from the balance. Similarly it
should
ask the user how much amount he wants to deposit in his account. It should
take
this amount as input and add to the balance. The program shall display the new
balance after amount has been withdrawn and deposited.
Sample Output:
Welcome!*/
#include<iostream>
using namespace std;
void main()
{
int amount, option, withdraw, new_balance, deposit;
cout << "Enter Your Amount Please... " << endl;
cin >> amount;
cout << "Make your Selection please....:- \n <1>___Withdraw Amount \n
<2>___deposit Amount \n <3>___Leave" << endl;
cin >> option;
if (option == 1)
{
cout << "How Much Amount You Want To Withdraw....? " << endl;
cin >> withdraw;
if (withdraw > amount)
{
cout << "Sorry..!'You have low amount' " << endl;
}
else
{
new_balance = amount - withdraw;
cout << "Your Balance Amount Is.... " << new_balance <<
endl;
}
}
else if (option == 2)
{
cout << "How Much Amount You Want To Deposit...? " << endl;
cin >> deposit;
new_balance = deposit + amount;
cout << "Your New Amount Is.... " << new_balance << endl;
}
else if (option == 3)
{
cout << "Thank's For Visiting.... " << endl;
}
else
{
cout << "Invalid " << endl;
}
system("pause");
}
The End