[go: up one dir, main page]

0% found this document useful (0 votes)
5 views12 pages

LAB3

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

LAB3

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

Exercise 3.

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;

// Ask the user to input the month (1 to 12)


cout << "Enter the month number (1-12): ";
cin >> month;

// Check the month and display the corresponding number of days


if (month == 1 || month == 3 || month == 5 || month == 7 || month ==
8 || month == 10 || month == 12) {
cout << "31 days" << endl;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
cout << "30 days" << endl;
} else if (month == 2) {
cout << "28 days" << endl;
} else {
cout << "Invalid month" << endl;
}
return 0;
}
Exercise 5. Write and run a program that reads a numeric grade from a student and
displays a corresponding character grade for that numeric grade. The program prints ‘A’
for exam grades greater than or equal to 90, ‘B’ for grades in the range 80 to 89, ‘C’ for
grades in the range 70 to 79, ‘D’ for grades in the range 60 to 69 and ‘F’ for all other
grades.

#include <iostream>
using namespace std;

int main() {
int grade;
cout << "Enter the numeric grade: ";
cin >> grade;

if (grade >= 90) {


cout << "A" << endl;
} else if (grade >= 80) {
cout << "B" << endl;
} else if (grade >= 70) {
cout << "C" << endl;
} else if (grade >= 60) {
cout << "D" << endl;
} else {
cout << "F" << endl;
}
return 0;
}
Exercise 6. Write and run a program that reads a marriage code (one character) and
writes out a message corresponding to the character:
if the character is ‘M’ or ‘m’ then the message is “Individual is married”
if the character is ‘D’ or ‘d’ then the message is “Individual is divorced”
if the character is ‘W’ or ‘w’ then the message is “Individual is widowed”
otherwise, the message is “An invalid code was entered”.
#include <iostream>
using namespace std;

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;

float CtoF(float C){


return (9.0 / 5.0) * C + 32.0;
}
float FtoC(float F){
return (5.0 / 9.0) * (F - 32.0);
}

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;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Exercise 11. Write a program to check whether a number entered by the user is even or
odd.

#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;

if ((a + b > c) && (a + c > b) && (b + c > a)) {


// Determine the type of triangle
if (a == b && b == c) {
cout << "Equilateral." << endl;
} else if (a == b || b == c || a == c) {
cout << "Isosceles." << endl;
} else {
cout << "Scalene." << endl;
}
} else {
cout << "Invalid" << endl;
}
return 0;
}

You might also like