8 views

Uploaded by aryanboxer786

- Session 7-Technique for Order Performance by Similarity to Ideal
- DTFT
- Snake and Ladder Program
- Shortcuts
- Conics Sections Scan Line Algorithm
- 080 Section Hand Out 1
- 116rationaltg
- Design and Analysis of Algorithms - VTU Life [20ebooks.com]
- [Wissam Raji] an Introductory in Elementary Number Theory
- introductiontopython-111006011932-phpapp01
- 11Math_G7_FL_03-02-B
- word-to-pdf
- QA Shortcuts
- Mathematics
- Definitions
- Formula Sheet
- int 2 set 1
- SAMPLE PROJECT REPORT
- Creating Network Graphs With Python
- Dictionary Encoding

You are on page 1of 4

Exercise 9

Exercise 9

Elementary Algorithms

Write a C++ program for implementation of following elementary algorithms. a) Compute GCD (greatest common divisor) of two given positive integers. b) Factorial of given number using recursion c) Procedure to calculate sum of squares of two integer numbers d) Program to find the maximum and minimum in set of n integer elements. Objective: the objective of this exercise enable you to perform GCD of two numbers, factorial of given number, sum of squares of two integer numbers, maximum and minimum in set of n integer elements. Procedure and Description: a) GCD of two positive integers: In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), of two non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. Algorithm GCD Euclid (m, n) //m,n:Positive integers This algorithm computes the greatest common divisor of two given positive integer Step: 1 begin {of algorithm} Step: 2 while n 0 do begin {of while loop} r m mod n;{r:a new variable is used to store the remainder which is obtained by dividing m by n, with 0 < r < m} m n; {the value of n is assigned as new value of m; but at this stage, value of n remains unchanged}

Sikkim Manipal University Page No.: 34

Exercise 9

n r, {the value of r becomes the new value of n and the value of r remains unchanged} end {of while loop} Step: 3 return (n) Step: 4 end; {of algorithm} Expected Output: After executing the program enter two positive integer numbers to find gcd. Resultant value is output. Example: Enter two positive integers: 20 16 Output: GCD of two numbers is: 4 b) Factorial of given number using recursion: In a simple language we can define recursion as a programming technique in which a function may call itself. Algorithm: Procedure factorial (n) //n:integer number Step: 1 fac; integer: begin Step: 2 fac 1 Step: 3 if n equals 1 then return fac Step: 4 else begin fac n * factorial (n 1) return (fac) end; Step: 5 Exit Expected Output: After executing the programs enter an integer number .Program takes the input and executes the factorial recursion function then final factorial function value is resultant output. Example: enter a number: 4 Output: factorial of given number is: 24

Page No.: 35

Exercise 9

c) Sum of squares of two integer numbers This program gives the steps to calculate sum of squares of two integer numbers Algorithm: Step: 1 Procedure sum-square (a, b: integer) integer; {denoted the inputs a and b integers and the output is also an integer} Step: 2 S: integer; {to store the required number} Step: 3 begin

S a2 b2 Step: 4 Step: 5 Returns (S) Step: 6 end Expected Output: After executing the program enter two integer numbers then by performing algorithm steps, it calculates the sum of squares of two integer numbers, the resultant value is output. Example: enter two integer numbers: 6 8 Output: sum of squares of two integer numbers: 100

d) The maximum and minimum in set of n integer elements. This program gives the steps for finding maximum value and minimum value in set of n integer elements. Algorithm: Step: 1 procedure SMAXMIN (max, min) //max,min:integer numbers Step: 2 integer i, max, min; //i: used as index Step: 3 max min A(1);//A: contains set of elements Step: 4 for I 2 to n do Step: 5 if A(i)>max then max A(i) Step: 6 else if A(i)<min then min A(i) Step: 7 end if Step: 8 end if Step: 9 repeat Step: 10 end SMAXMIN

Page No.: 36

Exercise 9

Expected Output: After executing the program. Enter set of n integer numbers. Before that enter n value. Then program executes algorithm steps and gives the output values i.e. the maximum value and minimum value. Example: Enter n value: 8 Enter set of integer values: 4 8 12 16 3 10 21 7 Output: the maximum and minimum values: 21 3

Page No.: 37

