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
checkNumber
function takes a single argument, num
, which is the integer to be checked.if
Statement: The if
statement checks if num
is greater than 0.
num
is greater than 0, it prints "num
is positive."else if
Statement: If the first condition is false, the else if
statement checks if num
is less than 0.
num
is less than 0, it prints "num
is negative."else
Statement: If neither the if
nor the else if
conditions are true, the else
statement executes.
num
is zero."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.
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
This example checks the condition and prints the appropriate message based on whether the integer is even or odd.
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
checkLeapYear
function takes a single argument, year
, which is the year to be checked.if
statement checks two conditions combined with an OR operator (||
):
year % 4 == 0 && year % 100 != 0
: This checks if the year is divisible by 4 but not by 100.year % 400 == 0
: This checks if the year is divisible by 400.year
is a leap year."else
Statement: If neither condition is true, it means the year is not a leap year, and the else
statement executes.
year
is not a leap year."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.
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.');
}
}
checkVowelConsonant
function takes a single argument, char
, which is the character to be checked.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]')
).if
block, another if
statement checks if char
is one of the vowels ('aeiouAEIOU'
).char
is a vowel."else
block prints "char
is a consonant."else
block prints "Invalid input. Please enter a single alphabetic character."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.
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.');
}
}
checkNumberInRange
function takes a single argument, num
, which is the integer to be checked.if
Statement: The if
statement checks if num
is within the range of 1 to 100 (inclusive) using the condition num >= 1 && num <= 100
.
num
is within the range, it prints "num
is within the range of 1 to 100."else
Statement: If the if
condition is false, it means num
is outside the range of 1 to 100, and the else
statement executes.
num
is outside the range of 1 to 100."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.
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.');
}
}
checkStringEmpty
function takes a single argument, str
, which is the string to be checked.if
Statement: The if
statement checks if the string str
is empty using the isEmpty
property of the String
class.
str.isEmpty
is true), it prints "The string is empty."else
Statement: If the if
condition is false (the string is not empty), the else
statement executes.
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.
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.');
}
}
checkMultipleOfThreeAndFive
function takes a single argument, num
, which is the integer to be checked.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.num
is a multiple of both 3 and 5, and the function prints "num
is a multiple of both 3 and 5."else
Statement: If either condition is false, it means num
is not a multiple of both 3 and 5, and the else
statement executes.
num
is not a multiple of both 3 and 5."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.
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.');
}
}
checkStringLength
function takes a single argument, str
, which is the string to be checked.if
Statement: The if
statement checks if the length of the string str
is greater than 10 using the length
property.
str.length > 10
), it prints "The string is longer than 10 characters."else
Statement: If the if
condition is false (the length of the string is 10 or less), the else
statement executes.
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.
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.');
}
}
checkDivisibleByTwoOrFive
function takes a single argument, num
, which is the integer to be checked.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.num
is divisible by either 2 or 5, and the function prints "num
is divisible by either 2 or 5."else
Statement: If neither condition is true, it means num
is not divisible by either 2 or 5, and the else
statement executes.
num
is not divisible by either 2 or 5."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.
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.');
}
}
checkNumberInRange
function takes a single argument, num
, which is the integer to be checked.if
Statement: The if
statement checks if num
falls within the range of 50 to 100 (inclusive) using the condition num >= 50 && num <= 100
.
num
is within the range, it prints "num
is within the range of 50 to 100."else
Statement: If the if
condition is false, it means num
is outside the range of 50 to 100, and the else
statement executes.
num
is outside the range of 50 to 100."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.