


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various algorithms to solve different problems such as adding numbers, converting temperatures from Celsius to Fahrenheit, finding the area and perimeter of a circle, swapping numbers, finding the smallest number, finding the sum of a series, finding the factorial of a number, finding the largest number among three numbers, and sorting an array using Bubble Sort Algorithm. It also includes explanations of algorithms and code.
Typology: Exercises
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















BY HIMAL BHANDARI
(^) an algorithm is a set of well-defined instructions to solve a particular problem. It takes a set of input and produces a desired output. For example, (^) An algorithm to add two numbers: Step 1:start Step 2:Take two number inputs Step 3:Add numbers using the + operator Step4:Display the result Step 5: stop
Step 1: Step 2:Fill up the kettle with water. Step 3: Boil the kettle. Step 4: Place a teabag in your favorite mug. Step 5: Pour boiling water into your favorite mug. Step 6: Brew the tea for a few moments. Step 7: Remove and dispose of the teabag. Step 8: Add milk. Step 9: Add sugar. Step 10: Stop
Step-1: Start Step-2: Input temperature in Celsius say C Step-3: F = (9.0/5.0 x C) + 32 Step-4: Display Temperature in Fahrenheit F Step-5: Stop
Algorithm & Flowchart to Swap Two Numbers using Temporary Variable Step-1: Start Step-2: Input Two Numbers Say NUM1,NUM Step-3: Display Before Swap Values NUM1, NUM Step-4: TEMP = NUM Step-5: NUM1 = NUM Step-6: NUM2 = TEMP Step-7: Display After Swap Values NUM1,NUM Step-8: Stop
Algorithm & Flowchart to find the smallest of two numbers: Step-1: Start Step-2: Input two numbers say NUM1,NUM Step-3: IF NUM1 < NUM2 THEN print smallest is NUM ELSE print smallest is NUM Step-4: Stop
Step-1: Start Step-2: Input Value of N Step-3: I = 1, SUM= Step-4: IF (I >N) THEN GO TO Step- Step-5: SUM = SUM + I Step-6: I = I + 1 Step-7: Go to step- Step-8: Display value of SUM Step-9: Stop
Step 1: Start Step 2: Declare variables n, factorial and i. Step 3: Initialize variables factorial ← 1 i ← 1 Step 4: Read value of n Step 5: Repeat the steps until i = n 5.1: factorial ← factorial*i 5.2: i ← i+ Step 6: Display factorial Step 7: Stop
Step1: Start Step2: begin BubbleSortAlgorithm( Array ) Step3:For all the elements of the array step3.1: if array[i] > array [i + 1] switch ( array[i] , array[i+1]) end if end for Step 4: return array Step5: end BubbleSortAlgorithm
Step1: Start // 1st^ pass, 2nd^ pass … (n-1) Step2: begin BubbleSortAlgorithm( Array ) Step3:For all the elements of the array till (n-1) step3.1: Starting from the first index, compare the first and the second elements. step3.2: If the first element is greater than the second element, they are swapped. The above Step5: endprocess goes on until the last element. Step 4: Display array
STEP 2: Initialize a variable temp to 1. STEP 3: Iterate a “for” loop from 2 to num-1. STEP 4: If num is divisible by lo op iterator, then update temp value to 0. STEP 5: If the temp is equal to 1, Return “Num IS PRIME”. Else, Return “Num IS NOT PRIME”. Step6: Stop
(^) Algorithm: is a series of steps for solving a problem, completing a task or performing a calculation. Algorithms are usually executed by computer programs but the term can also apply to steps in domains such as mathematics for human problem solving. (^) Code is a series of steps that machines can execute. In many cases, code is composed in a high level language that is then automatically translated into instructions that machines understand. (^) The term algorithm is traditionally used to indicate code that has a highly optimized design such as a well accepted solution to a complex problem in computer science. The term suggests code that has a rigorous design that solves a daunting problem.
(^) code generation is the process by which a compiler's code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine.
(^) Compilers, interpreters, translate programs written in high-level languages into machine code that a computer understands. And assemblers translate programs written in low-level or assembly language into machine code.
**1. Compiler