Project (1) To Prolog
Project (1) To Prolog
CS213 | 462
PROGRAMS:............................................................................................................................7
PROGRAMS:............................................................................................................................38
x:
for i := 1 to 10 do writeln(i);
• while...do: Used when the number of iterations is not
known in advance.
repeat
writeln(x);
X := X + 1; until x > 10;
• case choice of
• 1: writeln('Option 1');
• 2: writeln('Option 2');
• else
program PerimeterSquare;
var side, perimeter: integer; begin
write('Enter the side length of the square: ');
readln(side); perimeter := 4 * side; writeln('The perimeter
of the square is: perimeter); end.
,r
ff I If
ff the outpot is
f f
Code 2:Check if a Number is Even or Odd
\I I I I I I I I I I the outpot is
,1 ff If ff I If the outpot is
Name : Nayil Fahhad ALharbi
var
player1, player2: string;
begin
clrscr;
// Validate input
if not ((player1 = 'rock') or (player1 = 'paper') or
(player1 = 'scissors')) or
not ((player2 = 'rock') or (player2 = 'paper') or
(player2 = 'scissors')) then
begin
writeln('Invalid input! Please enter only "Rock",
"Paper", or "Scissors".');
Halt;
end;
Output:
result := '';
if i < 10 then
result := result + sLineBreak;
end;
WriteLn(result); end.
Output:
var
n, i, value, count, sum: Integer;
begin
Write('Enter the number of elements: ');
ReadLn(n);
if n <= 0 then
begin
WriteLn('[]');
Exit;
end;
count := 0;
sum := 0;
for i := 1 to n do
begin
ReadLn(value);
if value > 0 then
Inc(count)
else if value < 0 then
sum := sum + value;
end;
Output
Output:
Output:
Enter temperature in Celsius: 36
Temperature in Fahrenheit: 96.80 °F
Temperature in Kelvin: 309.15 K
Code 3: Guess the Number game
program GuessTheNumber; uses crt; var secret, guess: integer;
begin randomize; secret := random(100) + 1;
writeln(' Welcome to the Guessing Game!'); writeln(' Try to
guess the secret number between 1 and
100'); repeat
Output:
Welcome to the Guessing Game!
Try to guess the secret number between 1 and 100 Enter your
guess: 5
The secret number is higher!
Enter your guess: 99 The
secret number is lower!
Enter your guess: 95 The
secret number is lower! Enter
your guess: 94 The secret
number is lower! Enter your
guess: 92 The secret number
is higher! Enter your guess:
93
Output:
Output:
Output:
Program 2: Books Library
program BookLibrary; uses crt;
type Book = record
Title: string;
Author: string;
Date: string;
end; var
books: array[1..50] of Book;
bcount, i, select: integer;
index: integer; title,
author, date: string;
procedure Add; begin
if bcount < 50 then
begin
writeln('Enter the Title:');
readln(title);
writeln('Enter the Author name:');
readln(author);
writeln('Enter the Publication date:');
readln(date); bcount := bcount + 1;
books[bcount].Title := title;
books[bcount].Author := author;
books[bcount].Date := date;
writeln('Book added successfully.');
delay(2000) ; end else
writeln('The library is full.');
writeln('_'); writeln; end;
procedure Display; begin if
bcount = 0 then begin
writeln('The library is empty.');
delay(2000); end else begin
for i := 1 to bcount do
begin
writeln('Book ', i, ':'); writeln('Title
: ', books[i].Title); writeln('Author : ',
books[i].Author); writeln('Publication Date :
', books[i].Date);
TextColor(12);
writeln;
writeln('(Press any key to continue)');
TextColor(white);
writeln;
readln(); end; end;
writeln('_');
writeln; end;
procedure delete;
begin
writeln('Enter the index of the book to delete : ');
readln(index);
if (index > 0) and (index <= bcount) then
begin
for i := index to bcount - 1 do
books[i] := books[i + 1];
bcount := bcount - 1;
writeln('Book deleted successfully');
delay(2000); end else
writeln('Invalid index');
writeln('_');
delay(2000) ; writeln;
end; begin bcount := 0;
repeat writeln;
writeln('1. Add Book');
writeln('2. Display Books');
writeln('3. delete Book');
writeln('4. Exit');
writeln('_');
write('Enter your choice: ');
readln(select); writeln;
case select of 1: Add;
2: Display;
3: delete;
end; until
select = 4;
write('Thanks for using the program'); end.
Output:
clrscr;
writeln('Parking Classes and Proximity:');
writeln('Gold: $5.00 per hour (10m~ close to the building)');
writeln('Silver: $3.00 per hour (50m~ from the building)');
writeln('Bronze: $2.00 per hour (100m~ from the building)');
Output:
Part 2: Functional Programming, Scheme.
programs:
Naif suliman alharbi:
Code 1: Calculate the Perimeter of a Square
(display perimeter)
(newline)
Outpot:
Code 2: Check if a Number is Even or Odd
(newline)
The outpot:
Code 3: Choose Coffee or Tea
(cond
[(or (char=? choice #\c) (char=? choice #\C)) (display
"Here is your Coffee!")]
[(or (char=? choice #\t) (char=? choice #\T)) (display
"Here is your Tea!")]
[else (display "Invalid choice!")])
(newline)
The outpot:
Name : Nayil Fahhad Alharbi
Code 1: computing the sum of squares of numbers ( the previous examble “Code 1” has changed)
(define (get-number)
(begin
(display "Enter a number (1 to 10): ")
(let ((n (string->number (read-line))))
(if (and n (<= 1 n 10)) n
(begin (display "Invalid input! Try again.\n")
(get-number))))))
(define (sum-of-squares n)
(if (= n 0) 0 (+ (* n n) (sum-of-squares (- n 1)))))
(define (sum-of-squares-table n)
(for-each (lambda (i) (display (format "~a^2 = ~a\n" i
(* i i)))) (iota n 1)))
(define (main)
(let ((n (get-number)))
(display "\nSquares Table:\n")
(sum-of-squares-table n)
(display (format "\nSum of squares = ~a\n" (sum-of-
squares n)))))
(main)
Output:
Code 2: Multiplication table for number.
(multiplication-table (get-number))
Output:
Code 3: Count of positives/ sum of Negatives.
(define (count-positives-sum-negatives n)
(if (<= n 0)
(display "[]")
(let loop ((i 1) (count 0) (sum 0))
(if (> i n)
(begin
(display "Count of positives: ") (display
count) (newline)
(display "Sum of negatives: ") (display sum)
(newline))
(let ((value (read)))
(loop (+ i 1)
(if (> value 0) (+ count 1) count)
(if (< value 0) (+ sum value)
sum)))))))
Output:
Output:
(define (guess-the-number)
(define secret (+ 1 (random 100)))
(define (game-loop)
(display "\n Enter your guess: ")
(flush-output)
(let ((guess (read)))
(cond
((< guess secret)
(display "The secret number is higher!")
(game-loop))
((> guess secret)
(display "The secret number is lower!")
(game-loop))
(else
(display "🎉 Congratulations! You guessed the
correct number!")))))
(display "Welcome to the Guessing Game!\n")
(display "Try to guess the secret number between 1 and
100\n")
(game-loop))
Output:
Output:
Code 3: Count Characters:
Output:
Name : Mohammed Fahad Al-Motawa
((= choice 1)
(area-calc))
((= choice 2)
(perimeter-calc))
(else
(display "Invalid choice. Please try again.")
(newline)))))
(define (area-calc)
(display " | Area Calculation Options |")
(newline)
(display " | 1: Triangle | (requires height and
base)")
(newline)
(display " | 2: Circle | (requires radius)")
(newline)
(display " | 3: Rectangle | (requires length and
width)")
(newline)
((= shape 2)
(display "Enter Radius: ")
(let ((radius (read)))
(display "Circle Area = ")
(display (* 3.14 radius radius))
(newline)))
((= shape 3)
(display "Enter Length: ")
(let ((length (read))
(width (begin
(display "Enter Width: ")
(read))))
(display "Rectangle Area = ")
(display (* length width))
(newline)))
(else
(display "Invalid option for area calculation.")
(newline)))))
(define (perimeter-calc)
(display " | Perimeter Calculation Options |")
(newline)
(display " | 1: Triangle | (requires lengths of
three sides)")
(newline)
(display " | 2: Circle | (requires radius)")
(newline)
(display " | 3: Rectangle | (requires length and
width)")
(newline)
((= shape 2)
(display "Enter Radius: ")
(let ((radius (read)))
(display "Circle Circumference = ")
(display (* 2 3.14 radius))
(newline)))
((= shape 3)
(display "Enter Length: ")
(let ((length (read))
(width (begin
(display "Enter Width: ")
(read))))
(display "Rectangle Perimeter = ")
(display (* 2 (+ length width)))
(newline)))
(else
(display "Invalid option for perimeter calculation.")
(newline)))))
(main)
Output:
(define (add-book)
(if (< (length books) 50)
(let* ((title (begin (display "Enter the Title: ") (read-line)))
(author (begin (display "Enter the Author name: ") (read-
line)))
(date (begin (display "Enter the Publication date: ")
(read-line))))
(set! books (append books (list (list title author date))))
(display "Book added successfully.") (newline))
(display "The library is full.")))
(define (display-books)
(if (null? books)
(begin
(display "The library is empty.") (newline))
(let loop ((bks books) (index 1))
(unless (null? bks)
(display "Book ") (display index) (display ":") (newline)
(display "Title: ") (display (car (car bks))) (newline)
(display "Author: ") (display (cadr (car bks))) (newline)
(display "Publication Date: ") (display (caddr (car bks)))
(newline)
(display "(Press Enter to continue)") (newline)
(read-line)
(loop (cdr bks) (+ index 1))))))
(define (delete-book)
(display "Enter the index of the book to delete: ") (newline)
(let ((index (read)))
(read-line) ;; Consume newline after read
(if (and (> index 0) (<= index (length books)))
(begin
(set! books (append (take books (- index 1)) (drop books
index)))
(display "Book deleted successfully.") (newline))
(display "Invalid index."))))
(define (library-menu)
;; Initialization before loop
(display "Welcome to the Book Library System!") (newline)
(display "Initializing library...") (newline)
(let loop ()
(display "\n1. Add Book\n2. Display Books\n3. Delete Book\n4.
Exit\n")
(display "Enter your choice: ")
(let ((choice (read)))
(read-line) ;; Consume newline after reading choice
(cond
((= choice 1) (add-book))
((= choice 2) (display-books))
((= choice 3) (delete-book))
((= choice 4) (begin
(display "Thanks for using the program.")
(newline)
(display "(Press Enter to continue)")
(newline)
(read-line)
(exit)))
(else (display "Invalid choice, try again."))))
(loop)))
(library-menu)
Output:
(define (parking-reservation)
(define plate "")
(define hours 0)
(define parkClass "")
(define rate 0.0)
(define invoiceBeforeTax 0.0)
(define tax 0.0)
(define totalAfterTax 0.0)
(display "---------------------------\n")
(display "Invoice for Parking:\n")
(display (string-append "Car Plate: " plate "\n"))
(display (string-append "Hours: " (number->string hours)
"\n"))
(display (string-append "Class: " parkClass "\n"))
(display (string-append "Invoice Before Tax: $" (number-
>string invoiceBeforeTax) "\n"))
(display (string-append "Tax (15%): $" (number->string
tax) "\n"))
(display (string-append "Total After Tax: $" (number-
>string totalAfterTax) "\n"))
(display "---------------------------\n")))
(begin
(display "Invalid park class entered. Please try again.\
n")
(loop))))))
(parking-reservation)
Output:
Part 3 : prolog .
calculate_perimeter :-
write('Enter the side length of the square: '),
read(Side),
Perimeter is 4 * Side,
write('The perimeter of the square is: '),
write(Perimeter), nl.
Outpot :
Code 2:Check if a Number is Even or Odd
check_even_or_odd :-
write('Enter a number: '),
read(Num),
( Num mod 2 =:= 0
-> write('The number is even.')
; write('The number is odd.')
), nl.
Outpot:
rock_paper_scissors :-
string_lower(P1Input, P1),
string_lower(P2Input, P2),
( P1 = P2 ->
writeln('Draw!')
writeln('Player 1 won!')
; writeln('Player 2 won!') )
).
:- rock_paper_scissors.
Outpot:
Code 2: Multiplication table for number.
start :-
number_string(N, Input),
print_table(1, N)
).
print_table(11, _) :- !.
print_table(I, N) :-
Product is I * N,
I1 is I + 1,
print_table(I1, N).
:- start.
Outpot:
Code 3: Count of positives/ sum of Negatives.
start :-
number_string(N, Input),
( N =< 0 ->
writeln('[]')
loop(N, 0, 0)
).
N > 0,
read_line_to_string(user_input, Input),
number_string(Value, Input),
NewCount is Count + 1,
NewSum = Sum
NewCount = Count,
; % Value = 0
NewCount = Count,
NewSum = Sum
),
N1 is N - 1,
:- start.
Outpot: