0% found this document useful (0 votes)
13 views5 pages

Ifelse

if-else condition and switch statement in cpp
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)
13 views5 pages

Ifelse

if-else condition and switch statement in cpp
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/ 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