Professional Documents
Culture Documents
Lab 6 (Conditional Statment) - 1
Lab 6 (Conditional Statment) - 1
Lab 6 (Conditional Statment) - 1
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)
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)
Sample Input: 1 1
Sample Input: 1 2
#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).
total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;
Expected Output:
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)
#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) {
}
system("pause");
}
Task 9
Write a program that prints all English characters (Uppercase + Lowercase) ascii values.