The document provides explanations and examples of various C++ programming concepts including:
1. The ternary operator ?: and how it can be used as a conditional expression to assign a value based on a true/false test.
2. The break and continue keywords in loops, where break exits the entire loop and continue skips to the next iteration.
3. Switch-case statements for comparing a variable to constants and executing code for the matching case.
4. Nested loops for problems that require multiple iterations, like printing nested series sums or patterns.
5. Additional examples are provided for problems involving input/output, loops, conditional statements, series calculations, and determining special numbers.
1. For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- info@cpphomeworkhelp.com or
reach us at :- https://www.cpphomeworkhelp.com/
2. Ternary operator:
The conditional operator ? : is also another way to evaluate conditions. It operates
on three operands, and is thus known as a ternary operator. Here is an example of
its usage:
a = x < y ? x : y;
In this example, if the test expression evaluates to true – i.e. if x < y – then the
variable a will be assigned the value that the variable x contains. Otherwise, else it
will be assigned the value of the variable y.
The whole expression containing this operator (the right side of the assignment
statement) is called a conditional expression, and the sub-expression before the
question mark is called the test expression. If this test expression is true, the entire
expression takes on the value of the expression immediately after the question
mark. Otherwise, it takes on the value of the expression following the colon.
Usually you only want to use this construct for choosing between two simple
expressions for a particular value (both choices must be the same data type). Doing
otherwise could run afoul of the syntactic rules of the operator and lead to syntax
errors. You cannot, for instance, make the two options two different return
statements.
cpphomeworkhelp.com
3. 1. Without running, explain what the following set of statements will do:
bool input;
int a;
cin >> input >> a;
char *choice = ( input ? "Hello World!" : "I love C++!" );
if( ( input ? a > 1 : a < 1 ) ) {
cout << "Why did you pick "" << choice << ""?" << endl;
}
else {
cout << "Yay, you picked " << choice;
}
2. Convert this for loop to a do-while loop.
int sum = 0;
for( int i = 0; i < 5; i++ )
sum += i;
cout << sum;
cpphomeworkhelp.com
4. break and continue:
Two keywords often used with loops are break and continue.
The break keyword causes the entire construct to exit, and control passes to
the statement immediately after the body of the construct.
For example:
for( n = 10; n > 0; n-- )
{
cout << n << ", ";
if( n == 7 )
break;
}
In this example, only the first few values of n (the numbers 10, 9, 8) will be
executed. As soon as the value of n becomes 7, the loop will exit.
The continue statement causes the program to skip the rest of the loop in the
current iteration as if the end of the statement block had been reached,
causing it to jump to the start of the next iteration:
cpphomeworkhelp.com
5. for( int n = 10; n > 0; n-- )
{
if ( n == 7 )
continue;
cout << n << ", ";
}
In this example, 10, 9, and 8 will again be displayed, but as soon as the value of
n becomes 7, control will be passed to the beginning of the loop body. The rest
of that iteration is skipped, and the rest of the countdown till 1, will be displayed
(so the final output will be “10, 9, 8, 6, 5, 4, 3, 2, 1”). When you place a continue
command within a for loop, the increase statement (in this case, n--) is executed
before the loop iterates again.
3. Write a program to take in a number from a user, find its reciprocal and add it
to a running sum (the running sum will be 0, when the loop initially starts). The
program should repeat this procedure 10 times. However, if the user enters 0,
the loop should exit, and if the user enters 1, nothing should be added. Print the
final sum at the end of the program.
cpphomeworkhelp.com
6. switch statements:
In lecture, the main type of conditional construct discussed was if-else. There is
another type of construct, which is also sometimes used, called the switch-case
construct. In this construct, the value of a variable is compared to a set of
constants, and when a corresponding match is found, the statements
associated with that case are executed. It is like an if-else construct that can
only check for equality
For example:
switch(number)
{ case 3:
cout << "Red"; break;
case 2:
cout << "Orange"; break;
case 7:
cout << "Black"; break;
default:
cout << "None of the above";
}
cpphomeworkhelp.com
7. In this switch statement, the value of the variable numberis compared with 3, 2
and 7, and if a match is formed, the corresponding color is printed. If no match
is found then “None of the above” is printed. (The break keyword is required to
end each caseblock except the last.)
4.Using the switch-case construct write a program to input two numbers,
display the following menu, and then print according to the user’s choice:
1.Difference of two numbers
2.Quotient of two numbers
3. Remainder of two numbers
For example, if the user inputs 1, then the difference of the two numbers should
be printed.
5.Find the sum of the first n terms of the following series, where n is a number
entered by the user:
6. Print the following pattern, using horizontal tabs to separate numbers in the
same line. Let the user decide how many lines to print (i.e. what number to
start at).
cpphomeworkhelp.com
8. 5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Series:
As specified in lecture, nested loops are used when for one repetition of a
process, many repetitions of another process are needed. Similar to patterns,
nested loops can be used to print the sums of nested series. For example the
sum of the series 1+(1+2) +(1+2+3) +... Can
befoundbyaddingonetoarunningsumforeach execution of the inner loop, instead
of printing them as you would for a pattern. As with patterns, the number of
times the inner loop runs in this case depends on the value of the outer loop.
7. Write a program that inputs two numbers x and n, and find the sum of the
first n terms of the following series: x +(x + x2)+(x + x2 + x3) + ....
cpphomeworkhelp.com
9. 8. An Angstrom number is one whose digits, when cubed, add up to the number
itself. For instance, 153 is an Angstrom number, since 13 + 53 + 33 = 153 .
Write a program to input a number, and determine whether it is an Angstrom
number or not.
Hint: The modulus operator (%) will be useful here.
cpphomeworkhelp.com
10. Problem 1:
i. The program takes 2 inputs from the user, and stored it in variables input
and a.
ii. If the user has inputted the value of input as 1
a. the string choice will be assigned value “Hello World”.
b. In the if-else construct, the condition returned to the if statement will be
a>1. Now, if this condition is true (i.e if a>1), then
Why did you pick "Hello World"?
will be displayed.
c. If this returned condition is false, (i.e if a<1), then
Yay you picked Hello World
will be displayed.
iii. If the user has inputted the value of input as 0
a. the string choice will be assigned value “I love C++”.
b. In the if-else construct, the condition returned to the if statement will be
a<1. Now, if this condition is true (i.e if a<1), then
cpphomeworkhelp.com
11. Why did you pick "I love C++"?
will be displayed.
c. If this returned condition is false, (i.e if a>1), then
Yay you picked I love C++
will be displayed.
Problem 2:
int sum = 0, i = 0;
do
{
sum += i++;
} while ( i < 5 );
Problem 3:
#include <iostream>
int main()
{
float a, sum=0;
cpphomeworkhelp.com
12. for(int i=1;i<=10;i++)
{
cout << "Enter the number whose reciprocal has to be added to the
series:";
cin >> a;
if( a == 0 )
break;
else if ( a == 1)
continue;
else
sum += 1 / a;
}
Problem 4:
#include<iostream>
using namespace std;
int main()
{
int choice, a, b;
cout << "Enter two numbers:";
cin >> a >> b; cout << "1. Difference of two numbers" << endl
cpphomeworkhelp.com
13. << "2. Quotient of two numbers" << endl
<< "3. Remainder of two numbers" << endl
<< "Enter your choice:";
cin >> choice;
switch(choice)
{
case 1:
if ( a > b ) // Optional check
cout << "Difference is " << a - b << endl;
else
cout << "Difference is " << b - a << endl;
break;
case 2:
if ( a > b ) // Optional check
cout << "Quotient is " << a / b << endl;
else
cout << "Quotient is " << b / a << endl;
break;
case 3:
if ( a > b ) // Optional check
cout << "Remainder is " << a % b << endl;
cpphomeworkhelp.com
14. else
cout << "Remainder is " << b % a << endl;
break;
default:
cout << "Not an option!" << endl;
}
return 0;
}
Problem 5:
#include <iostream>
// For the second possibility below:
#include <cmath>
using namespace std;
int main()
{
int n;
float sum = 0, denom = 1;
cout << "Enter the number of terms of the series:" << endl;
cpphomeworkhelp.com
15. cin >> n;
for (int i = 1; i<=n; i++, denom += 2)
{
if( i % 2 == 0 )
sum += 1 / (denom * denom );
else
sum -= 1 / (denom * denom );
}
// or sum += pow(-1.0, i) * 1/( term * term)
// Also, instead of adding to denom each time, the denominator can be
// calculated as (2 * i – 1)
cout<<"The sum is:"<<sum<<endl;
return 0;
}
Problem 6:
#include <iostream>
using namespace std;
int main()
{
cpphomeworkhelp.com
16. int n;
float sum=0,term=1;
cout << "Enter the number of rows of the pattern:";
cin>>n; for (int i=n; i > 0; i--) {
for(int j=n; j>=i; j--)
cout << j << 't'; cout << endl;
}
return 0;
}
Problem 7:
#include<iostream>
// For the second possibility below:
#include <cmath>
using namespace std;
int main()
{
cpphomeworkhelp.com
17. int n,x, sum = 0;
cout << "Enter the value for term 'x': ";
cin >> x;
cout << "Enter the sub-series of the main series: ";
cin >> n;
for(int i=1; i<=n; i++)
{
int term=1;
for(int j=1; j<=i; j++)
{
term *= x;
sum += term;
}
}
/*
Alternate possibility:
for(int i=1; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
sum += pow(x, b);
}
} cpphomeworkhelp.com
18. */
cout << "The sum is:" << sum << endl;
return 0;
}
Problem 8:
#include <iostream>
using namespace std;
int main()
{
int num;
float sum=0, term=1;
cout<<"Enter the number:";
cin>>num;
for(int n=num; n > 0; n /= 10 )
{
int digit = n % 10;
sum += digit * digit * digit;
}
cpphomeworkhelp.com
19. if( sum == num )
cout << "This is an Angstrom number" << endl;
else
cout << "This is not an Angstrom number" << endl;
return 0;
}
cpphomeworkhelp.com