Experiment 03
AIM: Implementing programs using conditional/Decision making/selection statements
Task 1: Develop a program that accepts sales amount; if the sales amount is more than 5000, then the discount is 12% of the sales amount; otherwise, it is 7%—display the total discount and amount to be paid after the discount.
#include <iostream>
using namespace std;
int main()
{
int sa, da, fa;
cout << "Enter the Sales Amount: ";
cin >> sa;
if (sa > 5000)
{
da = sa * 12/100;
}
else if (sa > 0 && sa <= 5000)
{
da = sa * 7/100;
}
else
{
cout << "Invalid Input";
return 0;
}
fa = sa - da;
cout << endl << "The total discount is: " << da << endl << "The amount to be paid is: " << fa;
return 0;
}
Task 2: Write a program to test whether a given character is a capital or small letter and change small letters to capital letters and vice versa.
#include <iostream>
using namespace std;
int main()
{
char chr;
int a;
cout << "Enter the Character: ";
cin >> chr;
a = chr;
if (a > 64 && a < 91)
{
cout << endl << "Capital" << endl;
a = a + 32;
cout << "Small form: " << (char)a << endl;
}
else if (a > 96 && a < 123)
{
cout << endl << "Small" << endl;
a = a - 32;
cout << "Capital form: " << (char)a << endl;
}
else
{
cout << "Invalid";
}
return 0;
}
Task 3: Implement a program to accept a year as input and print whether it is a leap. A year is a leap if divisible by 4, and centennial years (years divisible by 100) are leap years only when divisible by 400.
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter the Year: ";
cin >> year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
cout << "The year is a Leap Year";
}
else
{
cout << "The year is not a Leap Year";
}
}
else
{
cout << "The year is a Leap Year";
}
}
else
{
cout << "The year is not a Leap Year";
}
return 0;
}
Task 4: Develop a program to perform divisibility tests by 3 and 5. If the entered number is divisible by three and not by five print “THREE”; if the number is divisible by five and not by three print “FIVE”; if divisible by both 3 & 5 print “BOTH” otherwise, print “NOT”
#include <iostream>
using namespace std;
int main()
{
int num, num3, num5;
cout << "Enter the Number: ";
cin >> num;
if (num % 3 == 0)
{
num3 = 1;
}
else
{
num3 = 0;
}
if (num % 5 == 0)
{
num5 = 1;
}
else
{
num5 = 0;
}
if (num3 == 1 && num5 == 1)
{
cout << "BOTH";
}
else if (num3 == 1)
{
cout << "THREE";
}
else if (num5 == 1)
{
cout << "FIVE";
}
else
{
cout << "NOT";
}
return 0;
}
Task 5: Vitamin D3 is recommended as the best indicator of vitamin D's nutritional status. If any patient is undergone a Vitamin D3 test, its value ranges from 0 <= to >100 nm/ML. Scott is a Pathologist, and he is doing a vitamin D3 test on his patient. You have to help him automate this process to know the status/level of vitamin D3 depending on its values in nm/mL. Write a program to help Scott to tell the status/level to patients as given in the table below.
D3 in nm/ML Status:
<20 Deficiency
20-30 Insufficiency
30-100 Sufficiency
>100 Toxicity
#include <iostream>
using namespace std;
int main()
{
float d3;
cout << "Enter the amount of D3: ";
cin >> d3;
if (d3 >= 0 && d3 < 20)
{
cout << "Deficiency";
}
else if (d3 >= 20 && d3 < 30)
{
cout << "Insufficiency";
}
else if (d3 >= 30 && d3 <= 100)
{
cout << "Sufficiency";
}
else if (d3 > 100)
{
cout << "Toxicity";
}
else
{
cout << "Invalid Input";
}
return 0;
}
Task 6: Write a program that takes three coefficients (a,b,and c) of a quadratic equation; ax2+bx+c=0 as input, compute all possible roots, and print them with appropriate messages.
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int a, b, c, r1, r2;
cout << "Enter the values of a, b, c of a quadratic equation:" << endl;
cin >> a >> b >> c;
if (((b * b) - (4 * a * c)) > 0)
{
cout << "Two roots possible" << endl;
r1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
r2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
cout << "The two roots are " << r1 << " and " << r2;
}
else if (((b * b) - (4 * a * c)) == 0)
{
cout << "One root possible" << endl;
r1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
r2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
if (r1 == r2)
{
cout << "The root is " << r1;
}
else
{
cout << "Invalid";
}
}
else
{
cout << "No possible roots";
}
return 0;
}
Task 7: Using a switch case, write a program to check whether the entered character is a vowel or consonant.
#include <iostream>
using namespace std;
int main()
{
char chr;
cout << "Enter the character: ";
cin >> chr;
switch (chr)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "Vowel";
break;
default:
cout << "Consonant";
}
return 0;
}
Task 8: Write a program that takes an arithmetic operator (+, -, *, or /) and two operands from the user. Perform corresponding arithmetic operations on the operands using switch case.
#include <iostream>
using namespace std;
int main()
{
int a, b;
char choice;
float result;
cout << "Enter the two integers:" << endl;
cin >> a >> b;
cout << "Enter the operation to perform (+, -, *, /)";
cin >> choice;
switch (choice)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = (float)a / b;
break;
default:
cout << "Invalid Operator";
return 0;
}
cout << "Result: " << result;
return 0;
}
Task 9: Implement a menu-driven program to calculate the area of a triangle, rectangle, circle, and sphere.
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
char choice;
cout << "Select the shape to calculate area of:" << endl
<< "T : Triangle" << endl
<< "R : Rectangle" << endl
<< "C : Circle" << endl
<< "S : Sphere" << endl;
cin >> choice;
switch (choice)
{
case 'T':
case 't':
{
int choice2;
cout << "Select the inputs for Triangle:" << endl
<< "1: Height & Base" << endl
<< "2: Sides" << endl;
cin >> choice2;
if (choice2 == 1)
{
float h, b, a;
cout << "Enter the Height & Base of Triangle (cm): " << endl;
cin >> h >> b;
a = b * h / 2;
cout << endl << "Area of Triangle is: " << a << " cm sq.";
}
else if (choice2 == 2)
{
float a, b, c, s, ar;
cout << "Enter the sides of Triangle (cm): " << endl;
cin >> a >> b >> c;
s = (a + b + c) / 2;
ar = sqrt(s * (s - a) * (s - b) * (s - c));
cout << endl << "Area of Triangle is: " << ar << " cm sq.";
}
else
cout << "Invalid Selection";
}
break;
case 'R':
case 'r':
{
float l, b, a;
cout << "Enter the Length & Breadth of Rectangle (cm): " << endl;
cin >> l >> b;
a = l * b;
cout << endl << "Area of Rectangle is: " << a << " cm sq.";
}
break;
case 'C':
case 'c':
{
float r, a;
cout << "Enter the Radius of Circle (cm): " << endl;
cin >> r;
a = r * r * 22 / 7;
cout << endl << "Area of Circle is: " << a << " cm sq.";
}
break;
case 'S':
case 's':
{
float r, a;
cout << "Enter the Radius of Sphere (cm): " << endl;
cin >> r;
a = r * r * 4 * 22 / 7;
cout << endl << "Area of Sphere is: " << a << " cm sq.";
}
break;
default:
cout << "Invalid selection";
}
return 0;
}