If-Else Solution
If-Else Solution
IF-ELSE|
1) Check Whether Number is Even or Odd
Method 1: Using if else
#include <iostream>
using namespace std;
int main() {
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Output
Enter an integer: 23
23 is odd.
int main() {
int n;
(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
return 0;
}
CONCEPT:
We used ternary operators ?: instead of if..else statement. The ternary operator is a
shorthand notation of if...else statement.
int main() {
char c;
bool isLowercaseVowel, isUppercaseVowel;
return 0;
}
Output
Enter an alphabet: u
u is a vowel.
CONCEPT:
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
The isalpha() function checks whether the character entered is an alphabet or not. If it is
not, it prints an error message.
int main() {
float n1, n2, n3;
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
int main() {
float n1, n2, n3;
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
int main() {
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
Output
CODE:
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
int main() {
int year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
SOLUTION BOOK
❤️ From SIDDHARTH SINGH
return 0;
}
Output
Enter a year: 2014
2014 is not a leap year