Control Statements Lab Solutions - 25
Control Statements Lab Solutions - 25
#include<iostream>
using namespace std;
int main() {
int number, firstDigit, secondDigit,thirdDigit;
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
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>
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;
*/
int main() {
int num1, num2, answer;
// Ask the multiplication question and repeat until the user answers
correctly
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>
int main()
{
int counter = 0, number, largest, secondLargest = 0;
}
else if (number > secondLargest)
secondLargest = number;
return 0;
}