SlideShare a Scribd company logo
1 of 21
Algorithms & Pseudocode
BY M. QURESHI
 A typical programming task can be divided into two phases:
 Problem solving phase
 produce an ordered sequence of steps that describe solution of problem
 this sequence of steps is called an algorithm
 Implementation phase
 implement the program in some programming language
Steps in problem Solving
 Produce a general Algorithm
 Refine the algorithm Successively to get step by step detailed algorithm
that is very close to a computer language.
 Pseudocode is an artificial and informal language that helps programmers
develop algorithms Pseudocode is very similar to everyday English
Pseudocode
 The first thing we do when designing a program is to decide
on a name for the program
 Let’s say we want to write a program to calculate interest, a
good name for the program would be CalculateInterest.
 Note the use of CamelCase.
 Pseudocode is used to represent the logic of the solution to a problem.
 Once the pseudocode is verified, it can be converted into a program using the
vocabulary and syntax of a programming language
 Keywords in Pseudocode.
 Begin … End: these keywords are used to start and finish pseudocode. Begin is
the first line and end is the last line of pseudocode.
 Accept: This keyword is used to obtain an input from a user.
 Display: this keyword is used to present a result or an output.
 If .. Else .. End if : These keywords are used in decision-making.
 //: Comment
 Do .. While, for … repeat … until : Represent loop
So we start the program as :
 Program CalculateInterest:
 And in general it’s :
 Our Program will finish with the following
 END.
PROGRAM <ProgramName>:
END.
So let’s say we want to express the
following algorithm
 Read in a number and print it out double the number.
PROGRAM PrintDoubleNumber:
Read A;
B = A*2;
Print B;
End.
So let’s say we want to express the
following algorithm
 Read in a number, check if it is odd or even.
PROGRAM IsOddOrEven:
Read A;
If (A/2 gives a remainder)
Then Print “Its’s Odd”;
Else Print ”It’s Even”;
EndIf;
End.
So let’s say we want to express the following
algorithm to print out the bigger of two
numbers:
 Read in two numbers, call them A and B. Is A is bigger than B, print out A,
otherwise print out B.
PROGRAM PrintBiggerOfTwo:
Read A;
Read B;
If (A>B)
Then Print A;
Else Print B;
EndIf;
End.
So let’s say we want to express the following
algorithm to print out the bigger of three
numbers:
 Read in three numbers, call them A, B and C.
 If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C.
 If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.
PROGRAM BiggerOfThree:
Read A;
Read B;
Read C;
If (A>B)
then If (A>C)
Then Print A;
Else Print B;
End If;(B>C)
Then Print B;
Else Print C;
End If;
End If;
End.
So let’s say we want to express the
following algorithm:
 Print out the numbers from 1 to 5
PROGRAM Print1to5:
A=1;
WHILE ( A !=6)
Do Print A;
A= A+1;
ENDWHILE;
End.
So let’s say we want to express the
following algorithm:
 – Add up the numbers 1 to 5 and print out the result
PROGRAM PrintSum1to5:
Total =0;
A=1;
WHILE ( A !=6)
Do Total = total +A;
A= A+1;
ENDWHILE;
Print Total;
End.
So let’s say we want to express the
following algorithm:
 – Read in a number and check if it’s a prime number.
 What’s a prime number?
 – A number that’s only divisible by itself and 1, e.g. 7.
 Or to put it another way, every number other than itself and 1 gives a remainder,
e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime.
 – So all we need to do is divide 7 by all numbers less than it but greater than one,
and if any of them have no remainder, we know it’s not prime.
 So,
 If the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime.
 If the number is 9, we know that 8, 7, 6, 5, and 4, all give remainders, but 3 does
not give a remainder, it goes evenly into 9 so we can say 9 is not prime.
 So remember, – if the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder,
prime.
 So, in general, – if the number is A, as long as A-1, A-2, A-3, A4, ... 2 give a
remainder, A is prime.
PROGRAM prime:
Read A;
B = A-1;
IsPrime = True;
While(B !=1)
DO IF ( A/B gives no remainder)
THEN IsPrime = false;
End If
B=B-1;
ENDWHILE;
If(IsPrime ==true)
Then Print “Prime”;
Else Print “Not Prime”;
EndIf;
End.
Write Algorithm & Pseudocode & then
Actual Code
Write a Program to Reverse an Integer
// --- Directions
// Given an integer, return an integer that is the reverse
// ordering of numbers.
// --- Examples
// reverseInt(15) === 51
// reverseInt(981) === 189
// reverseInt(500) === 5
// reverseInt(-15) === -51
// reverseInt(-90) === -9
Write a Program to Reverse a String
// --- Directions
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// reverse('apple') === 'leppa'
// reverse('hello') === 'olleh'
// reverse('Greetings!') === '!sgniteerG'
Write a program to Find Palindromes
 // --- Directions
 // Given a string, return true if the string is a palindrome
 // or false if it is not. Palindromes are strings that
 // form the same word if it is reversed. *Do* include spaces
 // and punctuation in determining if the string is a palindrome.
 // --- Examples:
 // palindrome("abba") === true
 // palindrome("abcdefg") === false
