
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Practical Examples Using Arithmetic Operators
In prolog, arithmetic Operators are very useful and are applicable in various practical usecases. In this chapter, we're demonstrating important examples.
Example - Calculating Area
Program (arithmetic.pl)
area_rectangle(Length, Breadth, Area) :- Area is Length * Breadth.
Output
D:/TP Prolog/Sample Codes/arithmetic.pl compiled, 0 lines read - 411 bytes written, 4 ms yes | ?- area_rectangle(5, 8, Area). Area = 40 yes | ?-
Example - Celcius to Farenheit Conversion
Program (arithmetic.pl)
celsius_to_fahrenheit(C, F) :- F is C * 9 / 5 + 32.
Output
| ?- consult('D:/TP Prolog/Sample Codes/arithmetic.pl'). compiling D:/TP Prolog/Sample Codes/arithmetic.pl for byte code... D:/TP Prolog/Sample Codes/arithmetic.pl compiled, 0 lines read - 574 bytes written, 3 ms yes | ?- celsius_to_fahrenheit(25, F). F = 77.0 yes | ?-
Example - Factorial
Program (arithmetic.pl)
factorial(0, 1). factorial(N, Result) :- N > 0, N_minus_1 is N - 1, factorial(N_minus_1, SubResult), Result is N * SubResult.
Output
| ?- consult('D:/TP Prolog/Sample Codes/arithmetic.pl'). compiling D:/TP Prolog/Sample Codes/arithmetic.pl for byte code... D:/TP Prolog/Sample Codes/arithmetic.pl compiled, 1 lines read - 852 bytes written, 3 ms yes | ?- factorial(5, F). F = 120 ? (15 ms) yes | ?-
Advertisements