C++

πŸ’» if else Statement in C++ Example Program

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.

Output:

 

C++ check whether a character is alphabet, digit or special character

This program is much similar to the previous one but here we are checking whether the given character is an alphabet, digit or a special character.

Output:

 

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

  1. ifΒ A > BΒ andΒ A > CΒ that means A is the largest number.
  2. ifΒ B > AΒ andΒ B > CΒ that means B is the largest number.
  3. Similarly ifΒ C > AΒ andΒ C > BΒ that means C is the largest number.

Output:

 

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

  1. ifΒ A > BΒ andΒ A > C, thenΒ print A.
  2. else ifΒ B > A and B > C, thenΒ print B.
  3. elseΒ print C.

Output:

 

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:

  1. ifΒ A >= BΒ then check for ifΒ A >= C, thenΒ print AΒ elseΒ print C.
  2. else part: ifΒ B >= CΒ thenΒ print BΒ elseΒ print C.

Output

 

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.

Output

 

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.

Output

 

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.

  1. if number > 0 then printΒ Positive.
  2. if number < 0 then printΒ Negative.
  3. if number == 0 then printΒ Zero.

Output

 

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.

Output

 

C++ check whether a character is vowel or consonant

There are five vowel characters {a,Β e,Β i,Β o,Β u}. If the user given character input is one of them that means it is a vowel otherwise it is a 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.

Output

 

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.

Output

Leave a Comment