Q18: Implement a C++ program that converts temperatures between Celsius and Fahrenheit. Ask the user for the temperature and the unit, then perform the conversion based on the input. Hint:
(Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32)
(Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32)
#include <iostream>
using namespace std;
int main() {
float c, f;
cout << " value ";
cin >> c;
f = ( c * 9/5 ) + 32;
cout << " in F the temp is :" << f <<endl;
return 0;
}
👍1
Q19: Create a C++ program that takes two integers as input and checks whether the first number is divisible by the second one. Provide appropriate messages based on the result.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
if (num2 != 0)
{
if (num1 % num2 == 0)
{
cout << num1 << " is divisible by " << num2 << endl;
}
else
{
cout << num1 << " is not divisible by " << num2 << endl;
}
}
else
{
cout << "Error: Division by zero is not allowed." << endl;
}
return 0;
}
👍1
Q20: Write a C++ program that takes three numbers as input and determines the largest among them using conditional statements
#include <iostream>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter num1 : ";
cin >> num1;
cout << "Enter num2 : ";
cin >> num2;
cout << "Enter num3 : ";
cin >> num3;
if (num1 >= num2 && num1 >= num3) {
cout << "The largest number is : " << num1 << endl;
}
else if (num2 >= num1 && num2 >= num3)
{
cout << "The largest number is : " << num2 << endl;
}
else
{
cout << "The largest number is : " << num3 << endl;
}
return 0;
}
👍2
Q21: Create a C++ program that takes three angles as input and classifies the triangle based on the angle values (acute, obtuse, or right-angled). determines the largest among them using conditional statements
#include <iostream>
using namespace std;
int main()
{
int angle1, angle2, angle3;
cout << "Enter the first angle: ";
cin >> angle1;
cout << "Enter the second angle: ";
cin >> angle2;
cout << "Enter the third angle: ";
cin >> angle3;
if (angle1 + angle2 + angle3 == 180)
{
if (angle1 < 90 && angle2 < 90 && angle3 < 90)
{
cout << "The triangle is acute-angled." << endl;
}
else if (angle1 == 90 || angle2 == 90 || angle3 == 90)
{
cout << "The triangle is right-angled." << endl;
}
else
{
cout << "The triangle is obtuse-angled." << endl;
}
}
else
{
cout << "Invalid input: The angles do not form a triangle." << endl;
}
return 0;
}
👍1
Q22: Write a C++ program that calculates the total cost after applying a discount based on the purchase amount. For example, provide a 10% discount if the purchase amount is above $100.
#include <iostream>
using namespace std;
int main()
{
int purchaseamount, discount, totalcost;
cout<<" enter the purchaseamount: ";
cin>>purchaseamount;
if ( purchaseamount > 100)
{
discount = 0.10 * purchaseamount;
}
else
{
discount = 0;
}
totalcost = purchaseamount - discount;
cout<< "Discount applied: $"<<discount<<endl;
cout<< "Total cost after discount: $"<<totalcost<<endl;
return 0;
}
👍1
Q23: C++ code to calculate the factorial of any entered number.
#include <iostream>
using namespace std;
int main()
{
int a , fac;
cout << "enter the number (a) : ";
cin >> a;
fac = 1;
for ( int i =1; i <= a ; i++)
{
fac = fac * i;
}
cout << "the fac of the entered number is : "<<fac<<endl;
return 0;
}
👍1
Q24: C++ code to calculate the power of any entered number (the base and exponent should be entered by user).
#include <iostream>
using namespace std;
int main()
{
float base, exponent, result;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
result = 1;
for(int i=0;i<exponent;i++)
{
result*=base;
}
cout << base << " raised to the power of " << exponent << " is: " << result << endl;
return 0;
}
👍1
Q25: Write a program that calculates and prints the sum of the even integers from 2 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not.
#include <iostream>
using namespace std;
int main()
{
char input;
do {
int num, sum_even = 0;
cout << "enter the number : ";
cin >> num;
for (int b = 2; b <= num; b += 2)
{
sum_even += b;
}
cout << " sum of even numbers from 2 to "<<num<<" is :"<< sum_even<< endl;
cout << " Do you want to terminate the program or not? ( y/n):" ;
cin >> input;
}
while (input != 'y');
return 0;
}
❤1👍1
Q26: Write a C++ program to print the sequence of numbers (101, 111, 112 … 130), except number 107 and 125.
#include <iostream>
using namespace std;
int main()
{
for ( int i = 101; i <= 130; i++ )
{
if ( i!=107 && i!=125)
cout << i << endl;
}
return 0;
}
❤2👍1
Q27: Write a program that calculates and prints the product of odd integers from 1 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not.
#include <iostream>
using namespace std;
int main()
{
char input;
do {
int num, product_odd = 1;
cout << "enter the value of m : ";
cin >> num;
for (int b = 1; b <= num; b += 2)
{
product_odd *= b;
}
cout << " product of odd numbers from 1 to"<<num<<":"<< product_odd<< endl;
cout << " Do you want to terminate the program or not? ( y/n):" ;
cin >> input;
}
while (input != 'y');
return 0;
}
👍1
Q28: Write a program which takes PIN number of the user as input and then verifies his pin. If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.
#include <iostream>
using namespace std;
int main()
{
const int MAX_ATTEMPTS = 4;
int pinAttempts = 0;
int enteredPIN;
int validPINs[] = {1234, 5678, 9876, 5432, 1111};
for (int i = 0; i < MAX_ATTEMPTS; ++i)
{
cout << "Enter your PIN: ";
cin >> enteredPIN;
bool pinVerified = false;
for (int j = 0; j < 5; ++j)
{
if (enteredPIN == validPINs[j])
{
pinVerified = true;
break;
}
}
if (pinVerified)
{
cout << "PIN verified. Welcome!" << endl;
break;
} else
{
cout << "Incorrect PIN. Please try again." << endl;
++pinAttempts;
if (pinAttempts == MAX_ATTEMPTS)
{
cout << "Limit expired. Exiting program." << endl;
break;
}
}
}
return 0;
}
Q28: Write a program which takes PIN number of the user as input and then verifies his pin.
If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.
If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.
#include <iostream>
using namespace std;
int main() {
// Assume 5 random PIN numbers
int pinNumbers[5] = {1234, 5678, 9876, 4321, 7890};
int userPIN;
int attempts = 0;
// Loop for maximum 4 attempts
for (int i = 0; i < 4; ++i) {
cout << "Enter your PIN number: ";
cin >> userPIN;
// Check if the entered PIN matches any of the stored PINs
bool pinVerified = false;
for (int j = 0; j < 5; ++j) {
if (userPIN == pinNumbers[j]) {
pinVerified = true;
break;
}
}
if (pinVerified) {
cout << "PIN verified" << endl;
cout << "Welcome" << endl;
break;
} else {
cout << "Invalid PIN. Please try again." << endl;
attempts++;
}
}
// If the user exceeds 4 attempts
if (attempts == 4) {
cout << "Limit expired" << endl;
}
return 0;
}
Q29: Write a C++ program to calculate the summation of the following sequence for nth terms.
𝑆𝑢𝑚 = 1/1 + 2/4 + 3/9 + ....
𝑆𝑢𝑚 = 1/1 + 2/4 + 3/9 + ....
#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0;
cout<<" enter the value of n : ";
cin>>n;
for( int i = 1; i <= n; i++)
{
sum += float (i) / (i*i);
}
cout<<" the summation the following sequence for "<<n<<" is : "<<sum<<endl;
return 0;
}
👍1
Q30: C++ code to find the summation of the following series.
Sum = a + a^2 + a^3 + ... + a^n
Sum = a + a^2 + a^3 + ... + a^n
#include <iostream>
using namespace std;
int main() {
int a, n;
int sum = 0;
cout << "Enter the value of 'a': ";
cin >> a;
cout << "Enter the value of 'n': ";
cin >> n;
// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
int term = 1;
for (int j = 1; j <= i; ++j) {
term *= a;
}
sum += term;
}
cout << "The summation of the series is: " << sum << endl;
return 0;
}
❤1
Q31: C++ code to find the summation of the following series for nth terms.
𝑆𝑢𝑚 = 1/a^1 + 2/a^2 + 3/a^3 + ....
𝑆𝑢𝑚 = 1/a^1 + 2/a^2 + 3/a^3 + ....
#include <iostream>
using namespace std;
int main() {
int a, n;
float sum = 0.0;
float power_of_a = 1.0;
cout << "Enter the value of 'a': ";
cin >> a;
cout << "Enter the value of 'n': ";
cin >> n;
// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
power_of_a *= a; // Update power_of_a by multiplying it by a
sum += float(i) / power_of_a;
}
cout << "The summation of the series up to the " << n << "th term is: " << sum << endl;
return 0;
}
❤1
Q32: C++ code to find the summation of the following series.
Sum = 1 + 2! + 3! + ⋯ + n!
Sum = 1 + 2! + 3! + ⋯ + n!
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
int factorial = 1;
cout << "Enter the value of 'n': ";
cin >> n;
for (int i = 1; i <= n; ++i) {
factorial *= i;
sum += factorial;
}
cout << "The summation of the series is: " << sum << endl;
return 0;
}
❤1👍1
Q33: Grades in the programming lab are classified according to letters A case of Excellent, B case of Well done, C case of done, D case of Pass and F case of Try again.
Write C++ code to implement this logic using Switch statement.
Write C++ code to implement this logic using Switch statement.
#include <iostream>
using namespace std;
int main ()
{
char grade ;
cout << " take one from this ( A , B, C, D, F) ";
cin >> grade;
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
cout << "Well done" << endl;
break;
case 'C' :
cout << "done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
❤1
Q34: Create a C++ program that takes five integers as input and determines whether their product is positive or negative.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4, num5;
int product;
cout << "Enter five integers: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
product = num1 * num2 * num3 * num4 * num5;
if (product > 0)
{
cout << "The product is positive." << endl;
}
else if (product < 0)
{
cout << "The product is negative." << endl;
}
else
{
cout << "The product is zero." << endl;
}
return 0;
}
❤2
Q35: Write a C++ program that takes a day number (1-7) as input and prints the corresponding day of the week.
#include <iostream>
using namespace std;
int main()
{
int daynumber;
cout << " enter the day ( 1 -7 ) : ";
cin >> daynumber;
switch (daynumber)
{
case 1:
cout<< " sunday "<<endl;
break;
case 2:
cout<< " monday "<<endl;
break;
case 3:
cout<< " tuseday "<<endl;
break;
case 4:
cout<< " wednesday "<<endl;
break;
case 5:
cout<< " thursday "<<endl;
break;
case 6:
cout<< " friday "<<endl;
break;
case 7:
cout<< " saturday "<<endl;
break;
default:
cout<<" not defined/true "<<endl;
}
return 0;
}
❤1
Q36: Write a C++ program that determines if a person is eligible to vote based on their age. Consider the legal voting age as 18.
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<" enter your age : ";
cin>>age;
if ( age >= 18)
{
cout<< " you are eligible to vote "<<endl;
}
else
{
cout<< " you are not eligible to vote "<<endl;
}
return 0;
}
❤1
Q37: Write C++ program takes an input of a person first name and uses a switch statement to output the corresponding person's age (consider three persons)
#include <iostream>
using namespace std;
int main()
{
char firstName;
cout << "Enter a person's first initial (A/B/C): ";
cin >> firstName;
switch (firstName)
{
case 'A':
case 'a':
cout << "The age of person with first initial A is: 25" << endl;
break;
case 'B':
case 'b':
cout << "The age of person with first initial B is: 30" << endl;
break;
case 'C':
case 'c':
cout << "The age of person with first initial C is: 35" << endl;
break;
default:
cout << "Unknown person" << endl;
}
return 0;
}