Ppl Prolog

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ppl Prolog as PDF for free.

More details

  • Words: 956
  • Pages: 7
Write a predicate in Prolog to center string in a filed of 80 characters. Solution: domains NN,N,Y = integer predicates go write_right_space(integer) write_spaces(integer) clauses: go:N = 10, write_right_spaces(N). write_right_spaces(N):Y = 80 – N; Write_spaces(Y). write_spaces(0):- !. write_spaces(x):write(“ “), NN=x-1; Write_spaces(NN).

Write a Prolog predicates strops(imputstring, substring, startpos) that returns the starting position of substring in the given input string as startpos Solution domains: SS=string predicates: go strops(string, string, integer) search(string, string, integer, integer) clauses go:- write(“Enter I/P string : ”); readln( I ) write(“Enter search string :”); readln( S ) str_len( I , 11 ), str_len( S , 12 ), L2 <= L1 strops( I , S , - ). go:- writte 1”Invalid Input “), n1. stropos(I,S,-) :- -str_len( S , S1 ), search(I , S , 1 , s1 ). search(I , S , 1 , s1 ) :- frontstr( S1 , I , SS , - ), SS = S, write(“ Reg string found at position ” , L ), n1, !. search(I , S , L , s1 ) :L1 = L + 1 Frontchar( I , - , I1 ), search(I , S , L1 , s1 ). search( “ “ , - , - ,- ) :write(“string not found ” ) , nl, !.

Write a Prolog program to sum ‘n’ integers and find their average. Solution domains predicates go sum( integer , integer , integer ) avg( integer ) clauses. go:- write(“Enter the number of values : ”); readln( N ), avg( N ). avg( N ) :N1 =N, S = 0; sum( N1 , S , N ) sum( o , S , N ) :A = S / N write(“In sum : ”, S ); write(“In Avg : ”, A ), nl, sum( N1 , S , N ) :- write(“Enter the value : ”); read int( x ), SS = S + x, NN = N1 – 1, sum( NN , SS , N ).

Write a Prolog program to reverse list of integer and store it in a separate list. Print the original as well as reverse list. Solution domains : list = integer* predicates : reverse_list( list , list) write_list( list ) reverse( list , list , list ) clauses : reverse_list( L1 , L2 ) :reverse( L1 , [] , L2 ), write_list( L2 ),

reverse( [] , Head | Tail] , L1 , L2):reverse( Tail , [Head | L1] , L2). write_list( [] ). write_list( [x | Tail]) :write( x ), write_list( Tail ). Write a prolog program to extract nth element given list. Solution domains 1st = symbol* predicates go extract( lst , integer , symbol ) clauses go:write(“Enter N:- ”), readint( N ), extract([a , b , c , d], N , 2) write(“Element =”, 2), nl extract([x | _ ], 1 , z):extract([ _ | Tail], N , z):NN = N – 1, Extract(Tail, NN , z). Write a prolog program which counts number of words in a given input string solution domains str = string predicates: go no_words(string , integer , integer ) clauses go:write(“Enter a string :- ”), readln( str ), no_words(str , O , N), write(“No of words = ”, N), nl. no_words(str , X , N):fronttoken(str , F , R), NOT(F = “ ”),

X1 = x + 1, no_words(R , X1 , N). no_words(_ , X , N):N = X. Write a prolog program to enter employee details using compound object emp (eno,ename,designation,salary) Solution domains e =

emp(eno , ename , desgn , salary) ename , desgn = string eno , sal = integers. predicates: read_emp(e) go clauses: go:- read_emp(emp), nl write(“Accept (y/n)? ”). Readchr(Reply), Reply = ‘ y ’, ! go:- nl , write(“please reenter”), Nl, go. Read_emp(emp(Eno , Ename , Desgn , Salary)) Write(“Enter Eno: ”), Readln( Eno ), Write(“Enter Ename: ”), Readln(Ename), Write(“Enter desgn: ”), Readln(Desgn), Write(“Enter salary ”), Readln(salary). Write a prolog program to display Fibonacci series and display sum of series Solution Sum, avg , cnt , num , cnt1 = Integer Predicates: fibo(integer , integer , integer) go clauses: go:- write(“How many terms: ”), readln( num ), sum = 0,

avg = 0, cnt = 1, fibo(num , 0 , 1), write(“sum =, ”sum) avg = sum / num, write(“Avg = ”, avg); go. Fibo(cnt , N1 , N2):Write( N1 ), Sum = sum + N1, N3 = N2, N4 = N1 + N2, cnt = cnt – 1, fibo(cnt , N3 , N4). Write a prolog program for the following 1. Check whether the given number is a prime number. 2. Check whether the given number is a perfect number. Solution 1. Prime Number Predicates go chkprime(integer , integer) clauses go:- write(“Enter the number : ”) readint( N ), x = 2, chkprime(N , X), go:- write(“Given no. is not prime”), nl chkprime(N , X):N, write(“Given no. is prime”), !. chkprime(N , X):Y = N mod X, Y = 0, !,fail. chkprime(N , X):- Y = N mod X, not(y = 0), XX = X + 1, chkprime(N , XX). 2.Perfect N domains Sum, no, cnt = integer

Predicates go perfect(integer , integer , integer) goal go clauses go:- write(“Enter No: ”), readint(No), sum1, cnt=1, perfect(No , cnt , sum) perfect(No , cnt , sum):No = cnt, No = sum, Write(“Number is perfect”), !. perfect(No , cnt , sum):No = cnt, sum < > No Write(“Number is not perfect”), !. perfect(No , cnt , sum):No < > cnt N1 = No mod cnt, N1 = 0 Sum1 = sum1 + cnt, Cnt1 = cnt + 1, perfect(no , cnt1 , sum1). perfect(No , cnt , sum):No < > cnt N1 = No mod cnt, No < > 0 cnt2 = cnt + 1, perfect(No , cnt2 , sum).

Related Documents

Ppl Prolog
November 2019 31
Prolog
October 2019 28
Prolog
November 2019 24
Prolog
June 2020 14
Prolog
November 2019 28
Ppl
November 2019 28