0% found this document useful (0 votes)
12 views22 pages

Exp-3 A Sanskark f035

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views22 pages

Exp-3 A Sanskark f035

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering / School of


Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Experiment: 3
PART A

(PART A: TO BE REFERRED BY STUDENTS)

Aim: Implementing programs using conditional/Decision making/selection statements

Learning Outcomes: The learner would be able to


1. Identify appropriate decision-making statement
2. Understand the syntax of decision-making statements
3. Use decision-making statements to solve problems by writing programs
4. Work with nested decision-making statements

Theory:
C++ Control Constructs/Structure/Statements
- Control statements are used to alter the flow of program execution.
- Control statements evaluate the condition (uses relational and/or logical operators) &
control the flow of execution.
- C++ control constructs/statements are as follows.
Decision Making Statements Loop Control Statements Jump Control Instructions
or
or or
Branching Statement
Conditional Statements Iterative Statements

Or

Selection Statements

● if ● for ● break

● if-else ● while ● continue

● Nested if-else ● do-while ● return

● else if Ladder

● switch-case
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

if
●if - is a decision-making statement.
●“if” is the keyword used to decide to control the flow of execution.
Syntax:-
1. ‘if’ with single statement
if(condition)
true_statement;
- In the above syntax, the condition is evaluated first; if the condition is evaluated as
true, then true_statement is executed.
- The default scope of ‘if’ is a single statement; that’s why there is no need to use curly
braces.
‘if’ with multiple statement.
if(condition){
true_statement 1;
true_statement 2;
…….
true_statement n;
}
Using curly braces increased(multiple statements) the scope of the ‘if’ statement.

if-else
- If-else is a decision-making statement.
- The else clause is an extension to the if clause & contains a false part.
- “if” & “else” are keywords used to decide to control the flow of execution.
- The else block should have matching if, otherwise, mismatch else error will occur.
- As per syntax, no condition is required with the else block.

Syntax:- Flowchart:-

1. if-else with single statement

if(condition)

true_statement;

else

false_statement;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

In the above syntax, if (condition) is evaluated first, if the condition is evaluated as true, then
true_statement is executed. If the condition is evaluated as false, then false_statement is
executed.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

2. if-else with multiple statement. Flowchart:-

