



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1.Write a C++ program that takes three numbers as input and displays whether all numbers are equator not by using nested if condition. #include using namespace std; int main() { int num1, num2, num3; // Input three numbers cout << "Enter three numbers: "; cin >> num1 >> num2 >> num3; // Check if all numbers are equal using nested if if (num1 == num2) { if (num2 == num3) { cout << "All numbers are equal." << endl; } else { cout << "All numbers are not equal." << endl; } } else { cout << "All numbers are not equal." << endl; } return 0; }
2. Write a C++ program that prints product of all odd numbers from 1 to 10 using a for loop. #include using namespace std; int main() {
int product = 1; // Loop through numbers from 1 to 10 for (int i = 1; i <= 10; i++) { // Check if the number is odd if (i % 2 != 0) { product *= i; // Multiply the product by the odd number } } // Print the product of all odd numbers cout << "Product of all odd numbers from 1 to 10 is: " << product << endl; return 0; } 3.Write a C++ program using a while loop that takes an integer for variable nValue and returns the sum of all numbers from 1 to the value in nValue. (Hint: For example, nValue=4 should return 10 which is 1+2+3+4) #include using namespace std; int main() { int nValue, sum = 0, i = 1; // Input the value of nValue cout << "Enter a positive integer: "; cin >> nValue; // Check if nValue is positive if (nValue < 1) { cout << "Please enter a positive integer." << endl;
return 0; } 5 .Write a C++ program to read the address of a user and exit when the user enters dot(.) from the keyboard. #include #include using namespace std; int main() { string addressLine; cout << "Enter the address (enter a dot '.' to exit):" << endl; while (true) { getline(cin, addressLine); if (addressLine == ".") { break; // Exit the loop if input is a dot } cout << "You entered: " << addressLine << endl; } cout << "Program exited." << endl; return 0; } 6.Write a C++ program to find out the area of a rectangle and display an error message if one of the sides is zero.
#include using namespace std; int main() { float length, width; // Input the sides of the rectangle cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the width of the rectangle: "; cin >> width; // Check if any side is 0 if (length == 0 || width == 0) { cout << "There is no rectangle because one of the sides is 0." << endl; } else { // Calculate the area float area = length * width; cout << "The area of the rectangle is: " << area << endl; } return 0; } 7.Write a C++ program to get a character from the keyboard as input and determine whether it is a vowel or a consonant. #include using namespace std; int main() {