Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. Functions
  2. BuiltinFunctions

mathFunction

In Dart, you can perform various mathematical operations using built-in
functions and operators. Here are some common mathematical operations you
can perform in Dart:

 To use math in dart, you must import 'dart:math';.

Basic Arithmetic Operations:

Dart supports the basic arithmetic operations, including addition, subtraction, multiplication, and division, using operators like +, -, *, and /.

void main() {
  int num1 = 10;
  int num2 = 5;

  int sum = num1 + num2; // Addition
  int difference = num1 - num2; // Subtraction
  int product = num1 * num2; // Multiplication
  double quotient = num1 / num2; // Division

  print('Sum: $sum');
  print('Difference: $difference');
  print('Product: $product');
  print('Quotient: $quotient');
}

Modulus Operator:

The modulus operator % calculates the remainder of a division operation.

void main() {
  int num1 = 10;
  int num2 = 3;

  int remainder = num1 % num2; // Modulus

  print('Remainder: $remainder');
}

Mathematical Functions:

Dart provides mathematical functions like sqrt, pow, sin, cos, and tan in the dart:math library for more advanced calculations.

import 'dart:math';

void main() {
  double number = 25.0;

  double squareRoot = sqrt(number); // Square root
  double power = pow(number, 2); // Exponentiation
  double sine = sin(number); // Sine
  double cosine = cos(number); // Cosine
  double tangent = tan(number); // Tangent

  print('Square Root: $squareRoot');
  print('Power of 2: $power');
  print('Sine: $sine');
  print('Cosine: $cosine');
  print('Tangent: $tangent');
}

Random Numbers:

To generate random numbers, you can use the Random class from the dart:math library.

import 'dart:math';

void main() {
  Random random = Random();

  // Generate a random integer between 1 and 100 (inclusive)
  int randomNumber = random.nextInt(100) + 1;

  print('Random Number: $randomNumber');
}

These are some of the fundamental mathematical operations and functions available in Dart. Depending on your needs, you can perform a wide range of mathematical calculations in your Dart programs.

PreviousimportantBuiltinFunctionsNextOOP With Dart

Last updated 1 year ago