Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

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

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

Write a complete prolog program to read an integer and display
each digit with the respective frequencies of it.
For Example if 12342 is input the output should be
Digit Frequency
1__________1
2__________2
3__________1
4 1

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 = "),
        readint(Num),
        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
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

Related Articles and Code:


 

Other Interesting Articles in Artificial Intelligence:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!