Introduction to Programming with C++, 3rd edition
Introduction to Programming with C++, 3rd edition
3rd Edition
ISBN: 9780133377477
Author: Y. Daniel Liang
Publisher: Pearson Academic Computing
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 3, Problem 16CP

(a)

Explanation of Solution

Given: The following code:

if (number % 2 == 0)

cout << number << " is even";

if (number % 5 == 0)

cout << number << " is multiple of 5";

To find: The output of the code.

Solution:

When the value of the number is 14.

The output of the code will be “14 is even�. In this case, both the ‘if’ statements will be evaluated but only the first ‘if’ statement will be executed.

// Header files

#include

// Header files

#include

//Declaring the namespace

using namespace std;

// Declaring the main of code

int main() {

// Declaring the variable.

int number=14;

// Using the if-statement.

if(number % 2 == 0)

// Displays the message.

cout << number << " is even";

// Using the if-statement.

if(number % 5 == 0)

// Displays the message.

cout << number << " is multiple of 5";

// Using return statement

return 0;

}

When the value of the number is 15.

The output of the code will be “15 is multiple of 5�. In this case, both the ‘if’ statements will be evaluated but only the second ‘if’ statement will be executed.

// Header files

#include

// Header files

#include

//Declaring the namespace

using namespace std;

// Declaring the main of code

int main() {

// Declaring the variable...

(b)

Explanation of Solution

Given: The following code:

if (number % 2 == 0)

cout << number << " is even";

else if (number % 5 == 0)

cout << number <<" is multiple of 5";

To find: The output of the code.

Solution:

When the value of the number is 14.

The output of the code will be “14 is even�. In this case, the ‘else-if’ statement will be evaluated only when the condition of the ‘if’ statement is false. Here, the condition of the ‘if’ statement is true. Hence, only the ‘if’ statement will be executed.

// Header files

#include

// Header files

#include

//Declaring the namespace

using namespace std;

// Declaring the main of code

int main() {

// Declaring the variable.

int number=14;

// Using the if-statement.

if(number % 2 == 0)

// Displays the message.

cout << number << " is even";

// Using the else-if-statement.

else if(number % 5 == 0)

// Displays the message.

cout << number << " is multiple of 5";

// Using return statement

return 0;

}

When the value of the number is 15.

The output of the code will be “15 is multiple of 5�. In this case, the ‘else-if’ statement will be evaluated only when the condition of the ‘if’ statement is false. Here, the condition of the ‘if’ statement is false. Hence, only the ‘else-if’ statement will be executed.

// Header files

#include

// Header files

#include

//Declaring the namespace

using namespace std;

// Declaring the main of code

int main() {

// Declaring the variable...

Blurred answer
Students have asked these similar questions
Using R language
Using R language
Using R language

Chapter 3 Solutions

Introduction to Programming with C++, 3rd edition

Ch. 3 - What is wrong in the following code?...Ch. 3 - Show the output of the following code: Ch. 3 - Which of the following statements are equivalent?...Ch. 3 - Prob. 14CPCh. 3 - Are the following statements correct? Which one is...Ch. 3 - Prob. 16CPCh. 3 - Are the following statements equivalent?Ch. 3 - Prob. 18CPCh. 3 - a. How do you generate a random integer i such...Ch. 3 - Write an expression that obtains a random between...Ch. 3 - Prob. 21CPCh. 3 - (a) Write a Boolean expression that evaluates to...Ch. 3 - (a) Write a Boolean expression for x54.5. (b)...Ch. 3 - To test whether is between 10 and 100, which of...Ch. 3 - Are the following two expressions the same? Ch. 3 - What is the value of the expression Ch. 3 - Suppose, when you run the program, you enter the...Ch. 3 - Write a Boolean expression that evaluates to true...Ch. 3 - Write a Boolean expression that evaluates to true...Ch. 3 - Write a Boolean expression that evaluates to true...Ch. 3 - Write a Boolean expression that evaluates to true...Ch. 3 - What data types are required for a switch...Ch. 3 - What is y after the following switch statement is...Ch. 3 - What is x after the following if-else statement is...Ch. 3 - Suppose that, when you run the following program,...Ch. 3 - Rewrite the following if statements using the...Ch. 3 - Rewrite the following conditional expressions...Ch. 3 - List the precedence order of the Boolean...Ch. 3 - True or false? All the binary operators except =...Ch. 3 - Evaluate the following expressions:...Ch. 3 - ...Ch. 3 - (Algebra: solve quadratic equations) The two roots...Ch. 3 - (Check numbers) Write a program that prompts the...Ch. 3 - (Algebra: solve 22 linear equations) You can use...Ch. 3 - (Check temperature) Write a program that prompts...Ch. 3 - (Find future dates) Write a program that prompts...Ch. 3 - (Health application: BMI) Revise Listing 3.2,...Ch. 3 - Prob. 7PECh. 3 - (Financial application: monetary units) Modify...Ch. 3 - (Find the number of days in a month) Write a...Ch. 3 - (Game: addition quiz) Listing 3.4,...Ch. 3 - (Cost of shipping) A shipping company uses the...Ch. 3 - (Game: heads or tails) Write a program that lets...Ch. 3 - Prob. 13PECh. 3 - (Game: lottery) Revise Listing 3.7, Lottery.cpp,...Ch. 3 - (Game: scissor, rock, paper) Write a program that...Ch. 3 - (Compute the perimeter of a triangle) Write a...Ch. 3 - (Science: wind-chill temperature) Programming...Ch. 3 - (Game: addition for three numbers) Listing 3.4,...Ch. 3 - (Geometry: point in a circle?) Write a program...Ch. 3 - (Geometry: point in a rectangle?) Write a program...Ch. 3 - (Game: pick a card) Write a program that simulates...Ch. 3 - (Geometry: intersecting point) Two points on line...Ch. 3 - (Geometry: points in triangle'?) Suppose a right...Ch. 3 - (Use the operators) Write a program that prompts...Ch. 3 - (Geometry: two rectangles) Write a program that...Ch. 3 - (Geometry: two circles) Write a program that...Ch. 3 - Prob. 27PECh. 3 - (Financials: currency exchange) Write a program...Ch. 3 - Prob. 29PECh. 3 - (Financial: compare costs) Suppose you shop for...Ch. 3 - Prob. 31PECh. 3 - (Algebra: slope-intercept form) Write a program...Ch. 3 - (Science: day of the week)) Zeller's congruence is...Ch. 3 - Prob. 34PECh. 3 - (Business: check ISBN-10) An lSBN-10...Ch. 3 - (Palindrome number) Write a program that prompts...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning