Q-1: Write an if-else statement that checks if a given integer num is positive, negative, or zero.

Solution for Question 1

void main() {
checkNumber(10); // Output: 10 is positive.
checkNumber(-5); // Output: -5 is negative.
checkNumber(0); // Output: 0 is zero.
}

void checkNumber(int num) {
if (num > 0) {
print('$num is positive.');
} else if (num < 0) {
print('$num is negative.');
} else {
print('$num is zero.');
}
}

Explanation

  1. Function Definition: The checkNumber function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks if num is greater than 0.
    • If num is greater than 0, it prints "num is positive."
  3. else if Statement: If the first condition is false, the else if statement checks if num is less than 0.
    • If num is less than 0, it prints "num is negative."
  4. else Statement: If neither the if nor the else if conditions are true, the else statement executes.
    • It prints "num is zero."
  5. Function Calls: The main function demonstrates the checkNumber function by calling it with a positive number (10), a negative number (-5), and zero (0).

This example checks the condition and prints the appropriate message based on whether the integer is positive, negative, or zero.

Q-2: Write an if-else statement that checks if a given integer num is even or odd.

Solution for Question 2

void main() {
checkEvenOdd(4); // Output: 4 is even.
checkEvenOdd(7); // Output: 7 is odd.
}

void checkEvenOdd(int num) {
if (num % 2 == 0) {
print('$num is even.');
} else {
print('$num is odd.');
}
}

Explanation
  1. Function Definition: The checkEvenOdd function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks if num is even by using the modulus operator (%). It calculates num % 2, which gives the remainder when num is divided by 2.
    • If the remainder is 0 (i.e., num % 2 == 0), it means num is even, and the function prints "num is even."
  3. else Statement: If the if condition is false (i.e., the remainder is not 0), it means num is odd, and the else statement executes.
    • The function prints "num is odd."
  4. Function Calls: The main function demonstrates the checkEvenOdd function by calling it with an even number (4) and an odd number (7).

This example checks the condition and prints the appropriate message based on whether the integer is even or odd.

Q-3: Write an if-else statement that checks if a given year year is a leap year. (Hint: A year is a leap year if it is divisible by 4, except for years which are divisible by 100 but not 400.)

Solution for Question 3

void main() {
checkLeapYear(2020); // Output: 2020 is a leap year.
checkLeapYear(1900); // Output: 1900 is not a leap year.
checkLeapYear(2000); // Output: 2000 is a leap year.
checkLeapYear(2023); // Output: 2023 is not a leap year.
}

void checkLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
print('$year is a leap year.');
} else {
print('$year is not a leap year.');
}
}

Explanation
  1. Function Definition: The checkLeapYear function takes a single argument, year, which is the year to be checked.
  2. Leap Year Condition:
    • The if statement checks two conditions combined with an OR operator (||):
      1. year % 4 == 0 && year % 100 != 0: This checks if the year is divisible by 4 but not by 100.
      2. year % 400 == 0: This checks if the year is divisible by 400.
    • If either condition is true, it means the year is a leap year, and the function prints "year is a leap year."
  3. else Statement: If neither condition is true, it means the year is not a leap year, and the else statement executes.
    • The function prints "year is not a leap year."
  4. Function Calls: The main function demonstrates the checkLeapYear function by calling it with different years:
    • 2020 (leap year)
    • 1900 (not a leap year, divisible by 100 but not 400)
    • 2000 (leap year, divisible by 400)
    • 2023 (not a leap year)

This example checks the conditions and prints the appropriate message based on whether the year is a leap year or not.

Q-4: Write an if-else statement that checks if a given character char is a vowel or a consonant. (Assume the character is a letter.)

Solution for Question 4

void main() {
checkVowelConsonant('a'); // Output: a is a vowel.
checkVowelConsonant('B'); // Output: B is a consonant.
checkVowelConsonant('e'); // Output: e is a vowel.
checkVowelConsonant('G'); // Output: G is a consonant.
}

void checkVowelConsonant(String char) {
// Ensure the input is a single character and a letter
if (char.length == 1 && RegExp(r'[a-zA-Z]').hasMatch(char)) {
// Check if the character is a vowel
if ('aeiouAEIOU'.contains(char)) {
print('$char is a vowel.');
} else {
print('$char is a consonant.');
}
} else {
print('Invalid input. Please enter a single alphabetic character.');
}
}

Explanation

  1. Function Definition: The checkVowelConsonant function takes a single argument, char, which is the character to be checked.
  2. Input Validation:
    • The if statement first checks if the input is a single character and if it is a letter using char.length == 1 and a regular expression (RegExp(r'[a-zA-Z]')).
  3. Vowel Check:
    • Inside the first if block, another if statement checks if char is one of the vowels ('aeiouAEIOU').
    • If the character is a vowel, it prints "char is a vowel."
  4. Consonant Check:
    • If the character is not a vowel, the else block prints "char is a consonant."
  5. Invalid Input:
    • If the input is not a single alphabetic character, the else block prints "Invalid input. Please enter a single alphabetic character."
  6. Function Calls: The main function demonstrates the checkVowelConsonant function by calling it with different characters:
    • 'a' (vowel)
    • 'B' (consonant)
    • 'e' (vowel)
    • 'G' (consonant)

This example checks the condition and prints the appropriate message based on whether the character is a vowel or a consonant, and also includes validation for proper input.

Q-5: Write an if-else statement that checks if a given integer num is within the range of 1 to 100 (inclusive).

Solution for Question 5

void main() {
checkNumberInRange(50); // Output: 50 is within the range of 1 to 100.
checkNumberInRange(0); // Output: 0 is outside the range of 1 to 100.
checkNumberInRange(101); // Output: 101 is outside the range of 1 to 100.
checkNumberInRange(100); // Output: 100 is within the range of 1 to 100.
}

void checkNumberInRange(int num) {
if (num >= 1 && num <= 100) {
print('$num is within the range of 1 to 100.');
} else {
print('$num is outside the range of 1 to 100.');
}
}

Explanation

  1. Function Definition: The checkNumberInRange function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks if num is within the range of 1 to 100 (inclusive) using the condition num >= 1 && num <= 100.
    • If num is within the range, it prints "num is within the range of 1 to 100."
  3. else Statement: If the if condition is false, it means num is outside the range of 1 to 100, and the else statement executes.
    • The function prints "num is outside the range of 1 to 100."
  4. Function Calls: The main function demonstrates the checkNumberInRange function by calling it with different numbers:
    • 50 (within the range)
    • 0 (outside the range)
    • 101 (outside the range)
    • 100 (within the range)

This example checks the condition and prints the appropriate message based on whether the integer is within the specified range.

Q-6: Write an if-else statement that checks if a given string str is empty or not.

Solution for Question 6

void main() {
checkStringEmpty(''); // Output: The string is empty.
checkStringEmpty('Hello, world!'); // Output: The string is not empty.
}

void checkStringEmpty(String str) {
if (str.isEmpty) {
print('The string is empty.');
} else {
print('The string is not empty.');
}
}

Explanation

  1. Function Definition: The checkStringEmpty function takes a single argument, str, which is the string to be checked.
  2. if Statement: The if statement checks if the string str is empty using the isEmpty property of the String class.
    • If the string is empty (str.isEmpty is true), it prints "The string is empty."
  3. else Statement: If the if condition is false (the string is not empty), the else statement executes.
    • The function prints "The string is not empty."
  4. Function Calls: The main function demonstrates the checkStringEmpty function by calling it with an empty string ('') and a non-empty string ('Hello, world!').

This example checks the condition and prints the appropriate message based on whether the string is empty or not.

Q-7: Write an if-else statement that checks if a given integer num is a multiple of 3 and 5.

Solution for Question 7

void main() {

checkMultipleOfThreeAndFive(15); // Output: 15 is a multiple of both 3 and 5. checkMultipleOfThreeAndFive(10); // Output: 10 is not a multiple of both 3 and 5. checkMultipleOfThreeAndFive(9); // Output: 9 is not a multiple of both 3 and 5. checkMultipleOfThreeAndFive(30); // Output: 30 is a multiple of both 3 and 5.

}

void checkMultipleOfThreeAndFive(int num) {
if (num % 3 == 0 && num % 5 == 0) {
print('$num is a multiple of both 3 and 5.');
} else {
print('$num is not a multiple of both 3 and 5.');
}
}




Explanation

  1. Function Definition: The checkMultipleOfThreeAndFive function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks two conditions combined with an AND operator (&&):
    • num % 3 == 0: This checks if num is divisible by 3.
    • num % 5 == 0: This checks if num is divisible by 5.
    • If both conditions are true, it means num is a multiple of both 3 and 5, and the function prints "num is a multiple of both 3 and 5."
  3. else Statement: If either condition is false, it means num is not a multiple of both 3 and 5, and the else statement executes.
    • The function prints "num is not a multiple of both 3 and 5."
  4. Function Calls: The main function demonstrates the checkMultipleOfThreeAndFive function by calling it with different numbers:
    • 15 (multiple of both 3 and 5)
    • 10 (not a multiple of both 3 and 5, only a multiple of 5)
    • 9 (not a multiple of both 3 and 5, only a multiple of 3)
    • 30 (multiple of both 3 and 5)

This example checks the condition and prints the appropriate message based on whether the integer is a multiple of both 3 and 5.

Q-8: Write an if-else statement that checks if the length of a given string str is greater than 10 characters.

Solution for Question 8

