0% found this document useful (0 votes)
32 views

ALgorithm

Uploaded by

Dana Sobh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
32 views

ALgorithm

Uploaded by

Dana Sobh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 17
1) Write an algorithm to exchange the values of two variables A and B, regardless of their prior content. output("Enter 2 numbers") input num1 input num2 num3 <-- numi num1 <-- num2 num2 <-- num3 output("The new values are", num1, num2 ) 2) Write an algorithm for transferring to B the value of A, to C the value of B and to A the value of C (always regardless of the prior contents of these variables). . OUTPUT("Enter 3 values ") Input A Input B Input C B<--A C<--B A<--C output("The values are", A, B, C ) 3) Write an algorithm that performs the reading of the time t in seconds, and it displays the time t in days, hours, minutes, seconds. Example: if t= 21,020 seconds the algorithm will cepa O days 5 hours 50 minutes and 20 seconds. output("Enter a time of seconds") input seconds output("It will display d=",d,"h=",h,"m=",m,"s=",s) & ve xGa x60 a ee (anaer x 69) W qe © / (Ge a) oe ch (toxés) me v¢ /0 S — 7.6 4) Write an algorithm and draw a flowchart that will find and print the product of 3 numbers. output("Enter first number") input num1 output("Enter second number") input num2 output("Enter third number") input num3 product = num1* num2* num3 output("The product of these three numbers is" , product) 5) Write an algorithm and draw a flowchart to calculate the average of 5 numbers output("Enter five numbers") input num1 input num2 input num3 input num4 input numS sum = num1+num2+num3+num4+nums. av =sum/S output("The average is ", av) 6. Write an algorithm that calculate and print the result of 1/x° +" fora given x and y. Trace the algorithm with the following values (x=12, y=5). output("Enter the value of x') input x output("Enter the value of y") input y sum = x*x + y*y result = sqrt(sum) output("The answer is ", result) 7) Write an algorithm that asks the user to enter his country and then display his language Ex: USA, UK : English Lebanon: Arabic Paris: Francais output("Enter your country") input country if country = USA or country = UK then: output("English") else if country = Lebanon then: output("Arabic") else if country = Paris then: 1 ‘output("Not a valid country") ave tf ase ih else. solving the previous ex using switch output("Enter your country") input country switch country do: case "US) case "UK"; — Ss output("English") break case "Lebanon": output("Arabic") break case "Paris": output("French") break Cotnepsise output("Not a valid country") _————_—— 8) Write pseudocode to generate the first N numbers in the Fibonacci sequence. ot 2 => ort Ot Nae oN NR sae a 2 as —o Th 32> o!|z222\" co. tna > 8 abt) = ele) +oib[s\= © Gb) = eb (sY +€ib (4) : =% Rife COI + CB) eco) Neh & Exb() sei (a)=* avin © /) bls) a Fib (\ eeili}= . ae. (Se 4) abl) = LO a Cib(at abl\c4 dow coker, Bele!) 4 Cres) ciblo) =e cables if F(n-2) F(n-3) F(n-2) F(n-3) F(n-3)F(n-4) F(n-4)F(n-5) F(n-3) F(n-4) F(a) F(n-5) rite pseudocode for a function that takes a number as an argument and eturns "Fizz", "Buzz" or "FizzBuzz". If the number is a multiple of 3 the output should be "Fizz". If the number given is a multiple of 5, the output should be "Buzz". If the number given is a multiple of both 3 and 5, the output should be "FizzBuzz". If the number is not a multiple of either 3 or 5, the number should be output nits own as shown in the examples below. he output should always be a string even if it is not a multiple of 3 or 5. Examples izz_buzz(3) — "Fizz" fizz_buzz(5) > "Buzz" izz_buzz(15) — "FizzBuzz" fizz_buzz(4) 7 "4" A four-digit number is called Lucky if RN keen CRN mL RCs ole Vem arm ual eed eee aa anor oar The number Ko MU Waal eL-Te ale} AUN sealed reat asta a BNI 2A [19 028A = 3HO_ AZ [Ao “4 S) a 5g =3)\ PALO —> mH gan ct Sa hlo > FR oa [to ~@e® } plese 1, a) tee? U7 input num d4=num/10 r4=num%10 3 d3 =d4/10 r3=d4%10 4 d2=d3/10 ->3 r2=d3% 10» sumFirstTwoDigits = d2 + r2 3 +r4 if sumFirstTwoDigits = sumLastTwoDigits then: sumLast TwoDigit: output("This is a lucky number") else output("This is not a lucky number") QUESTION Write a function that simulates a coin head or tails game. An array should be used to save the two possible options the computer can randomly select from. The user should be able to enter their choice or head or tails. If the user guesses correctly the function outputs ‘Congratulations! You guessed correctly, otherwise outputs ‘Sorry, you guessed wrong’ -declare tails heads : string function tailsOrheads() return string outcomes = ["head","tail"] tailsOrheads = random_number (outcomes) output ("Guess if it is| heads or tails ™) input tails heads if tails_heads = tailsOrheads then: output ("Congratulations! ") else: output ("Your guess is wrong ", tailsOrheads) end function //call the function tailsOrheads () Write a Python function generate_lottery_numbers that generates a list of 6 unique random numbers between 1 and 49, simulating a lottery ticket. Output the numbers outside of the function. function generate_lottery_numbers() return integer lottery_numbers = [] while length(lottery_numbers) <6 random_int = random_number(1, 49) if random_int not in lottery_numbers add_to_list(lottery_numbers, random_int) //call the function lottery_ticket =generate_lottery_numbers() output("Lottery numbers are", lottery_ticket)

You might also like