How to print number from 1 to N in prolog.
Write a prolog rule to print from 1 to N number when called.
Source code:
print_numbers(10) :- write(10), !.
print_numbers(X) :- write(X), nl, Next is X + 1, print_numbers(Next).
Code description: Here, "print_numbers(10)" is a statement where 10 is the limit of the number. ":-" is represent an "if" condition in prolog. "write(X)" means it will show the variable value, Capital "X" is a variable where values are stored. "Next" is used for the next value and here,
"Next is X + 1" means it starts with the "X" variables value and everytime it incremented by 1. Finally, "print_numbers(Next)" used for showing the values of numbers from start value to end value.For here, end value is 10.
Comments
Post a Comment