- Session 7-Technique for Order Performance by Similarity to IdealUploaded byVikram Santhanam
- DTFTUploaded bykrishatanug
- Snake and Ladder ProgramUploaded byManikandan
- ShortcutsUploaded byiiht madurai
- Conics Sections Scan Line AlgorithmUploaded bySouyama Debnath
- 080 Section Hand Out 1Uploaded bydodoman666
- 116rationaltgUploaded byLouis Guy
- Design and Analysis of Algorithms - VTU Life [20ebooks.com]Uploaded byrsi127287
- [Wissam Raji] an Introductory in Elementary Number TheoryUploaded bymp19uy
- introductiontopython-111006011932-phpapp01Uploaded bysabar5
- 11Math_G7_FL_03-02-BUploaded byKyle Redula
- word-to-pdfUploaded byWijayanti Tri
- QA ShortcutsUploaded byAshishManerikar
- MathematicsUploaded byChintu Mahadani
- DefinitionsUploaded byadddata
- Formula SheetUploaded byJonelle Morris-Dawkins
- int 2 set 1Uploaded byKarthickm92
- SAMPLE PROJECT REPORTUploaded byapi-19976925
- Creating Network Graphs With PythonUploaded byiggix
- Dictionary EncodingUploaded bychandhureddy
- slws2aUploaded bynoor
- Test1 SolutionsUploaded byKishor Ks
- Signals and Systems 8.pptUploaded byRodolfo García Galaz
- Advanced Thermal Analysis of Underground Power Cables[1]Uploaded bymealsibai
- Unit 3 ProblemUploaded bythamizt
- Tetali.CVUploaded byMohit Bhalla
- On the parity of the Smarandache function, by XIONG Wen-jingUploaded byAnonymous 0U9j6BLllB
- Mobile Computing and Simulation october 2009Uploaded byFamida Begam
- ON THE DIVISOR PRODUCTS AND PROPER DIVISOR PRODUCTS SEQUENCESUploaded byRyanElias
- TWO PHASE RETRIAL QUEUEING SYSTEM WITH IMPATIENT CUSTOMERS, ADDITIONAL OPTIONAL SERVICE, SERVER BREAKDOWN, DELAYED REPAIR AND RESERVED TIMEUploaded byebenesarb

- AmitUploaded byaryanboxer786
- Leicester.docxUploaded byaryanboxer786
- inplant Edit.xlsxUploaded byaryanboxer786
- Structure.docxUploaded byaryanboxer786
- Avaneesh SainiUploaded byaryanboxer786
- Syllabus for Testing of Footwear Materials & Complete FootwearUploaded byaryanboxer786
- surjeet (1).docUploaded byaryanboxer786
- PPT LASTING tameem.pptUploaded byaryanboxer786
- eco.docxUploaded byaryanboxer786
- eco.docxUploaded byaryanboxer786
- SHWETANK2Uploaded byaryanboxer786
- Sanjeev Thakur.docxUploaded byaryanboxer786
- PURPOSE OF SKIVIING.docxUploaded byaryanboxer786
- What is a TextureUploaded byaryanboxer786
- Syllabus for Testing of Footwear Materials & Complete Footwear.docxUploaded byaryanboxer786
- hasan ppt.pptUploaded byaryanboxer786
- lasting FAQ.docxUploaded byaryanboxer786
- Management Information SystemUploaded byaryanboxer786
- Clicking FAQUploaded byaryanboxer786
- MK0010-sales distribution.docxUploaded byaryanboxer786
- MB0052_-_Strategic_Management_and_Business_Policy.docUploaded byaryanboxer786
- Girendra Fmt 21Uploaded byaryanboxer786
- CFTIUploaded byaryanboxer786
- 1339031540_597183Uploaded byTusharr Ahuja
- u Lip MutualUploaded byaryanboxer786
- CFTI.pptxUploaded byaryanboxer786
- AbhishekUploaded byaryanboxer786
- 0244315001424516141.pdfUploaded byaryanboxer786
- 1154345446_597183Uploaded byTusharr Ahuja
- Closing FAQ.docxUploaded byaryanboxer786

- chapter 7 replication,Uploaded byHarpinder Kaur
- BGS Users GuideUploaded byProfessor Puppies
- .Net FrameworkUploaded bysaditya76
- SAP ABAP MATERIALS_ ABAP REPORTS.pdfUploaded bySrinivasDukka
- 2373B-ENU-TrainerHandbookUploaded bypieroviano
- Java Programming Lab Manual (r13) (1)Uploaded byShalemRaj
- Errata_Programming ArcGIS 10.1 With Python CookbookUploaded byLouis Rice
- MVC in PHP, A Real World Example - Tom ButlerUploaded byamaniel
- EN - Exploiting Stack Buffer Overflows in the Linux x86 Kernel.pdfUploaded byArman Khandaker
- Sublime Text Unofficial DocumentationUploaded byJavi Ballester Tomàs
- ArraysUploaded byForkenstein
- 61131300 File c Program Files Microchip Mplab Ide Readmes Readme for Pk3cmdUploaded byBlack Book
- Cursor for LoopUploaded byOudaye Ramdhean
- Prolog TutorialUploaded byParth Pithdia
- 2010Uploaded bysanyasirao1
- System SettingsUploaded bySwapnil Bankar
- asm-guideUploaded bykaziujunk
- DOM Based XSS and Proper Output EncodingUploaded byLily Kang
- Try Out PanelUploaded byMaha Shiv Guruji
- Basic Core Java Interview QuestionsUploaded bykittu458
- Click BookUploaded byMuhammad Wasim
- Micromine 10 User GuideUploaded bySeamus Donovan
- review question OOAD.docxUploaded bynangaayissi
- ArenaBasicsUploaded bydendays7
- ALV Object Model - Simple 2D Table - The BasicsUploaded byduchuy85
- IMS-pptUploaded byjoebite
- LINQ to SQLUploaded byFily Cariño Rojas
- Q'sUploaded bykampli_a
- Le in TutorialUploaded bychrisnp
- SortUploaded byNagarajan Venkatesan