You are on page 1of 10

Pimpri Chinchwad Education Trust’s Record No.

: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Assignment No. 02
Aim: A hotel has a pricing policy as follows:-
Stay for 2 person : 2500Rs. per night
Stay for 3 person : 3500Rs. per night
Stay for 4 person : 4500Rs. per night
Additional person : 1000Rs. per person per night
If the customer is staying on company business tour, there is a 20% discount. Take the
number of people, number of night staying, if it's business tour or not as input from user.
Write a program to calculate and print the cost of the room.
Objective: To demonstrate operators in C programming and perform simple arithmetic
operations.
Outcomes: Students will be able to
 Understand how to use arithmetic operator to find solution of the problem
 Use decision making statements

Theory:
Conditional Control Statement

Conditional Statements in C programming are used to make decisions based on the conditions.
Conditional statements execute sequentially when there is no condition around the statements. If
you put some condition for a block of statements, the execution flow may change based on the
result evaluated by the condition. This process is called decision making in 'C.'

In 'C' programming conditional statements are possible with the help of the following two
constructs:

1. If statement

2. If-else statement

It is also called as branching as a program decides which statement to execute based on the result
of the evaluated condition.
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

if :- There are several variations of “ if” statement:-

 if statement:-
- Used to specify conditional execution of a program statement or group of statements.

- In a simple ‘if’ statement, a condition is tested

- If the condition is true, a set of statements that are inside a pair of (opening and closing) curly
braces are executed

- If the condition is false, the statements are not executed and the program

control goes to the next statement that immediately follows the if block

Syntax:-

if (condition)
{
Program statements;
}

 if-else statement:-
- It directs the computer to select a sequence of one or more instructions based on the result of
the comparison.

Syntax:
if (condition)
{
Statement(s) to be executed when condition evaluates to true;
}
else
{
Statement(s) to be executed when condition evaluates to false;
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

 if-else-if statement:
- It is also called as Nested if.
Syntax:
if (condition)
{
Statement(s) to be executed when condition evaluates to true;
}
else if (condition)
{
Statement(s) to be executed when condition evaluates to false;
}
else
{
Statement(s )to be executed when condition evaluates to false;
}
Example:
What does the following code snippet do?
if (iNum1 == iNum2 )
{
printf("The two numbers are equal");
}
else
{
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

if (iNum1 > iNum2)


{
printf("iNum1 is greater than iNum2");
}
else
{
printf("iNum2 is greater than iNum1");
}
}
Flowchart for if-else statement

+
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Relational Operators

C has six relational operators that can be used to formulate a Boolean expression for making a
decision and testing conditions, which returns true or false :

< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equal to

!= not equal to

Notice that the equal test (==) is different from the assignment operator (=) because it is one of
the most common problems that a programmer faces by mixing them up.

For example:

int x = 41;
x =x+ 1;
if (x == 42) {
printf("You succeed!");}

Output :
You succeed
Keep in mind that a condition that evaluates to a non-zero value is considered as true.
For example:
int present = 1;
if (present)
printf("There is someone present in the classroom \n");
Output :
There is someone present in the classroom
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Flowchart:

Pseudo code:
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Step 1: Start
Step 2: Read no_of_guest, no_of_nights, is_business
Step 3 : disc= 0
Step 4 : if no_of_guest<=2 , charges= 2500*no_of_night , go to step 8
Step 5: if no_of_guest==3, charges= 3500*no_of_night, go to step 8
Step 6 : if no_of_guest==4, charges= 4500*no_of_night, go to step 8
Step 7: if no_of_guest>4, extra_guest= no_of_guest-4,
charges= (4500+(extra_guest*1000))*no_of_night, go to step 8
Step 8 : if is_business, disc = disc = charges*20/100
Step 9 : final_charge= charges-disc
Step 10 : Display disc, final_charge
Step 11: Stop
Code:-
#include<stdio.h>
//Harshit khnadelwal
//L1271
//Assignment2
/* A hotel has a pricing policy as follows:-
Stay for 2 person : 2500Rs. per night
Stay for 3 person : 3500Rs. per night
Stay for 4 person : 4500Rs. per night
Additional person : 1000Rs. per person per night
If the customer is staying on company business tour, there is a 20% discount. Take the
number of people, number of night staying, if it&#39;s business tour or not as input from user.
Write a programt to calculate and print the cost of the room.*/

int main()
{
// price per night
int base_price = 2500, //price for 2 or less person
Extra_person = 1000; // price per additional person
int Nights,People,Total,Per_night_cost,On_business_tour;
printf("Hello I am your pricing bot\n");
printf("Pls tell the no.of people: ");
scanf("%d", &People);
printf("How many nights do want to stay: ");
scanf("%d",&Nights);
printf("are u staying on company business tour (1/0): ");
scanf("%d",&On_business_tour);
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Per_night_cost= base_price;
if (People>2)
{
Per_night_cost=base_price + (People-2)*Extra_person;

}
Total= Per_night_cost*Nights;
if (On_business_tour);
{
int discount = Total*0.2;
printf("You get a discout of 20 percent for company business tour worth Rs.%i is applied\n",discount);
Total=Total-discount;
}
printf("total cost of room:%i",Total);
return 0;
}

Test cases :
no_of_guest = 2, no_of_nights=2, is_business=1
disc = 1000
final_charge = 4000
Output:-

Conclusion: The program is correctly calculating the fare of the hotel room as per given
condition.
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

Questions
1.What is if else if ladder? Explain with an example.
Pimpri Chinchwad Education Trust’s Record No.: ACAD/R/023
Revision: 01
Pimpri Chinchwad College of Engineering
Date: 15/09/2020

Department: FY B. Tech. Academic Year: 2020-2021 Sem : I

2.How would you check more than one conditions in a ‘if ‘statement ? Give example.

You might also like