Program Factorialfinder; var input: integer; factorial: integer; count: integer; begin writeln('input a number where you want to find the factorial of'); readln(input); factorial :=1; if input <> 0 then begin for count:=1 to input do begin factorial := factorial * count; writeln('factorial: ',factorial); end; end; readln; end.
(* Question : Write an interactive program that will encode or decode a line of text. To encode a line of text, proceed as follows: 1. Convert each character, including blank spaces, to its ASCII equivalent. 2. Generate a positive random integer. Add this integer to the ASCII equivalent of each character. (The same random number will be used for the entire line of text.) 3. Suppose that N1 represents the lowest permissible value in the ASCII code, and N2 represents the highest permissible value. If the number obtained in step 2 above (i.e., the original ASCII equivalent plus the random integer) exceeds N2, then subtract the largest possible multiple of N2 from this number and add the remainder to N1. Hence the encoded number will always fall between N1 and N2 and will therefore always represent some ASCII character. 4. Print the characters that correspond to the encoded ASCII values. The procedure is reversed when decoding a line of text. Be certain, however, that the same random number is used in decoding as was used in encoding *) program ascii; uses crt; var begin ; end.
program Palindromes; function isPalindromic(s : String) : boolean; var reversedS : String; count : Integer; begin reversedS := ''; for count := Length(s) downto 1 do reversedS := reversedS + s[count]; if reversedS = s then isPalindromic := true else isPalindromic := false; end; var stringToCheck : String; palindrome : boolean; begin WriteLn('Enter the string to check whether it is a palindrome or not.'); Write('> '); ReadLn(stringToCheck); stringToCheck := 'racecar'; // example input palindrome := isPalindromic(stringToCheck); if palindrome = true then WriteLn(stringToCheck + ' is a palindrome!') else WriteLn(stringToCheck + ' is not a palindrome!'); ReadLn; end.
Program CaeserCipher(output); uses sysutils; var plainText : string; cipherKey, i : integer; begin writeln('Enter plaintext:'); readln(plainText); plainText := UpperCase(plainText); writeln('Enter cipher key: '); readln(cipherKey); for i:= 1 to length(plainText) do begin plainText[i] := chr(ord(plainText[i]) + cipherKey); if ord(plainText[i]) >= 91 then plainText[i] := chr(ord(plainText[i]) - 26); end; end.
Program FactorialFinder(output); var userNum, cumulativeNum, i, count : integer; begin writeln('Enter number: '); read(userNum); //Take user input if userNum = 0 then begin count := 1; //Such that 0! produces a result of 1 end else begin count := userNum; end; cumulativeNum := 1; //Set the cumulative total to 1 for i:= 1 to count do begin cumulativeNum := i * cumulativeNum; //recursively multiply the cumulative total end; writeln(userNum, '! is equal to ',cumulativeNum); readln(); end.
program FruitMachine; uses SysUtils; const SYMBOLS : array[0..5] of String = ('cherry', 'bell', 'lemon', 'orange', 'star', 'skull'); procedure showSymbols(symbol1 : String; symbol2 : String; symbol3 : String); var length1, length2, length3 : Integer; symbolWithSpacing1, symbolWithSpacing2, symbolWithSpacing3 : String; begin length1 := Length(symbol1); length2 := Length(symbol2); length3 := Length(symbol3); symbolWithSpacing1 := ' ' + symbol1 + StringOfChar(' ', 7 - length1); symbolWithSpacing2 := ' ' + symbol2 + StringOfChar(' ', 7 - length2); symbolWithSpacing3 := ' ' + symbol3 + StringOfChar(' ', 7 - length3); WriteLn; WriteLn('############################'); WriteLn('# # # #'); WriteLn('# # # #'); WriteLn('#' + symbolWithSpacing1 + '#' + symbolWithSpacing2 + '#' + symbolWithSpacing3 +'#'); WriteLn('# # # #'); WriteLn('# # # #'); WriteLn('############################'); WriteLn; end; function roll() : String; var randomRoll : Integer; symbol1, symbol2, symbol3 : String; rollResult : String; begin randomRoll := Random(Length(SYMBOLS)); symbol1 := SYMBOLS[randomRoll]; randomRoll := Random(Length(SYMBOLS)); symbol2 := SYMBOLS[randomRoll]; randomRoll := Random(Length(SYMBOLS)); symbol3 := SYMBOLS[randomRoll]; showSymbols(symbol1, symbol2, symbol3); rollResult := ''; if (symbol1 = symbol2) and (symbol1 = symbol3) then rollResult := 'triple' else if (symbol1 = symbol2) or (symbol1 = symbol3) or (symbol2 = symbol3) then rollResult := 'double' else rollResult := 'single'; if rollResult = 'triple' then begin if symbol1 = 'bell' then rollResult := 'triplebell' else if symbol1 = 'skull' then rollResult := 'tripleskull'; end else if rollResult = 'double' then begin if symbol1 = 'skull' then rollResult := 'doubleskull'; end; roll := rollResult; end; function getChangeInCredit(rollResult : String; currentCredit : Real) : Real; var changeInCredit : Real; begin case rollResult of 'triple' : changeInCredit := 1.0; 'double' : changeInCredit := 0.5; 'triplebell' : changeInCredit := 5; 'tripleskull' : changeInCredit := -currentCredit; 'doubleskull' : changeInCredit := -1.0; otherwise changeInCredit := 0.0; end; getChangeInCredit := changeInCredit; end; var credit : Real; doRoll : String; rollResult : String; changeInCredit : Real; begin Randomize; WriteLn('WELCOME TO THE FRUIT MACHINE SIMULATOR'); WriteLn; credit := 1.0; while credit > 0.0 do begin WriteLn('You currently have a credit of: $' + FloatToStr(credit)); WriteLn('Each roll costs $0.2'); WriteLn('Would you like to roll on the Fruit Machine? (Y/N)'); ReadLn(doRoll); doRoll := 'Y'; // included to demonstrate in this compiler without inputs if UpperCase(doRoll) <> 'Y' then break; credit := credit - 0.2; rollResult := roll(); changeInCredit := getChangeInCredit(rollResult, credit); credit := credit + changeInCredit; if changeInCredit > 0.0 then WriteLn('You won $' + FloatToStr(changeInCredit) + '!') else if changeInCredit < 0.0 then WriteLn('You lost $' + FloatToStr(changeInCredit) + '!') else WriteLn('No winnings this time!'); WriteLn; end; if credit > 0.0 then WriteLn('You quit playing and took home your winnings.') else WriteLn('You lost all your credit.'); ReadLn; end.
Program Palindromes; var Input:string; _Start,_End:integer; function Reverse(CString:string; _Start,_End: Integer):String; Var Temp:char; begin if (_Start >= _End) then begin if (Input = CString) then writeln(Input + ' is a palindrome') else writeln(Input + ' is not a palindrome'); end else begin Temp := CString[_Start]; CString[_Start] := CString[_End]; CString[_End] := Temp; _Start+=1; _End-=1; Reverse(CString,_Start,_End); end; end; begin readln(Input); _Start := 1; _End := length(Input); Reverse(Input,_Start,_End); readln(); end.
program Factorial; var Find:integer; function FindFactorial(FIndex, CurrentTotal: integer): integer; begin FIndex -= 1; if (FIndex <= 1) then Writeln(CurrentTotal) else begin CurrentTotal := CurrentTotal*FIndex; FindFactorial(FIndex,CurrentTotal); end end; begin readln(Find); FindFactorial(Find,Find); readln(); end.
Program HelloWorld; uses sysutils; var i :Integer; userInput: string; n :Integer; sum :Integer; ai :Integer; begin readln(userInput); n := StrToInt(userInput); // Example: 1, 3, 5, 7, 9, .... // Ai = Ai-1 + 2 // Ai = 1 + 2*i sum := 0; for i:=0 to n-1 do begin ai := 1 + 2 * i; sum := sum + ai; write(ai); if i < n-1 then begin write(', ') end; end; writeln(); writeln('Sum: ', sum); end.
program logicGates; uses SysUtils; var gate : string; in1,in2,out : integer; begin writeln('What gate do you need? '); readln(gate); gate := UpperCase(gate); writeln('First Input'); readln(in1); writeln('Second Input'); readln(in2); if gate = 'OR' then begin if (in1 = 1) or (in2 = 1) then begin out := 1; end else out := 0; end else if gate = 'AND' then begin if (in1 = 1) and (in2 = 1) then begin out := 1; end else out := 0; end else if gate = 'XOR' then begin if ((in1 = 1) and (in2 = 0)) or ((in1 = 0) and (in2 = 1)) then begin out := 1; end else out := 0; end else if gate = 'NAND' then begin if (in1 = 1) and (in2 = 1) then begin out := 0; end else out := 1; end else if gate = 'NOR' then begin if (in1 = 1) or (in2 = 1) then begin out := 0; end else out := 1; end; writeln('OUTPUT: ',IntToStr(out)); readln; end.
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more