Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

prime number checker dart

import 'dart:io';

void main() {
  stdout.write("Please give us a number: ");
  int chosenNumber = int.parse(stdin.readLineSync());

  checkPrime(chosenNumber);
}

void checkPrime(int number) {
  // List comprehensions
  List<int> a = [
    for (var i = 1; i <= number; i++)
      if (number % i == 0) i
  ];

  // Check for prime
  a.length == 1
      ? print("The chosen number is a prime")
      : print("The chosen number is not a prime");
}
Source by hackmd.io #
 
PREVIOUS NEXT
Tagged: #prime #number #checker #dart
ADD COMMENT
Topic
Name
7+3 =