Lab 6 (Conditional Statment) - 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Introduction to Computing

Lab 06
Topic Conditional Statement (if-else), Nested if, Switch
Objective Learning how to build logic by using conditional statements.

multiple if:

syntax:

if(expression)
{
Body of if(statement);
}
if(expression)
{
Body of if(statement);
}
In this selection statement, a set of instruction are dependent on the condition/expression of the
selection statement if the condition/expression is true body of the selection statement will be executed.
But it is quite different from above mention statements. Because, if any selection statement is true it
will not have skipped any statement. Every selection statement will be tested and executed accordingly.

nested if:

syntax:

if(expression)
{
if(expression)
{
Body of if(statement);
}
}
In this selection statement, a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed which is another
selection statement.

Switch:

Syntax:

Switch(condition)

case 1:
break;

case 2:

break;

default:

Task 1

Write a program that takes one value from the user, asks about the type of conversion, and then
performs a conversion depending on the type of conversion. (use if-else-if)

If the user enters:

 I -> convert from inches to centimeters. [1 inch = 2.54 centimeter]


 C -> convert from centimeters to inches. [1 centimeter = 0.393701 inch]
 G -> convert from gallons to liters. [1 gallon = 3.78541 liter]
 L -> convert from liters to gallons. [1 liter = 0.264172 gallon]
 M -> convert from mile to kilometer. [1 mile = 1.60934 kilometer]
 K -> convert from kilometer to mile. [1 kilometer = 0.621371 mile]
 P -> convert from pound to kilogram. [1 pound = 0.453592 kilogram]
 O -> convert from pound to ounce. [1 pound = 16 ounce]
 F -> convert from Fahrenheit to Celsius. [ c = (f – 32) * (5 / 9)]
 C -> convert from Celsius to Fahrenheit. [ f = c * (9 / 5) + 32]

Task 2

Write and run a program that plays the game of “Rock, paper, scissors.” In this game, two players
simultaneously say (or display a hand symbol representing) either “rock,” “paper,” or “scissors.” The
winner is the one whose choice dominates the other. (use nested if)

The rules are:

 paper dominates (wraps) rock,


 rock dominates (breaks) scissors,
 and scissors dominate (cut) paper.
You can use 1=rock,2=paper,3=scissors

Sample Input: 1 1

Sample Output: Draw

Sample Input: 1 2

Sample Output: 2nd player wins

