Exercises

  • Exercise 1 :
  • Write a program that accepts two Strings from user : first_name and last_name and print :
    Dr. last_name first_name.

    Input:

    Albert
    Einstein

    Output:

    Dr. Einstein Albert


    Solution:

    import 'dart:io';

     

    void main() {

      print("Enter First Name : ");

      var first_name = stdin.readLineSync();

      print("Enter Last Nale  : ");

      var last_name = stdin.readLineSync();

      print("Dr. " + last_name + " " + first_name);

    }



    Enter First Name :
    Albert
    Enter Last Nale :
    Einstein
    Dr. Einstein Albert

  • Exercise 2 :
  • Write a program that accepts three integers from user : a, b, c , create these new variables and print their value :

    • 1) sum = a+b+c

    • 2) mul = a*b*c

    • 3) diff = mul - sum

    • 4) aa=a++         (use Unary Operators)

    • 5) bb=++b         (use Unary Operators)

    • 6) cc=--c            (use Unary Operators)

    • 7) aa=aa+bb       (use Assignment Operators)

    • 8) cc=cc-aa         (use Assignment Operators)

    • 9) d=a/(b/c)

    • 10) check if d is of type double using is keyword.


stdin.readLineSync() read the input as a string.
To convert it to int use this function:

int.parse(stdin.readLineSync());


Solution:

import 'dart:io';

 

void main() {

  print("Enter 3 integers : ");

  var a = int.parse(stdin.readLineSync());

  var b = int.parse(stdin.readLineSync());

  var c = int.parse(stdin.readLineSync());

  //1

  var sum = a + b + c;

  print(sum);

  //2

  var mul = a * b * c;

  print(mul);

  //3

  var diff = mul - sum;

  print(diff);

  //4

  var aa = a++;

  print(aa);

  //5

  var bb = ++b;

  print(bb);

  //6

  var cc = --c;

  print(cc);

  //7

  aa += bb;

  print(aa);

  //8

  cc -= aa;

  print(cc);

  //9

  var d = a / (b / c);

  print(d);

  //10

  print(d is double);

}



Enter 3 integers :
5
7
10
22
350
328
5
8
9
13
-4
6.75
true

  • Exercise 3 :
  • Write a program that accepts a string from user and check if the first letter and the last letter are equals then print "Equals" else print "Not equals".
    Try to use Conditional Operators.


    Solution:

    import 'dart:io';

     

    void main() {

      var input = stdin.readLineSync();

      input[0] == input[input.length - 1] ? print("Equals") : print("Not Equals");

    }



    ADAMA
    Equals

    JOHN
    Not Equals

  • Exercise 4 :
  • What is the ouput of this program?


    void main() {

      const pi = 3.14;

      int a = 5, b = -12, c = 27;

      bool test1 = (c + b) % a == 0;

      print(test1);

      c -= 20;

      bool test2 = ((c > 0) || (b > 0)) && (5 != a++);

      print(test2);

      bool test3 = !test1 || !test2;

      print(test3);

      bool test4 = ++a + --b + c++ > pi;

      print(test4);

    }



    Solution:

    true
    false
    true
    false