0% found this document useful (0 votes)
16 views6 pages

Control Statements Lab Solutions - 25

The document contains multiple C++ programming tasks including checking for palindromes, calculating sums and averages of integers, creating a basic calculator, calculating the product of odd integers, verifying multiplication answers, and finding the two largest values from a set of numbers. Each task is accompanied by a code snippet that demonstrates the solution. The programs utilize various control structures and input/output operations in C++.

Uploaded by

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

Control Statements Lab Solutions - 25

The document contains multiple C++ programming tasks including checking for palindromes, calculating sums and averages of integers, creating a basic calculator, calculating the product of odd integers, verifying multiplication answers, and finding the two largest values from a set of numbers. Each task is accompanied by a code snippet that demonstrates the solution. The programs utilize various control structures and input/output operations in C++.

Uploaded by

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

1.

A palindrome is a number or a text phrase that reads the same backwards


as forwards. For example, each of the following three-digit integers is a
palindrome: 121,363. Write a program that reads in a three-digit integer and
determines whether it is a palindrome. (Hint: Use the division and modulus
operators to separate the number into its individual digits)

#include<iostream>
using namespace std;
int main() {
int number, firstDigit, secondDigit,thirdDigit;

cout << "Enter a three-digit number: ";


cin >> number;

firstDigit = number / 100;

thirdDigit = number % 10;

if (firstDigit == thirdDigit)
cout << number << " is a palindrome" << endl;
else
cout << number << " is not a palindrome" << endl;

return 0;
}
2. Write a program that inputs three integers from the keyboard and prints
the sum, average, product, smallest and largest of these numbers.

#include<iostream>
using namespace std;
int main() {
int num1, num2, num3, smallest, largest; // declaration

cout << "Input three different integers: "; // prompt


cin >> num1 >> num2 >> num3; // input

largest = num1; // assume first number is largest

if (num2 > largest) // is num2 larger?


largest = num2;

if (num3 > largest) // is num3 larger?


largest = num3;

smallest = num1; // assume first number is smallest

if (num2 < smallest)


smallest = num2;

if (num3 < smallest)


smallest = num3;

cout << "Sum is " << num1 + num2 + num3


<< "\nAverage is " << (num1 + num2 + num3) / 3
<< "\nProduct is " << num1 * num2 * num3
<< "\nSmallest is " << smallest
<< "\nLargest is " << largest << endl;

return 0;
}
3.Create a basic calculator using switch-case statements.

#include <iostream>
using namespace std;
int main()
{
char oper;
float num1, num2;
cout << "enter an operator (+,-,*,/):";
cin >> oper;
cout << "enter two numbers" << endl;
cin >> num1 >> num2;
switch (oper) {
case'+':
cout << num1 << "+" << num2 << "=" << num1 + num2;
break;
case'-':
cout << num1 << "-" << num2 << "=" << num1 - num2;
break;
case'*':
cout << num1 << "*" << num2 << "=" << num1 * num2;
break;
case'/':
cout << num1 << "/" << num2 << "=" << num1 / num2;
break;
default:
cout << "Error!The operator isn’t correct";
break;
}
return 0;
}
4. Write a program that calculates and prints the product of the odd
integers from 1 to 15.
#include <iostream>

using namespace std;

int main()
{
int product = 1;
int i = 3;
while (i <= 15)
{
product *= i;
i += 2;
}

/*
for (int i = 3; i <= 15; i += 2)
product *= i;

*/

cout << "Product of the odd integers from 1 to 15 is: "


<< product << endl;
}
5. Write a C++ program that prompts the user to enter two numbers. After that, it will ask the user
to calculate the product of those two numbers and input the answer. If the user provides the
wrong answer, it will ask again until the correct answer is given.
#include <iostream>
using namespace std;

int main() {
int num1, num2, answer;

// Ask the user to enter the first and second numbers


cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;

// Ask the multiplication question and repeat until the user answers
correctly
cout << "What is the product of " << num1 << " and " << num2 << "? ";
cin >> answer;

while (answer != num1 * num2) {


cout << "Incorrect answer, please try again!" << endl;
cout << "What is the product of " << num1 << " and " << num2 << "? ";
cin >> answer;
}

cout << "Correct answer! The product of " << num1 << " and " << num2 << " is
" << num1 * num2 << "." << endl;

return 0;
}
6.Find the two largest values among the 10 numbers. Note: You must input
each number only once.

#include <iostream>

using namespace std;

int main()
{
int counter = 0, number, largest, secondLargest = 0;

cout << "Enter the first number: ";


cin >> largest;

while (++counter < 10) {


cout << "Enter next number: ";
cin >> number;

if (number > largest) {


secondLargest = largest;
largest = number;

}
else if (number > secondLargest)
secondLargest = number;

cout << "\nLargest is " << largest


<< "\nSecond largest is " << secondLargest << endl;

return 0;
}

You might also like