LAB3
LAB3
Write and run a program that reads an angle (expressed in degrees) and
states in which quadrant the given angle lies. An angle A is said to be in the
first quadrant if it is in the range 0 ≤ A < 90
second quadrant if it is in the range 90 ≤ A < 180
third quadrant if it is in the range 180 ≤ A < 270
and fourth quadrant if it is in the range 270 ≤ A < 360.
#include <iostream>
using namespace std;
int main() {
float angle;
cout << "Enter the angle in degrees: ";
cin >> angle;
if (angle >= 0 && angle < 90) {
cout << "First Quadrant." << endl;
} else if (angle >= 90 && angle < 180) {
cout << "Second Quadrant." << endl;
} else if (angle >= 180 && angle < 270) {
cout << "Third Quadrant." << endl;
} else if (angle >= 270 && angle < 360) {
cout << "Fourth Quadrant." << endl;
} else {
cout << "Out of range." << endl;
}
return 0;
}
Exercise 4. Write and run a program that reads a month from the user and displays the
number of days in that month.
#include <iostream>
using namespace std;
int main() {
int month;
#include <iostream>
using namespace std;
int main() {
int grade;
cout << "Enter the numeric grade: ";
cin >> grade;
int main() {
char code;
cout << "Enter the marriage code (M/m, D/d, W/w): ";
cin >> code;
switch (code) {
case 'M':
case 'm':
cout << "Individual is married" << endl;
break;
case 'D':
case 'd':
cout << "Individual is divorced" << endl;
break;
case 'W':
case 'w':
cout << "Individual is widowed" << endl;
break;
default:
cout << "An invalid code was entered" << endl;
break;
}
return 0;
}
Write and run a program that gives the user only three choices: convert from Fahrenheit
to Celsius, convert from Celsius to Fahrenheit, or quit. If the third choice is selected, the
program stops. If one of the first two choices is selected, the program should prompt the
user for either a Fahrenheit or Celsius temperature, as appropriate, and then compute
and display a corresponding temperature.
#include <iostream>
using namespace std;
int main()
{
int choose;
float tempC, tempF;
cout << "1. Convert Celcius to Farenheit \n";
cout << "2. Convert Farenheit to Celcius \n";
cout << "3. Quit \n";
cout << "Enter your choice (1/2/3): ";
cin >> choose;
if (choose == 1){
cout << "Type temperature in Celcuis: ";
cin >> tempC;
tempF = CtoF(tempC);
cout << "Temperature in Farenheit: " << tempF;
} else
if (choose == 2){
cout << "Type temperature in Farenheit: ";
cin >> tempF;
tempC = FtoC(tempF);
cout << "Temperature in Celcius: " << tempC;
} else
if (choose == 3){
return 0;
} else{
cout << "Invalid input.";
}
return 0;
}
Exercise 8. Write a program to calculate the number of results obtained when solving
the quadratic equation: ax2 + bx + c =0 with given real inputs a, b, and c
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cout << "Enter a, b, and c for ax^2 + bx + c = 0:" << endl;
cin >> a >> b >> c;
double del = b * b - 4 * a * c;
if (del > 0) {
cout << "The equation has two real solutions." << endl;
} else if (del == 0) {
cout << "The equation has one real solution." << endl;
} else {
cout << "The equation has no real solutions." << endl;
}
return 0;
}
Exercise 9. Write a program to find the largest number among three numbers using if, if
else and nested if else statements.
#include <iostream>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
//if
cout << "\nUsing simple if statements:" << endl;
if (num1 >= num2 && num1 >= num3) {
cout << "The largest number is: " << num1 << endl;
}
if (num2 >= num1 && num2 >= num3) {
cout << "The largest number is: " << num2 << endl;
}
if (num3 >= num1 && num3 >= num2) {
cout << "The largest number is: " << num3 << endl;
}
//if-else
cout << "\nUsing if-else statements:" << endl;
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;
}
//nested if-else
cout << "\nUsing nested if-else statements:" << endl;
if (num1 >= num2) {
if (num1 >= num3) {
cout << "The largest number is: " << num1 << endl;
} else {
cout << "The largest number is: " << num3 << endl;
}
} else {
if (num2 >= num3) {
cout << "The largest number is: " << num2 << endl;
} else {
cout << "The largest number is: " << num3 << endl;
}
}
return 0;
}
Exercise 10. Write a program to check whether a year (integer) entered by the user is a
leap year or not.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
Exercise 12. Write a program to check to check whether a given number is positive or
negative.
#include <iostream>
using namespace std;
int main() {
double number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << number << " is positive." << endl;
} else if (number < 0) {
cout << number << " is negative." << endl;
} else {
cout << "Neither." << endl;
}
return 0;
}
Exercise 13. Write a program to check whether a Triangle is Equilateral, Isosceles or
Scalene
#include <iostream>
using namespace std;
int main() {
double a, b, c;
cout << "Enter the lengths of the three sides of the triangle:" <<
endl;
cin >> a >> b >> c;