Write a Program that will capitalize a
sentence
 // --- Directions
 // Write a function that accepts a string. The function should
 // capitalize the first letter of each word in the string then
 // return the capitalized string.
 // --- Examples
 // capitalize('a short sentence') --> 'A Short Sentence'
 // capitalize('a lazy fox') --> 'A Lazy Fox'
 // capitalize('look, it is working!') --> 'Look, It Is Working!'
Write a function that returns the number
of vowels
 // --- Directions
 // Write a function that returns the number of vowels
 // used in a string. Vowels are the characters 'a', 'e'
 // 'i', 'o', and 'u'.
 // --- Examples
 // vowels('Hi There!') --> 3
 // vowels('Why do you ask?') --> 4
 // vowels('Why?') --> 0

More Related Content

What's hot

Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to PseudocodeDamian T. Gordon
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
Chapter 2 Representation Of Algorithms 2
Chapter 2  Representation Of  Algorithms 2Chapter 2  Representation Of  Algorithms 2
Chapter 2 Representation Of Algorithms 2Li-Anne Serrano
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approachAkash Sethiya
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowchartsSamuel Igbanogu
 
Part I:Introduction to assembly language
Part I:Introduction to assembly languagePart I:Introduction to assembly language
Part I:Introduction to assembly languageAhmed M. Abed
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1srmohan06
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityAakash Singh
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 

What's hot (20)

Control Structures
Control StructuresControl Structures
Control Structures
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Chapter 2 Representation Of Algorithms 2
Chapter 2  Representation Of  Algorithms 2Chapter 2  Representation Of  Algorithms 2
Chapter 2 Representation Of Algorithms 2
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Binary codes
Binary codesBinary codes
Binary codes
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
Part I:Introduction to assembly language
Part I:Introduction to assembly languagePart I:Introduction to assembly language
Part I:Introduction to assembly language
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Pascal for beginers tute
Pascal for beginers   tutePascal for beginers   tute
Pascal for beginers tute
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
 
C++
C++C++
C++
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 

Similar to Algorithm and psuedocode

Similar to Algorithm and psuedocode (20)

Basic Computer Programming
Basic Computer ProgrammingBasic Computer Programming
Basic Computer Programming
 
Simple perl scripts
Simple perl scriptsSimple perl scripts
Simple perl scripts
 
Learn Programming with Livecoding.tv http://goo.gl/tIgO1I
Learn Programming with Livecoding.tv http://goo.gl/tIgO1ILearn Programming with Livecoding.tv http://goo.gl/tIgO1I
Learn Programming with Livecoding.tv http://goo.gl/tIgO1I
 
Simple perl scripts
Simple perl scriptsSimple perl scripts
Simple perl scripts
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
programming.ppt
programming.pptprogramming.ppt
programming.ppt
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbook
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
10th class computer science notes in english by cstechz
10th class computer science notes in english by cstechz10th class computer science notes in english by cstechz
10th class computer science notes in english by cstechz
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
 
How to Program
How to ProgramHow to Program
How to Program
 
01 Programming Fundamentals.pptx
01 Programming Fundamentals.pptx01 Programming Fundamentals.pptx
01 Programming Fundamentals.pptx
 
Chapter 2- Prog101.ppt
Chapter 2- Prog101.pptChapter 2- Prog101.ppt
Chapter 2- Prog101.ppt
 
Programming
ProgrammingProgramming
Programming
 