void main() {
checkStringLength('Hello'); // Output: The string is 10 characters or less.
checkStringLength('Hello, world!'); // Output: The string is longer than 10 characters.
checkStringLength('Dart'); // Output: The string is 10 characters or less.
checkStringLength('Programming'); // Output: The string is longer than 10 characters.
}

void checkStringLength(String str) {
if (str.length > 10) {
print('The string is longer than 10 characters.');
} else {
print('The string is 10 characters or less.');
}
}

Explanation

  1. Function Definition: The checkStringLength function takes a single argument, str, which is the string to be checked.
  2. if Statement: The if statement checks if the length of the string str is greater than 10 using the length property.
    • If the length of the string is greater than 10 (str.length > 10), it prints "The string is longer than 10 characters."
  3. else Statement: If the if condition is false (the length of the string is 10 or less), the else statement executes.
    • The function prints "The string is 10 characters or less."
  4. Function Calls: The main function demonstrates the checkStringLength function by calling it with different strings:
    • 'Hello' (length 5, less than or equal to 10)
    • 'Hello, world!' (length 13, greater than 10)
    • 'Dart' (length 4, less than or equal to 10)
    • 'Programming' (length 11, greater than 10)

This example checks the condition and prints the appropriate message based on whether the length of the string is greater than 10 characters.

Q-9: Write an if-else statement that checks if a given temperature temp in degrees Celsius is below freezing (0 degrees), at the freezing point, or above freezing.

Solution for Question 9

void main() {
checkDivisibleByTwoOrFive(10); // Output: 10 is divisible by either 2 or 5.
checkDivisibleByTwoOrFive(7); // Output: 7 is not divisible by either 2 or 5.
checkDivisibleByTwoOrFive(20); // Output: 20 is divisible by either 2 or 5.
checkDivisibleByTwoOrFive(13); // Output: 13 is not divisible by either 2 or 5.
}

void checkDivisibleByTwoOrFive(int num) {
if (num % 2 == 0 || num % 5 == 0) {
print('$num is divisible by either 2 or 5.');
} else {
print('$num is not divisible by either 2 or 5.');
}
}

Explanation

  1. Function Definition: The checkDivisibleByTwoOrFive function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks two conditions combined with an OR operator (||):
    • num % 2 == 0: This checks if num is divisible by 2.
    • num % 5 == 0: This checks if num is divisible by 5.
    • If either condition is true, it means num is divisible by either 2 or 5, and the function prints "num is divisible by either 2 or 5."
  3. else Statement: If neither condition is true, it means num is not divisible by either 2 or 5, and the else statement executes.
    • The function prints "num is not divisible by either 2 or 5."
  4. Function Calls: The main function demonstrates the checkDivisibleByTwoOrFive function by calling it with different numbers:
    • 10 (divisible by both 2 and 5)
    • 7 (not divisible by 2 or 5)
    • 20 (divisible by both 2 and 5)
    • 13 (not divisible by 2 or 5)

This example checks the condition and prints the appropriate message based on whether the integer is divisible by either 2 or 5.

Q-10: Write an if-else statement that checks if a given time hour (in 24-hour format) is in the morning (5 AM to 11:59 AM), afternoon (12 PM to 4:59 PM), evening (5 PM to 8:59 PM), or night (9 PM to 4:59 AM).

Solution for Question 10

void main() {
checkNumberInRange(75); // Output: 75 is within the range of 50 to 100.
checkNumberInRange(50); // Output: 50 is within the range of 50 to 100.
checkNumberInRange(100); // Output: 100 is within the range of 50 to 100.
checkNumberInRange(49); // Output: 49 is outside the range of 50 to 100.
checkNumberInRange(101); // Output: 101 is outside the range of 50 to 100.
}

void checkNumberInRange(int num) {
if (num >= 50 && num <= 100) {
print('$num is within the range of 50 to 100.');
} else {
print('$num is outside the range of 50 to 100.');
}
}

Explanation

  1. Function Definition: The checkNumberInRange function takes a single argument, num, which is the integer to be checked.
  2. if Statement: The if statement checks if num falls within the range of 50 to 100 (inclusive) using the condition num >= 50 && num <= 100.
    • If num is within the range, it prints "num is within the range of 50 to 100."
  3. else Statement: If the if condition is false, it means num is outside the range of 50 to 100, and the else statement executes.
    • The function prints "num is outside the range of 50 to 100."
  4. Function Calls: The main function demonstrates the checkNumberInRange function by calling it with different numbers:
    • 75 (within the range)
    • 50 (at the lower boundary of the range)
    • 100 (at the upper boundary of the range)
    • 49 (just below the lower boundary)
    • 101 (just above the upper boundary)

This example checks the condition and prints the appropriate message based on whether the integer falls within the specified range.

Learn to build beautiful, natively compiled desktop, mobile, and web applications from a single codebase with Flutter.

Multi-Platform

MobileWebDesktop
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram