0% found this document useful (0 votes)
11 views11 pages

Assignment # 2 (11th March)

The document contains a programming assignment with seven questions, each demonstrating different programming concepts in C++. Questions include finding the smaller and larger of two numbers, sorting three integers, identifying triangle types, calculating BMI, converting numbers to words, performing basic arithmetic operations, and generating a calendar for a given month and year. Each question is accompanied by code snippets and expected outputs.
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)
11 views11 pages

Assignment # 2 (11th March)

The document contains a programming assignment with seven questions, each demonstrating different programming concepts in C++. Questions include finding the smaller and larger of two numbers, sorting three integers, identifying triangle types, calculating BMI, converting numbers to words, performing basic arithmetic operations, and generating a calendar for a given month and year. Each question is accompanied by code snippets and expected outputs.
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/ 11

Assignment #2

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;

if (BMI >= 30.0) {


cout << "You are Obese!";
} else if (BMI >= 25.0) {
cout << "You are Overweight";
} else if (BMI >= 18.5) {
cout << "Normal weight";
} else {
cout << "Underweight";
}
return 0;
}

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

for (int i = 1; i < month; i++) {


totalDays += getDaysInMonth(i, year);
}
int dayOfWeek = (totalDays + 1) % 7;
cout << "\nCalendar for " << month << "/" << year << ":\n\n";
cout << "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n";
if (dayOfWeek != 0) {
for (int i = 0; i < dayOfWeek; ++i) {
cout << "\t";
}
}
for (int i = 1; i <= getDaysInMonth(month, year); ++i) {
cout << i << "\t";
if ((dayOfWeek + i) % 7 == 0) {
cout << endl;
}
}
return 0;
}
Output:

1
SP24-BSE-106(SAMI ULLAH)
0

You might also like