Practice Questions: Conditions and Loops In Dart
 

Questions

  1. Write a Dart program that checks if a number is positive, negative, or zero and prints an appropriate message.
  2. Create a program that uses a for loop to print the first 10 square numbers (1, 4, 9, ...).
  3. Write a Dart program that calculates the factorial of a given number using a while loop.
  4. Implement a program that prints the Fibonacci series up to a specified number of terms using a for loop.
  5. Create a program that uses a switch statement to print the day of the week based on a number (1 for Monday, 7 for Sunday).
  6. Write a program that asks the user for a number and keeps asking until a positive number is entered, using a do-while loop.
  7. Implement a program that counts down from 10 to 1 using a while loop and prints "Blast Off!" at the end.
  8. Create a program that uses a for loop to find the sum of even numbers between 1 and 100.
  9. Write a program that uses break to exit a loop when a specific condition is met (e.g., when the number is 50).
  10. Implement a program that uses continue to skip odd numbers and print only even numbers from 1 to 20.
  11. Create a program that checks if a string is a palindrome (reads the same forwards and backwards).
  12. Write a Dart program that converts a letter grade (A, B, C, D, F) into a numerical score using a switch statement.
  13. Implement a program that takes an integer input from the user and prints whether it is prime or not.
  14. Create a program that uses a nested for loop to create a multiplication table for numbers 1 through 5.
  15. Write a program that uses a while loop to sum the digits of a positive integer input by the user.

Answers with Code Explanation
 

1. Check if a number is positive, negative, or zero

void main() {
  int number = -5; // Example number 
  if (number > 0) {
    print('The number is positive.');
  } else if (number < 0) {
    print('The number is negative.');
  } else {
    print('The number is zero.');
  }
}

Explanation: The program checks if the number is greater than, less than, or equal to zero and prints the corresponding message.
 

2. Print the first 10 square numbers

void main() {
  for (int i = 1; i <= 10; i++) {
    print(i * i);
  }
}

Explanation: The loop iterates from 1 to 10, calculating and printing the square of each number.

3. Calculate the factorial of a number

void main() {
  int n = 5; // Example number   int factorial = 1;
  int i = 1;

  while (i <= n) {
    factorial *= i;
    i++;
  }

  print('Factorial of $n is: $factorial');
}

Explanation: The program initializes a factorial variable and uses a while loop to multiply it by each integer up to n.

4. Print the Fibonacci series

void main() {
  int terms = 10; // Number of terms   int a = 0, b = 1;

  for (int i = 1; i <= terms; i++) {
    print(a);
    int next = a + b;
    a = b;
    b = next;
  }
}

Explanation: The loop generates Fibonacci numbers by summing the two previous numbers and updating them accordingly.

5. Print the day of the week

void main() {
  int dayNumber = 3; // Example number 
  switch (dayNumber) {
    case 1:
      print('Monday');
      break;
    case 2:
      print('Tuesday');
      break;
    case 3:
      print('Wednesday');
      break;
    case 4:
      print('Thursday');
      break;
    case 5:
      print('Friday');
      break;
    case 6:
      print('Saturday');
      break;
    case 7:
      print('Sunday');
      break;
    default:
      print('Invalid day number.');
  }
}

Explanation: The program uses a switch statement to print the corresponding day based on the input number.

6. Ask for a positive number

import 'dart:io';

void main() {
  int number;

  do {
    print('Enter a positive number:');
    number = int.parse(stdin.readLineSync()!);
  } while (number <= 0);

  print('You entered: $number');
}

Explanation: The program prompts the user until a positive number is entered, using a do-while loop.

7. Countdown from 10 to 1

void main() {
  int countdown = 10;

  while (countdown > 0) {
    print(countdown);
    countdown--;
  }
  print('Blast Off!');
}

Explanation: The while loop decrements the countdown variable and prints it until it reaches zero, then prints "Blast Off!"

8. Sum of even numbers between 1 and 100

void main() {
  int sum = 0;

  for (int i = 1; i <= 100; i++) {
    if (i % 2 == 0) {
      sum += i;
    }
  }

  print('Sum of even numbers between 1 and 100 is: $sum');
}

Explanation: The loop iterates through numbers from 1 to 100, adding even numbers to the sum variable.

9. Exit a loop when the number is 50

void main() {
  for (int i = 1; i <= 100; i++) {
    if (i == 50) {
      print('Breaking the loop at i = $i');
      break;
    }
    print(i);
  }
}

Explanation: The loop prints numbers until it reaches 50, at which point it exits using break.

10. Skip odd numbers and print even numbers

void main() {
  for (int i = 1; i <= 20; i++) {
    if (i % 2 != 0) {
      continue; // Skip odd numbers     }
    print(i);
  }
}

Explanation: The loop skips the printing of odd numbers by using the continue statement.

11. Check if a string is a palindrome

void main() {
  String str = "madam";
  String reversed = str.split('').reversed.join('');

  if (str == reversed) {
    print('$str is a palindrome.');
  } else {
    print('$str is not a palindrome.');
  }
}

Explanation: The program compares the original string with its reversed version to determine if it is a palindrome.

12. Convert letter grade to numerical score

void main() {
  String grade = 'B';

  switch (grade) {
    case 'A':
      print('Score: 90-100');
      break;
    case 'B':
      print('Score: 80-89');
      break;
    case 'C':
      print('Score: 70-79');
      break;
    case 'D':
      print('Score: 60-69');
      break;
    case 'F':
      print('Score: Below 60');
      break;
    default:
      print('Invalid grade.');
  }
}

Explanation: The switch statement maps letter grades to their corresponding numerical score ranges.

13. Check if a number is prime

 

void main() {
  int number = 29; // Example number   
  bool isPrime = true;

  for (int i = 2; i <= number ~/ 2; i++) {
    if (number % i == 0) {
      isPrime = false;
      break;
    }
  }

  if (isPrime) {
    print('$number is a prime number.');
  } else {
    print('$number is not a prime number.');
  }
}

Explanation: The program checks for factors of the number and sets isPrime to false if any are found.

14. Multiplication table from 1 to 5

 

void main() {
  for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 10; j++) {
      print('$i x $j = ${i * j}');
    }
    print(''); // Print a blank line between tables   
    }
}

Explanation: The outer loop iterates through numbers 1 to 5, while the inner loop creates the multiplication table for each number.

15. Sum the digits of a positive integer

 

import 'dart:io';

void main() {
  print('Enter a positive integer:');
  int number = int.parse(stdin.readLineSync()!);
  int sum = 0;

  while (number > 0) {
    sum += number % 10; // Add the last digit to the sum 
    number ~/= 10;       // Remove the last digit   
    }

  print('Sum of the digits is: $sum');
}

Explanation: The program extracts and sums the digits of the number until it reduces to zero. The modulus operator retrieves the last digit, and integer division removes it.

These questions and answers cover a variety of conditions and loops concepts in Dart, providing a solid foundation for further practice and learning.