Search:

Submit Article

# Prolog program to read an integer and display each digit with the respective frequencies of it

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 6945

## Code for Prolog program to read an integer and display each digit with the respective frequencies of it in Artificial Intelligence

```database
frequency(integer,integer)

predicates
start
initfreq(integer)
writing
notzero(integer)
go(integer)
count(integer)
occuring_digit(integer,integer)

goal
clearwindow,
initfreq(9),
start,
writing.
clauses
initfreq(Num):-
asserta(frequency(Num,0)),
NN = Num - 1,
notzero(NN),
initfreq(NN).

initfreq(_).

notzero(Num) :-
Num <> 0.

start:-
write("\nEnter any positive number = "),
go(Num).

go(Num):-
Reminder = (Num mod 10),
count(Reminder),
Temp = (Num div 10),
notzero(Temp),
go(Temp).
go(_).

count(Num):-
frequency(Num,Freq),
retract(frequency(Num,Freq)),
FF = Freq + 1,
asserta(frequency(Num,FF)).

writing:-
write("\nDigit\tFrequency\n"),
fail.

writing:-
frequency(Num,Freq),
retract(frequency(Num,Freq)),
occuring_digit(Num,Freq),
fail.

writing.

occuring_digit(Num,Freq):-
notzero(Freq),
writef("%-5\t%-9",Num,Freq),nl.

occuring_digit(_,_).```
Share:

 Milind Mishra author of Prolog program to read an integer and display each digit with the respective frequencies of it is from India. View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!