Q basic ch
Q basic chQ basic ch
Q basic ch
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Algorithm and psuedocode

  • 2.  A typical programming task can be divided into two phases:  Problem solving phase  produce an ordered sequence of steps that describe solution of problem  this sequence of steps is called an algorithm  Implementation phase  implement the program in some programming language
  • 3. Steps in problem Solving  Produce a general Algorithm  Refine the algorithm Successively to get step by step detailed algorithm that is very close to a computer language.  Pseudocode is an artificial and informal language that helps programmers develop algorithms Pseudocode is very similar to everyday English
  • 4. Pseudocode  The first thing we do when designing a program is to decide on a name for the program  Let’s say we want to write a program to calculate interest, a good name for the program would be CalculateInterest.  Note the use of CamelCase.
  • 5.  Pseudocode is used to represent the logic of the solution to a problem.  Once the pseudocode is verified, it can be converted into a program using the vocabulary and syntax of a programming language  Keywords in Pseudocode.  Begin … End: these keywords are used to start and finish pseudocode. Begin is the first line and end is the last line of pseudocode.  Accept: This keyword is used to obtain an input from a user.  Display: this keyword is used to present a result or an output.  If .. Else .. End if : These keywords are used in decision-making.  //: Comment  Do .. While, for … repeat … until : Represent loop
  • 6. So we start the program as :  Program CalculateInterest:  And in general it’s :  Our Program will finish with the following  END. PROGRAM <ProgramName>: END.
  • 7. So let’s say we want to express the following algorithm  Read in a number and print it out double the number. PROGRAM PrintDoubleNumber: Read A; B = A*2; Print B; End.
  • 8. So let’s say we want to express the following algorithm  Read in a number, check if it is odd or even. PROGRAM IsOddOrEven: Read A; If (A/2 gives a remainder) Then Print “Its’s Odd”; Else Print ”It’s Even”; EndIf; End.
  • 9. So let’s say we want to express the following algorithm to print out the bigger of two numbers:  Read in two numbers, call them A and B. Is A is bigger than B, print out A, otherwise print out B. PROGRAM PrintBiggerOfTwo: Read A; Read B; If (A>B) Then Print A; Else Print B; EndIf; End.
  • 10. So let’s say we want to express the following algorithm to print out the bigger of three numbers:  Read in three numbers, call them A, B and C.  If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C.  If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.
  • 11. PROGRAM BiggerOfThree: Read A; Read B; Read C; If (A>B) then If (A>C) Then Print A; Else Print B; End If;(B>C) Then Print B; Else Print C; End If; End If; End.
  • 12. So let’s say we want to express the following algorithm:  Print out the numbers from 1 to 5 PROGRAM Print1to5: A=1; WHILE ( A !=6) Do Print A; A= A+1; ENDWHILE; End.
  • 13. So let’s say we want to express the following algorithm:  – Add up the numbers 1 to 5 and print out the result PROGRAM PrintSum1to5: Total =0; A=1; WHILE ( A !=6) Do Total = total +A; A= A+1; ENDWHILE; Print Total; End.
  • 14. So let’s say we want to express the following algorithm:  – Read in a number and check if it’s a prime number.  What’s a prime number?  – A number that’s only divisible by itself and 1, e.g. 7.  Or to put it another way, every number other than itself and 1 gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime.  – So all we need to do is divide 7 by all numbers less than it but greater than one, and if any of them have no remainder, we know it’s not prime.
  • 15.  So,  If the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime.  If the number is 9, we know that 8, 7, 6, 5, and 4, all give remainders, but 3 does not give a remainder, it goes evenly into 9 so we can say 9 is not prime.  So remember, – if the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, prime.  So, in general, – if the number is A, as long as A-1, A-2, A-3, A4, ... 2 give a remainder, A is prime.
  • 16. PROGRAM prime: Read A; B = A-1; IsPrime = True; While(B !=1) DO IF ( A/B gives no remainder) THEN IsPrime = false; End If B=B-1; ENDWHILE; If(IsPrime ==true) Then Print “Prime”; Else Print “Not Prime”; EndIf; End.
  • 17. Write Algorithm & Pseudocode & then Actual Code Write a Program to Reverse an Integer // --- Directions // Given an integer, return an integer that is the reverse // ordering of numbers. // --- Examples // reverseInt(15) === 51 // reverseInt(981) === 189 // reverseInt(500) === 5 // reverseInt(-15) === -51 // reverseInt(-90) === -9
  • 18. Write a Program to Reverse a String // --- Directions // Given a string, return a new string with the reversed // order of characters // --- Examples // reverse('apple') === 'leppa' // reverse('hello') === 'olleh' // reverse('Greetings!') === '!sgniteerG'
  • 19. Write a program to Find Palindromes  // --- Directions  // Given a string, return true if the string is a palindrome  // or false if it is not. Palindromes are strings that  // form the same word if it is reversed. *Do* include spaces  // and punctuation in determining if the string is a palindrome.  // --- Examples:  // palindrome("abba") === true  // palindrome("abcdefg") === false
  • 20. Write a Program that will capitalize a sentence  // --- Directions  // Write a function that accepts a string. The function should  // capitalize the first letter of each word in the string then  // return the capitalized string.  // --- Examples  // capitalize('a short sentence') --> 'A Short Sentence'  // capitalize('a lazy fox') --> 'A Lazy Fox'  // capitalize('look, it is working!') --> 'Look, It Is Working!'
  • 21. Write a function that returns the number of vowels  // --- Directions  // Write a function that returns the number of vowels  // used in a string. Vowels are the characters 'a', 'e'  // 'i', 'o', and 'u'.  // --- Examples  // vowels('Hi There!') --> 3  // vowels('Why do you ask?') --> 4  // vowels('Why?') --> 0