if(condition){

true_statement 1;

true_statement 2;

…….

true_statement n;

else{

true_statement 1;

true_statement 2;

…….

true_statement n;

Using curly braces shows the increased scope of the ‘if’ & ‘else’ clauses. If the condition
is evaluated as true, then true_statements are executed; otherwise, false_statements are executed.

Nested if-else
- It is a complex decision-making statement if and/or else clause can be nested one inside
another (as per syntax of else).
- If clause may have if-else and/or its chain in nested if-else. Similarly, the else clause may
have if-else and/or its chain or both if & else clauses have sub if-else clause/block.
- Complex nested if-else (Multiple decision-making) statements may cause problems
maintaining the program.
Syntax:-
if(condition){
if(condition){
statements;
}
else{
statements;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

}
}
else{
if(condition){
statements;
}
else{
statements;
}
}
In the above syntax, the inner if and/or else clauses may have if-else clauses in one
another. It may confuse; that’s why to be careful when nesting if-else. Nested if-else is nothing
but chained with one another.

else-if ladder
- It is a common programming construct used to make multiple decisions. Sometimes, we
may call this as if-else-if ladder.
- It is different than that of nested if-else & less confusing than that of nested if-else.

Syntax:- Flowchart:

if(condition1){

statement1:

else if(condition2){

statement2;

else if(condition3){

statement3;

else{
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

elsebody;

In above format, condition is evaluated from top to down.


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

switch case
- It is a multiple-branching statement.
- It checks for equality, not condition.
Advantages:-
- Easy to use
- Easy to find out errors(if any) & debug.
- Complexity of the program is minimized.

Syntax:

switch(switch conditional statement ){

case constant1:

statements;

break;

case constant2:

statements;

break;

case constant_n:

statements;

break;

default:

default_statement;

In above syntax:

- switch conditional statement is


equality_constant or variable or
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

expression should be of type int or char.

- default is optional

- cases and default can be in any sequence

Nested switch case:


- outer switch block may contain inner switch block, i. e. switch with in a switch.
- The inner and/or outer switch may contain same equality constant.

Syntax:-
switch(equality_constant or variable or expression){
case constant1: statement1; break;
case constant2: statement2; break;
:
case constant_n: statement2; break;
default: default_statement;
switch(equality_constant or variable or expression){
case constant1: statement1; break;
case constant2: statement2; break;
:
case constant_n: statement2; break;
default: default_statement;
}
}
Examples:-
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Note:- Complete the above program


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Write a program to find roots of quadratic equation…

To calculate roots:
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Tasks:

Sr. Problem Statement Flow


No chart
.

1 Write a program to calculate the Goods and Services Tax (GST) for a given amount based
on the GST rate. The program should be able to handle different GST rates and provide a
clear breakdown of the total amount, GST amount, and the amount before GST. Your
program should accept the original price of the item and the GST rate. The rate can be a
whole number or a decimal. The program should display the amount of GST applied, price
before GST and after GST. Constraints: The original price should be a positive number
and the GST rate should be a non-negative number and can be a decimal ranging from 0%
to 28%.
2 Build a program to determine the second largest number from a set of three numbers.

3. Implement a program to find roots of quadratic equation using if-else. ✓

4. Develop a program to calculate the electricity bill including a discount based on the total
bill amount. The initial base bill is 100 Rs, and the program should calculate the total bill
amount by multiplying the number of units consumed by 10 Rs per unit and then adding
this amount to the base bill. The discount should be applied according to predefined ranges
of bill amounts based on the following ranges:
▪ Bill Amount ≤ 200: No discount
▪ 200 < Bill Amount ≤ 500: 5% discount
▪ 500 < Bill Amount ≤ 800: 10% discount
▪ 800 < Bill Amount ≤ 1100: 15% discount
▪ Bill Amount > 1100: 20% discount
The program should display initial base bill amount, bill amount, percentage discount
applied, the discount amount applied and the total bill amount after applying the discount.
5. Write a program that uses a switch-case statement to determine whether an entered
character is a vowel or a consonant. The program should also validate (using if-else) the
input to ensure that only alphabetic characters are processed. If the input is a number or a
special symbol, the program should indicate that the input is not an alphabetic character.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

6. Implement a menu-driven program to calculate the area of a triangle, rectangle, circle, and
sphere.
7. Develop a program that takes an arithmetic operator (+, -, *, or /) and two operands from
the user. Perform corresponding arithmetic operations on the operands using switch case.

Additional Questions
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.
2. 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.
3. 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.
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”
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
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Question 1

#include <iostream>
using namespace std;
int main()
{
double pricebeforegst;
double gstrate;
double gstamount;
double priceaftergst;
cout<<"enter price before gst";
cin>>pricebeforegst;
cout<<"enter gst rate";
cin>>gstrate;
if (pricebeforegst<=0)
{
cout<<"enter valid no";
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

}
if (gstrate<0||gstrate>28)
{
cout<<"enter valid gst rate";
return 1 ;
}

gstamount= pricebeforegst*(gstrate/100);
priceaftergst=pricebeforegst+gstamount;
cout<<"price after gst is :"<<priceaftergst;
return 0;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Question 2
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter 3 numbers";
cin>>a>>b>>c;

if(a>=b&&a>=c){
b>=c?cout<<"2nd largest "<<b:cout<<"2nd largest "<<c;
}
else if(b>=a&&b>=c){
a>=c?cout<<"2nd largest "<<a:cout<<"2nd largest "<<c;
}
else{
a>=b?cout<<"2nd largest "<<a:cout<<"2nd largest "<<b;
}

return 0;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Question 3

#include <iostream>
#include <cmath>

Using namespace std;

Int main() {
Float a, b, c, discriminant, root1, root2, realPart, imagPart;

Cout << “Enter coefficients a, b, and c: “;


Cin >> a >> b >> c;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Discriminant = b * b – 4 * a * c;

If (discriminant > 0) {
// Real and different roots
Root1 = (-b + sqrt(discriminant)) / (2 * a);
Root2 = (-b – sqrt(discriminant)) / (2 * a);
Cout << “Roots are real and different:\n”;
Cout << “Root 1 = “ << root1 << endl;
Cout << “Root 2 = “ << root2 << endl;
} else if (discriminant == 0) {
// Real and equal roots
Root1 = root2 = -b / (2 * a);
Cout << “Roots are real and equal:\n”;
Cout << “Root 1 = Root 2 = “ << root1 << endl;
} else {
// Complex roots
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
cout << “Roots are complex:\n”;
cout << “Root 1 = “ << realPart << “ + “ << imagPart << “I” << endl;
cout << “Root 2 = “ << realPart << “ – “ << imagPart << “I” << endl;
}

Return 0;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Question 4
#include <iostream>

using namespace std;

int main() {
const float base_bill = 100.0;
const float unit_price = 10.0;

int units_consumed;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

float total_bill, discount, net_bill;

cout << "Enter the number of units consumed: ";


cin >> units_consumed;

total_bill = base_bill + (units_consumed * unit_price);

// Calculate discount based on bill amount


if (total_bill < 200) {
discount = 0;
} else if (total_bill <= 500) {
discount = total_bill * 0.05;
} else if (total_bill <= 800) {
discount = total_bill * 0.1;
} else if (total_bill <= 1100) {
discount = total_bill * 0.15;
} else {
discount = total_bill * 0.2;
}

net_bill = total_bill - discount;

cout << "\nInitial Base Bill: Rs. " << base_bill << endl;
cout << "Total Bill Amount: Rs. " << total_bill << endl;
cout << "Discount Percentage: " << (discount / total_bill) * 100 << "%" << endl;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cout << "Discount Amount: Rs. " << discount << endl;
cout << "Net Bill Amount: Rs. " << net_bill << endl;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy