if else statement in c++ sample problems
C++ check whether a character is alphabet or not
In this program, we are going to see whether a user given character is an alphabet or not. To determine the type of character, we have to check itβs ASCII value range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int main() { char ch; cout << "Enter any character: "; cin >> ch; if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { cout << endl << ch << " is ALPHABET."; } else { cout << endl<< ch << " is NOT ALPHABET."; } return 0; } |
Output:
1 2 3 4 | Enter any character : x x is ALPHABET. |
C++ check whether a character is alphabet, digit or special character
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> using namespace std; int main() { char ch; cout << "Enter any character : "; cin >> ch; if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { cout << endl << ch << " is ALPHABET."; } else if(ch >= '0' && ch <= '9') { cout << endl << ch << " is DIGIT."; } else { cout << endl << ch << " is SPECIAL CHARACTER."; } return 0; } |
Output:
1 2 3 4 | Enter any character : # # is SPECIAL CHARACTER. |
C++ find largest number among three number using if statement
In this program, we are going to see how to find the largest number among three numbers using if statement.
Logic
Here we have to compare each number with another two numbers, if it is greater than the both then simply print it.
Letβs say A = 11, B = 22 and C = 15
Then steps would be
- ifΒ A > BΒ andΒ A > CΒ that means A is the largest number.
- ifΒ B > AΒ andΒ B > CΒ that means B is the largest number.
- Similarly ifΒ C > AΒ andΒ C > BΒ that means C is the largest number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <iostream> using namespace std; int main() { float num1, num2, num3; cout << "Enter 1st number : "; cin >> num1; cout << endl << "Enter 2nd number : "; cin >> num2; cout << endl<< "Enter 3rd number : "; cin >> num3; if(num1 >= num2 && num1 >= num3) { cout << endl << num1 << " is largest number"; } if(num2 >= num1 && num2 >= num3) { cout << endl << num2 << " is largest number"; } if(num3 >= num1 && num3 >= num2) { cout << endl << num3 << " is largest number"; } return 0; } |
Output:
1 2 3 4 5 6 | Enter 1st number : 10 Enter 2nd number : 5 Enter 3rd number : 11 11 is largest number |
C++ find largest number among three number using if else statement
This program is similar to the previous one, with a single modification, here we will useΒ if elseΒ statement to find the largest number.
Logic
The logic to find the largest number among the three numbers, we have to compare each number with the other two numbers.
Let three variables be: A = 400, B = 200 and C = 300
- ifΒ A > BΒ andΒ A > C, thenΒ print A.
- else ifΒ B > A and B > C, thenΒ print B.
- elseΒ print C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream> using namespace std; int main() { float num1, num2, num3; cout << "Enter 1st number : "; cin >> num1; cout << endl << "Enter 2nd number : "; cin >> num2; cout << endl << "Enter 3rd number : "; cin >> num3; if(num1 >= num2 && num1 >= num3) { cout << endl << num1 << " is largest number"; } else if(num2 >= num3) { cout << endl << num2 << " is largest number"; } else { cout << endl << num3 << " is largest number"; } return 0; } |
Output:
1 2 3 4 5 6 | Enter 1st number : 10 Enter 2nd number : 200 Enter 3rd number : 11 200 is largest number |
C++ find largest number among three number using nested if else statement
In this program, we are going to find the largest number among three numbers, similar to the previous one, but it is nested if-else version.
Logic
Let three variables be: A = 400, B = 200 and C = 300
The logic goes like this:
- ifΒ A >= BΒ then check for ifΒ A >= C, thenΒ print AΒ elseΒ print C.
- else part: ifΒ B >= CΒ thenΒ print BΒ elseΒ print C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <iostream> using namespace std; int main() { float num1, num2, num3; cout << "Enter 1st number : "; cin >> num1; cout << endl << "Enter 2nd number : "; cin >> num2; cout << endl << "Enter 3rd number : "; cin >> num3; if(num1 >= num2) { if(num1 >= num3) { cout << endl << num1 << " is largest number"; } else { cout << endl << num3 << " is largest number"; } } else { if(num2 >= num3) { cout << endl << num2 << " is largest number"; } else { cout << endl << num3 << " is largest number"; } } return 0; } |
Output
1 2 3 4 5 6 | Enter 1st number : 100 Enter 2nd number : 10 Enter 3rd number : 99 100 is largest number |
C++ program to check whether a year is leap year or not
Leap year
A leap year is a calendar year that includes an additional day to synchronize the calendar year with the astronomical or seasonal year. βΒ Wikipedia
Logic
The Logic to check this is quite simple. We only need to check if the given year is multiple of 4 or 400, but it should not be multiple of 100.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; int main() { int year; cout << "Enter a year : "; cin >> year; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { cout << endl << year << " is a leap year."; } else { cout << endl << year << " is not a leap year."; } } else { cout << endl << year << " is a leap year."; } } else { cout << endl << year << " is not a leap year."; } return 0; } |
Output
1 2 3 4 | Enter a year : 2012 2012 is a leap year. |
C++ program to check whether number is even or odd
Even number
Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.
Odd number
Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.
Logic
To find if a number is even or odd we only need to check if the given number is multiple of 2 or not, If it is multiple of 2 that means it is even number otherwise an odd number.
To check if it is multiple of 2 or not we use modulus operator.
If the expressionΒ number % 2Β returns 0 that means theΒ numberΒ is a multiple of 2. In other words, it is completely divisible by 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer : "; cin >> number; if (number % 2 == 0) { cout << endl << number << " is even number."; } else { cout << endl << number << " is odd number."; } return 0; } |
Output
1 2 3 4 | Enter an integer : 886 886 is even number. |
C++ check whether a number is negative, positive or zero
Positive number
All the numbers greater than 0, but not equal to 0 are positive numbers.
Negative number
Similarly all the number less than 0, but not equal to 0 are negative numbers.
Logic
If the number is greater than zero that means it is positive. If the number is less than zero that means it is negative. If the number is equal to zero that means it is absolute zero.
- if number > 0 then printΒ Positive.
- if number < 0 then printΒ Negative.
- if number == 0 then printΒ Zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; int main() { int number; cout << "Enter any number : "; cin >> number; if(number > 0) { cout << endl << "POSITIVE NUMBER" << endl; } if(number < 0) { cout << endl << "NEGATIVE NUMBER" << endl; } if(number == 0) { cout << endl << "NUMBER IS ZERO" << endl; } return 0; } |
Output
1 2 3 4 | Enter any number : 101 POSITIVE NUMBER. |
C++ check whether a character is upper or lowercase alphabet
If the ASCII value of a character lies betweenΒ 65 (A)Β toΒ 90 (Z)Β then it is an uppercase character or if the characterβs ASCII value lies betweenΒ 97 (a)Β toΒ 122 (z)Β then it is a lowercase character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> using namespace std; int main() { char ch; cout << "Enter any character : "; cin >> ch; if(ch >= 'A' && ch <= 'Z') { cout << endl << ch << " is UPPERCASE alphabet."; } else if(ch >= 'a' && ch <= 'z') { cout << endl << ch << " is LOWERCASE alphabet."; } else { cout << endl << ch << " is not an alphabet."; } return 0; } |
Output
1 2 3 4 | Enter any character : S S is UPPERCASE alphabet. |
C++ check whether a character is vowel or consonant
Logic
Here we have to manually check the given character with all the vowel characters, we cannot use ASCII value range to determine whether it is a vowel or a consonant. The given character can also be in the form of uppercase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; int main() { char c; cout << "Enter a alphabet : "; cin >> c; // if given character is Lower case Vowel or Upper case Vowel // then print vowel otherwise consonant if ((c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') || (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')) { cout << endl << c << " is a vowel."; } else{ cout << endl << c << " is a consonant."; } return 0; } |
Output
1 2 3 4 | Enter a alphabet : A A is a vowel. |
C++ print day name of week from number
In this program, we are going to print day name based on week no. Like β If the user enters 1 that means it is Monday.
Logic
After taking input (Week no) from the user, we have to compare that number with 1 to 7 or we can say comparing that number with a day of a week. So the logic goes like β if the number is equal to 1 then it is Monday, if the number is 2 then it is Tuesday etcβ¦ Like that we have to compare with each day of the week.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> using namespace std; int main() { int weekday; cout << "Enter weekday day number (1-7) : "; cin >> weekday; if(weekday == 1) { cout << endl << "Monday"; } else if(weekday == 2) { cout << endl << "Tuesday"; } else if(weekday == 3) { cout << endl << "Wednesday"; } else if(weekday == 4) { cout << endl << "Thursday"; } else if(weekday == 5) { cout << endl << "Friday"; } else if(weekday == 6) { cout << endl << "Saturday"; } else if(weekday == 7) { cout << endl << "Sunday"; } else { cout << endl << "Please enter weekday number between 1-7."; } return 0; } |
Output
1 2 3 4 | Enter weekday day number (1-7) : 4 Thursday |