#include <iostream>
using namespace std;
int main(){
int user1;
int user2; //1 for rock 2 for paper 3
for sciccor
cout << "user 1 turn = ";
cin >> user1;
cout << "user 2 turn =";
cin >> user2;
if (user1 == user2){
cout << "draw" << endl;
}
else {
if (user1 == 1 && user2 == 3){
cout << "user 1 won" << endl;
}
else if (user1 == 2 && user2 == 1){
cout << "user1 won" << endl;
}
else if (user1 == 1 && user2 == 2){
cout << "user 2 won" << endl;
}
else if (user1 == 2 && user2 == 3){
cout << "user 2 won" << endl;
}
else if (user1 == 3 && user2 == 2){
cout << "usere 1 won" << endl;
}
else if (user1 == 3 && user2 == 1){
cout << "user2 won " << endl;
}

Task 3

Write and run a program that reads two integers and then uses the conditional expression operator
to print either “multiple” or “not” according to whether one of the integers is a multiple of the
other.
Sample Input: 12 6
Output: 12 = 6 * 2
Sample Input: 18 8
Output: 18 = 8 * 2 + 2
Sample Input: 12 13
Output: 14 = 12 * 1 + 1

#include <iostream>
using namespace std;
int main(){
int num1;
int num2;
cout << "enter a number ";
cin >> num1; cin >> num2;
if (num1%num2 == 0){
cout << num1 << "=" << num2 << "*" << num1 / num2 << endl;
}
else if (num2%num1 == 0){
cout << num2 << "=" << num1 << "*" << num2 / num1 << endl;
}
else
cout << "not a multiple " << endl;
system("pause");

Task 4

Write a program to determine the weight of the chocolates being sold. The program takes as input,
the number of chocolates being sold, the weight of one chocolate in ounces, and the choice of
weighing i.e., ounces, pounds, grams, or kilograms. The user enters ‘O’ as a choice to calculate
weight in ounces, ‘P’ for pounds, ‘G’ for grams, and ‘K’ for kilograms. Depending on the choice
entered by the user the program calculates the weight of chocolates. Display an appropriate
message if an invalid choice is entered. Use the following formulas to calculate the total weight of
the chocolates. (use switch).

For weighing in Ounces

total_weight = number_of_chocolates * weight_of_Chocolate

For weighing in Pounds

total_weight = number_of_chocolates * weight_of_Chocolate / 16

For weighing in Grams

total_weight = number_of_chocolates*weight_of_Chocolate * 28.349

For weighing in Kilograms

total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;
Expected Output:

Enter the number of chocolates being sold 20

Enter the weight of one chocolate in ounces 2

Enter the choice for weighing

Enter O to calculate in ounces

Enter P for pounds

Enter G for grams

Enter K for kilograms

Enter your Choice: O

Weight in Ounces is 40

Task 5

The date June 10, 1960, is special because when we write it in the following format, the month
times the day equals the year.

6/10/60

Write a program that asks the user to enter a month (in numeric form), a day, and a two-digit year.
The program should then determine whether the month times of the day are equal to the year. If so,
it should display a message saying the date is magic. Otherwise, it should display a message saying
the date is not magic. (use nested if, or if else)

#include <iostream>
using namespace std;
int main(){
int months;
int day;
int year;
cout << "Enter day = ";
cin >> day;
cout << "Enter the month = ";
cin >> months;
cout << "enter two digit year = ";
cin >> year;
if (months*day == year){
cout <<day<<"/"<<months<<"/"<<year<<" is special date "<< endl;

}
else{
cout << year << "this is a normal year" << endl;

system("pause");

Task 6

Write an if statement that prints the message “The number is valid” if the variable grade is within
the range 0 through 100.

Task 7

Write and run a program that simulates a simple calculator. It reads two integers and a character. If
the character is a ‘+’, the sum is printed; if it is a ‘- ‘, the difference is printed; if it is a ‘*’, the product
is printed; if it is a ‘/’, the quotient is printed; and if it is a ‘%’, the remainder is printed. (use switch)

Sample Input: 12%7 Sample Input: 12 $ 7


Sample Output: 5 Sample Output: Invalid operator Entered
Sample Input: 19x10 Sample Input: 10/3
Sample Output: 190 Sample Output: 3.33333

#include <iostream>
using namespace std;
int main(){
double num1;
double num2;
char op;
double result;
cout << "Enter number = ";
cin >> num1;
cout << "enter number = ";
cin >> num2;
cout << "enter operator=";
cin >> op;
switch (op){
case'+':
cout << num1 << "+" << num2 << " = " << num1 + num2 << endl;
break;
case'-':
cout << num1 << "-" << num2 << " = " << num1 - num2 << endl;
break;
case'/':
cout << num1 << "/" << num2 << " = " << num1 / num2 << endl;
break;
case'*':
cout << num1 << "*" << num2 << " = " << num1 * num2 << endl;
break;
case '%':
// Check for modulo by zero
if (num2 != 0) {
result = num1 % num2;
cout << "Remainder: " << result << endl;
}

default:
cout << "invalid operator" << endl;

system("pause")

Task 8
Write a program that takes 3 input integers and tells the 2nd maximum.

Sample Input: 90 5 60

Sample Output: 60

#include <iostream>
using namespace std;
int main(){
int num1;
int num2;
int num3;
cout << "enter num =";
cin >> num1;
cout << "enter num2 =";
cin >> num2;
cout << "enter number 3 = ";
cin >> num3;
if (num1 > num2&&num1 < num3) {

cout << num1 << "is greter " << endl;


}
else if (num2 > num1 && num2 < num3){
cout << num2 << "is greater " << endl;
}
else if (num3 > num1&&num3 < num2){
cout << num3 << "is greater" << endl;

}
system("pause");
}

Task 9
Write a program that prints all English characters (Uppercase + Lowercase) ascii values.

You might also like