Assignment # 2 (11th March)
Assignment # 2 (11th March)
SP24-BSE-106(SAMI ULLAH)
Programming fundamentals Assignment #2
Assignment #2
Question #1:
#include <iostream>
using namespace std;
int main(){
int x,y,Small_Number,Large_Number;
cout<<"Enter value of x";
cin>>x;
cout<<"Enter value of y";
cin>>y;
if(x<y){
Small_Number = x;
Large_Number = y;
}
else if(x>y){
Small_Number = y;
Large_Number = x;
}
else{
cout<<"Both numbers are equal";
}
cout<<"Assending Order: "<<Small_Number<<","<<Large_Number;
return 0;
}
Output:
SP24-BSE-106(SAMI ULLAH) 1
Assignment #2
Question #2:
#include <iostream>
using namespace std;
int main(){
int a,b,c;
cout<<"Enter three integers: ";
cin>> a >> b >> c;
int min_val, mid_val, max_val;
if (a < b && a < c) {
min_val = a;
if (b < c) {
mid_val = b;
max_val = c;
}
else {
mid_val = c;
max_val = b;
}
}
else if (b < a && b < c) {
min_val = b;
if (a < c) {
mid_val = a;
max_val = c;
}
else {
mid_val = c;
max_val = a;
}
}
else {
min_val = c;
if (a < b) {
mid_val = a;
max_val = b;
}
else {
mid_val = b;
max_val = a;
}
}
cout << "Sorted order: " << min_val << " " << mid_val << " " <<
max_val;
return 0;
SP24-BSE-106(SAMI ULLAH) 2
Assignment #2
}
Output:
Question #3:
#include <iostream>
using namespace std;
int main(){
int a, b, c;
cout << "Enter value of a: ";
cin >> a;
cout<<"Enter value of b: ";
cin>>b;
cout<<"Enter value of c: ";
cin>>c;
if (a == b && b == c) {
cout << "It is an Equilateral Triangle" << endl;
} else if (a == b || b == c || a == c) {
cout << "It is an Isosceles Triangle" << endl;
} else {
cout << "It is a Scalene Triangle" << endl;
}
return 0;
}
SP24-BSE-106(SAMI ULLAH) 3
Assignment #2
Output 1:
Output 2:
Output 3:
SP24-BSE-106(SAMI ULLAH) 4
Assignment #2
Question #4:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double bodyWeight, height, BMI;
cout << "Enter your height (in inches): ";
cin >> height;
cout << "Enter your body weight (in pounds): ";
cin >> bodyWeight;
BMI = (703.0 * bodyWeight) / pow(height, 2);
cout << "Your BMI is: " << BMI << endl;
SP24-BSE-106(SAMI ULLAH) 5
Assignment #2
Output:
Question #5:
#include <iostream>
using namespace std;
int main(){
int Number;
cout<<"Enter a number between 1-9\n";
cin>>Number;
if(Number==1){
cout<<"One";
}
else if(Number == 2){
cout<<"Two";
}
else if(Number == 3){
cout<<"Three";
}
else if(Number == 4){
cout<<"Four";
}
else if(Number == 5){
cout<<"Five";
}
else if(Number == 6){
cout<<"Six";
}
else if(Number == 7){
cout<<"Seven";
}
else if(Number == 8){
SP24-BSE-106(SAMI ULLAH) 6
Assignment #2
cout<<"Eight";
}
else if(Number == 9){
cout<<"Nine";
}
else{
cout<<"Enter a valid Number";
}
return 0;
}
Output:
SP24-BSE-106(SAMI ULLAH) 7
Assignment #2
Question # 6:
#include <iostream>
using namespace std;
int main(){
int x,y,Result;
char Operator;
cout<<"Enter value of x: ";
cin>>x;
cout<<"Enter value of y: ";
cin>>y;
cout<<"Select Operator(+ - * /)\n";
cin>>Operator;
switch(Operator){
case '+':
Result=x+y;
cout<<"Answer: "<<Result;
break;
case '-':
Result=x-y;
cout<<"Answer: "<<Result;
break;
case '*':
Result=x*y;
cout<<"Answer: "<<Result;
break;
case '/':
Result=x/y;
cout<<"Answer: "<<Result;
break;
default:
cout<<"Enter valid Operator";
}
return 0;
}
SP24-BSE-106(SAMI ULLAH) 8
Assignment #2
Output:
Question #7:
#include <iostream>
using namespace std;
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
}
else {
return false;
}
}
int getDaysInMonth(int month, int year) {
if (month == 2) {
return isLeapYear(year) ? 29 : 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
else {
return 31;
}
}
int main() {
int month, year, totalDays = 0;
cout << "Enter the month (1-12): ";
cin >> month;
cout << "Enter the year: ";
cin >> year;
SP24-BSE-106(SAMI ULLAH) 9
Assignment #2
1
SP24-BSE-106(SAMI ULLAH)
0