Posts

In this post, I am sharing the code to make a simple Calculator with the main 4 functions - add, subtract, multiply, and subtract:

File:Calculator Flat Icon Vector.svg - Wikimedia Commons

Source Code :

import 'dart:io';

void main() {
print('This is a Calculator!\n');
print('''Commands:
add => +
subtract => -
multiply => *
divide => /''');

calculation();
}

void calculation() {
stdout.writeln('First Number:');
var num1 = int.parse(stdin.readLineSync());

stdout.writeln('Second Number:');
var num2 = int.parse(stdin.readLineSync());

stdout.writeln('Command');
var comm = stdin.readLineSync();

if (comm == '+') {
num res = num1 + num2;
print('The Result is $res');
} else if (comm == '-') {
num res = num1 - num2;
print('The Result is $res');
} else if (comm == '*') {
num res = num1 * num2;
print('The Result is $res');
} else if (comm == '/') {
num res = num1 / num2;
print('The Result is $res');
} else {
print('ERROR!');
}

print('Want to continue calculation? (y/n)');
var i1 = stdin.readLineSync();

if (i1 == 'y') {
calculation();
} else {
print('Thanks for using!');
}
}


Output :


This is a Calculator!

Commands:
  add => +
  subtract => -
  multiply => *
  divide => /
First Number:
66
Second Number:
87
Command
*
The Result is 5742
Want to continue calculation? (y/n)
y
First Number:
5742
Second Number:
5000
Command
-
The Result is 742
Want to continue calculation? (y/n)
n
Thanks for using!



My new blog


https://www.govindgyt.blogspot.com/

Tags: AllCodingDart

Post a Comment

0 Comments

Skip to main content