Ifelse

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

#include <iostream>

#include <string>
using namespace std;

int main() {
string gender;
int age;

// Take user input for gender and age


cout << "Enter gender (male/female): ";
cin >> gender;
cout << "Enter age: ";
cin >> age;

// Check if the applicant meets the criteria


if (gender == "male" && age > 30) {
cout << "Job assigned to the applicant." << endl;
} else {
cout << "Job not assigned. Applicant does not meet the criteria." << endl;
}

return 0;
}
>

#include <iostream>
//#include <string>
using namespace std;

int main() {
string gender;
int age;

// Take user input for gender and age


cout << "Enter gender (male/female): ";
cin >> gender;
cout << "Enter age: ";
cin >> age;

// Nested if-else statements to check conditions


if (gender == "male") {
if (age > 30) {
cout << "Job assigned to the applicant." << endl;
} else {
cout << "Job not assigned. Applicant is male but not older than 30." << endl;
}
} else {
cout << "Job not assigned. Applicant is not male." << endl;
}

return 0;
}
#include <iostream>
using namespace std;

int main() {
int ageAli, ageHamza, ageBasit;

// Input ages
cout << "Enter age of Ali: ";
cin >> ageAli;
cout << "Enter age of Hamza: ";
cin >> ageHamza;
cout << "Enter age of Basit: ";
cin >> ageBasit;

// Determine the youngest age


if (ageAli < ageHamza && ageAli < ageBasit) {
cout << "Ali is the youngest." << endl;
} else if (ageHamza < ageAli && ageHamza < ageBasit) {
cout << "Hamza is the youngest." << endl;
} else if (ageBasit < ageAli && ageBasit < ageHamza) {
cout << "Basit is the youngest." << endl;
} else {
cout << "There is a tie for the youngest age." << endl;
}

return 0;
}
#include <iostream>
using namespace std;

int main() {
char operation;
float num1, num2;

// Take user input for operation and numbers


cout << "Enter operator (+, -, *, /, %): ";
cin >> operation;
cout << "Enter two numbers: ";
cin >> num1 >> num2;

// Perform operation based on the operator


switch (operation) {
case '+':
cout << "Result: " << num1 + num2;
break;
case '-':
cout << "Result: " << num1 - num2;
break;
case '*':
cout << "Result: " << num1 * num2;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2;
else
cout << "Error: Division by zero is not allowed.";
break;
case '%':
if (static_cast<int>(num2) != 0)
cout << "Result: " << static_cast<int>(num1) % static_cast<int>(num2);
else
cout << "Error: Modulus by zero is not allowed.";
break;
default:
cout << "Error: Invalid operator!";
break;
}

return 0;
}
#include <iostream>
using namespace std;

int main() {
char operation;
float num1, num2;

// Take user input for operation and numbers


cout << "Enter operator (+, -, *, /): ";
cin >> operation;
cout << "Enter two numbers: ";
cin >> num1 >> num2;

// Perform operation based on the operator


switch (operation) {
case '+':
cout << "Result: " << num1 + num2;
break;
case '-':
cout << "Result: " << num1 - num2;
break;
case '*':
cout << "Result: " << num1 * num2;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2;
else
cout << "Error: Division by zero is not allowed.";
break;

default:
cout << "Error: Invalid operator!";
break;
}

return 0;
}
#include <iostream>
using namespace std;

int main() {
char ch;

// Take input character from user


cout << "Enter a character: ";
cin >> ch;

// Convert to lowercase for uniformity


ch = tolower(ch);

// Check if the character is a vowel


switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << ch << " is a vowel.";
break;
default:
cout << ch << " is not a vowel.";
break;
}

return 0;
}

You might also like