SlideShare a Scribd company logo
1 of 8
Download to read offline
Pseudo Code Practice Problems:
Listed below is a brief explanation of Pseudo code as well as a list of examples and solutions.
Pseudo code
Pseudo code can be broken down into five components.
• Variables:
• Assignment:
• Input/output:
• Selection:
• Repetition:
A variable has a name, a data type, and a value. There is a location in memory associated with each variable. A variable
can be called anything or be given any name. It is considered good practice to use variable names that are relevant to
the task at hand.
Assignment is the physical act of placing a value into a variable. Assignment can be shown using
set = 5;
set = num + set;
The left side is the variable a value is being stored in and the right side is where the variable is being accessed. When a
variable is assigned a value, the old value is written over with the new value so the old value is gone. x = 5 does not
mean that x is equal to 5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5.
Input / Output both deal with an outside source (can be a user or another program) receiving or giving information. An
example would be assuming a fast food restaurant is a program. A driver (user) would submit their order for a burger
and fries (input), they would then drive to the side window and pick up their ordered meal (output.)
• Output – Write / display / print
• Input – Read / get / input
Selection construct allows for a choice between performing an action and skipping it. It is our conditional statements.
Selection statements are written as such:
if ( conditional statement)
statement list
else
statement list
Repetition is a construct that allows instructions to be executed multiple times (IE repeated).
In a repetition problem
– Count is initialized
– Tested
– incremented
Repetition problems are shown as:
while ( condition statement)
statement list
Examples
Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product.
Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.
Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0
and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between
20 and 30, write the word green. If it is any other number, write that it is not a correct color option.
Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100).
Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point.
Example 6: Write pseudo code that will perform the following.
a) Read in 5 separate numbers.
b) Calculate the average of the five numbers.
c) Find the smallest (minimum) and largest (maximum) of the five entered numbers.
d) Write out the results found from steps b and c with a message describing what they are
Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order.
Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the
sum and when a negative number is encountered, stop adding numbers and write out the final result.
Solutions
Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product.
Pseudo code
Read num1 , num2
Set multi to num1*num2
Write multi
Ch code
int num1, num2, multi;
cin>>num1>>num2;
multi = num1 * num2;
cout<<multi<<endl;
Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.
Example 2 Solution 1:
Pseudo Code:
Read isfive
If(isfive = 5)
Write "your number is 5"
Else if (isfive = 6)
Write "your number is 6"
Else
Write "your number is not 5 or 6"
CH code:
int isfive;
cin>> isfive;
if(isfive == 5)
{ cout<<"your number is 5"; }
else if(isfive == 6)
{ cout<<"your number is 6"; }
else
{ cout<<"your number is not 5 or 6"; }
Example 2 Solution 2:
Pseudo Code:
Read isfive
If(isfive = 5 or isfive = 6)
Write "your number is a 5 or 6"
Else
Write "your number is not 5 or 6"
CH code:
int isfive;
cin>> isfive;
if(isfive == 5 || isfive == 6)
{ cout<<"your number is 5 or 6"; }
else
{ cout<<"your number is not 5 or 6"; }
Example 2 Solution 3:
Pseudo Code:
Read isfive
If(isfive is not 5 and isfive is not 6)
Write "your number is not 5 or 6"
CH code:
int isfive;
cin>> isfive;
if(isfive != 5 && isfive != 6)
{ cout<<"your number is not 5 or 6"; }
Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0
and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between
20 and 30, write the word green. If it is any other number, write that it is not a correct color option.
Pseudo Code:
Write "Please enter a number"
Read colornum
If (colornum >0 and colornum <= 10)
Write blue
else If (colornum >0 and colornum <= 10)
Write blue
else If (colornum >0 and colornum <= 10)
Write blue
else
Write "not a correct color option"
CH code:
int colornum;
cout<<"Please enter a number"
cin>> colornum;
if(colornum > 0 && colornum <= 10)
{ cout<<"blue"; }
else if(colornum > 0 && colornum <= 10)
{ cout<<"blue"; }
else if(colornum > 0 && colornum <= 10)
{ cout<<"blue"; }
else
{ cout<<"not a correct color option" }
Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100).
Pseudo Code:
Set x to 1
While(x < 20)
write x
x = x*5
CH code:
int x = 1;
cin>>x;
while(x < 20)
{ cout<<x;
x = x*5;
}
Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point.
For example, say we want to see the first 5 even numbers starting from 0.
well, we know that evens numbers are 0, 2, 4, etc.
The first 5 even numbers are 0, 2, 4, 6, 8.
The first 8 even numbers are 0, 2, 4, 6, 8 ,10 ,12, 16
Example 5 solution 1:
Pseudo Code:
Read count
Set x to 0;
While(x < count)
Set even to even + 2
x = x + 1
write even
CH code:
int x, count, even;
x = 0;
even = 0;
cin>>count;
while(x < count)
{ cout<<even;
even = even+2;
x = x+1;
}
Example 5 solution 2:
Pseudo Code:
Read count
Set x to 0;
While(x < count)
Set even to even + 2
x = x + 1
write even
CH code:
int x, count, even;
cout<<"0";
x = 1;
cin>>count;
while(x < count)
{ cout<<x*2;
x = x+1;
}
Example 6: Write pseudo code that will perform the following.
a) Read in 5 separate numbers.
b) Calculate the average of the five numbers.
c) Find the smallest (minimum) and largest (maximum) of the five entered numbers.
d) Write out the results found from steps b and c with a message describing what they are.
Pseudo Code:
Write "please enter 5 numbers"
Read n1,n2,n3,n4,n5
Write "The average is"
Set avg to (n1+n2+n3+n4+n5)/5
Write avg
If(n1 < n2)
Set max to n2
Else
Set max to n1
If(n3 > max)
Set max to n3
If(n4 > max)
Set max to n4
If(n5 > max)
Set max to n5
Write "The max is"
Write max
If(n1 > n2)
Set min to n2
Else
Set min to n1
If(n3 < min)
Set min to n3
If(n4 < min)
Set min to n4
If(n5 < min)
Set min to n5
Write "The min is"
Write min
CH code:
cout<<"please enter 5 numbers";
int n1,n2,n3,n4,n5;
cin>>n1>>n2>>n3>>n4>>n5;
int avg = (n1+n2+n3+n4+n5)/5;
cout<<"The average is "<<avg;
int min, max;
if(n1<n2)
max=n2;
else
max=n1;
if(n3>max)
max=n3;
if(n4>max)
max=n4;
if(n5>max)
max=n5;
cout<<"The max is "<<max;
if(n1>n2)
min=n2;
else
min=n1;
if(n3<min)
min=n3;
if(n4<min)
min=n4;
if(n5<min)
min=n5;
cout<<"The min is "<<min;
Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order.
Homework 1 solution 1:
Pseudo Code:
Read num1, num2, num3
If (num1 < num2)
If(num2 < num3)
Write num1 , num2, num3
Else
If(num3 < num1)
Write num3, num1, num2
Else
Write num1, num3, num2
else
If(num1 < num3)
Write num2 , num1, num3
Else
If(num3 < num2)
Write num3, num2, num1
Else
Write num2, num3, num1
CH code:
int num1, num2, num3;
cin>> num1>>num2>>num3;
if (num1 < num2)
{ if(num2 < num3)
{ Cout<< num1<<" "<<num2<<" "<<num3; }
else
{ if(num3 < num1)
{ Cout<< num3<<" "<<num2<<" "<<num2; }
else
{ Cout<< num1<<" "<<num3<<" "<<num2; }
}
}
else
{ If(num1 < num3)
{ Cout<< num2<<" "<<num1<<" "<<num3; }
else
{ If(num3 < num2)
{ Cout<< num3<<" "<<num2<<" "<<num1; }
else
{ Cout<< num2<<" "<<num3<<" "<<num1; }
}
}
Homework 1 solution 2:
Pseudo Code:
Read num1, num2, num3
If (num1 < num2 < num3)
Write num1 , num2, num3
else If (num1 < num3 < num2)
Write num1 , num2, num3
else If (num2 < num1 < num3)
Write num2 , num1, num3
else If (num2 < num3 < num1)
Write num2 , num3, num1
else If (num3 < num1 < num2)
Write num3 , num1, num2
else If (num3 < num2< num1)
Write num3 , num2, num1
CH code:
int num1, num2;
cin>> num1>>num2>>num3;
if(num1 < num2 && num2 < num3 && num1 < num3)
{ cout<<num1<<" "<<num2<<" "<<num3; }
else if(num1 < num2 && num2 > num3 && num1 < num3)
{ cout<<num1<<" "<<num3<<" "<<num2; }
else if(num1 > num2 && num2 < num3 && num1 < num3)
{ cout<<num2<<" "<<num1<<" "<<num3; }
else if(num1 > num2 && num2 < num3 && num1 > num3)
{ cout<<num2<<" "<<num3<<" "<<num1; }
else if(num1 < num2 && num2 > num3 && num1 > num3)
{ cout<<num3<<" "<<num1<<" "<<num2; }
else if(num1 > num2 && num2 > num3 && num1 > num3)
{ cout<<num3<<" "<<num2<<" "<<num1; }
Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the
sum and when a negative number is encountered, stop adding numbers and write out the final result.
Pseudo Code:
Read x
Set sum to 0;
While(x >= 0)
Set sum to x + sum
Read x
CH code:
int x, sum;
sum = 0;
cin>>x;
while(x >= 0)
{ sum = sum + x
cin>>x;
}

More Related Content

Similar to Pseudo_Code_Practice_Problems.pdf

1 ECE 175 Computer Programming for Engineering Applica.docx
1  ECE 175 Computer Programming for Engineering Applica.docx1  ECE 175 Computer Programming for Engineering Applica.docx
1 ECE 175 Computer Programming for Engineering Applica.docxoswald1horne84988
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docxPinkiVats1
 
Course project solutions 2019
Course project solutions 2019Course project solutions 2019
Course project solutions 2019Robert Geofroy
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Python programing
Python programingPython programing
Python programinghamzagame
 
Oop lab assignment 01
Oop lab assignment 01Oop lab assignment 01
Oop lab assignment 01Drjilesh
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)ExcellenceAcadmy
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)ExcellenceAcadmy
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb scriptNilanjan Saha
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntaxVivek Bhargav
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)Sena Nama
 
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docxLab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docxsmile790243
 

Similar to Pseudo_Code_Practice_Problems.pdf (20)

Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
1 ECE 175 Computer Programming for Engineering Applica.docx
1  ECE 175 Computer Programming for Engineering Applica.docx1  ECE 175 Computer Programming for Engineering Applica.docx
1 ECE 175 Computer Programming for Engineering Applica.docx
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docx
 
Course project solutions 2019
Course project solutions 2019Course project solutions 2019
Course project solutions 2019
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Python programing
Python programingPython programing
Python programing
 
Statement
StatementStatement
Statement
 
Oop lab assignment 01
Oop lab assignment 01Oop lab assignment 01
Oop lab assignment 01
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb script
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
 
10 template code program
10 template code program10 template code program
10 template code program
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docxLab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
 

Recently uploaded

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Pseudo_Code_Practice_Problems.pdf

  • 1. Pseudo Code Practice Problems: Listed below is a brief explanation of Pseudo code as well as a list of examples and solutions. Pseudo code Pseudo code can be broken down into five components. • Variables: • Assignment: • Input/output: • Selection: • Repetition: A variable has a name, a data type, and a value. There is a location in memory associated with each variable. A variable can be called anything or be given any name. It is considered good practice to use variable names that are relevant to the task at hand. Assignment is the physical act of placing a value into a variable. Assignment can be shown using set = 5; set = num + set; The left side is the variable a value is being stored in and the right side is where the variable is being accessed. When a variable is assigned a value, the old value is written over with the new value so the old value is gone. x = 5 does not mean that x is equal to 5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5. Input / Output both deal with an outside source (can be a user or another program) receiving or giving information. An example would be assuming a fast food restaurant is a program. A driver (user) would submit their order for a burger and fries (input), they would then drive to the side window and pick up their ordered meal (output.) • Output – Write / display / print • Input – Read / get / input Selection construct allows for a choice between performing an action and skipping it. It is our conditional statements. Selection statements are written as such: if ( conditional statement) statement list else statement list Repetition is a construct that allows instructions to be executed multiple times (IE repeated). In a repetition problem – Count is initialized – Tested – incremented Repetition problems are shown as: while ( condition statement) statement list
  • 2. Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100). Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point. Example 6: Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five numbers. c) Find the smallest (minimum) and largest (maximum) of the five entered numbers. d) Write out the results found from steps b and c with a message describing what they are Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order. Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the sum and when a negative number is encountered, stop adding numbers and write out the final result.
  • 3. Solutions Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Pseudo code Read num1 , num2 Set multi to num1*num2 Write multi Ch code int num1, num2, multi; cin>>num1>>num2; multi = num1 * num2; cout<<multi<<endl; Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 2 Solution 1: Pseudo Code: Read isfive If(isfive = 5) Write "your number is 5" Else if (isfive = 6) Write "your number is 6" Else Write "your number is not 5 or 6" CH code: int isfive; cin>> isfive; if(isfive == 5) { cout<<"your number is 5"; } else if(isfive == 6) { cout<<"your number is 6"; } else { cout<<"your number is not 5 or 6"; } Example 2 Solution 2: Pseudo Code: Read isfive If(isfive = 5 or isfive = 6) Write "your number is a 5 or 6" Else Write "your number is not 5 or 6" CH code: int isfive; cin>> isfive; if(isfive == 5 || isfive == 6) { cout<<"your number is 5 or 6"; } else { cout<<"your number is not 5 or 6"; } Example 2 Solution 3: Pseudo Code: Read isfive If(isfive is not 5 and isfive is not 6) Write "your number is not 5 or 6" CH code: int isfive; cin>> isfive; if(isfive != 5 && isfive != 6) { cout<<"your number is not 5 or 6"; }
  • 4. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Pseudo Code: Write "Please enter a number" Read colornum If (colornum >0 and colornum <= 10) Write blue else If (colornum >0 and colornum <= 10) Write blue else If (colornum >0 and colornum <= 10) Write blue else Write "not a correct color option" CH code: int colornum; cout<<"Please enter a number" cin>> colornum; if(colornum > 0 && colornum <= 10) { cout<<"blue"; } else if(colornum > 0 && colornum <= 10) { cout<<"blue"; } else if(colornum > 0 && colornum <= 10) { cout<<"blue"; } else { cout<<"not a correct color option" } Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100). Pseudo Code: Set x to 1 While(x < 20) write x x = x*5 CH code: int x = 1; cin>>x; while(x < 20) { cout<<x; x = x*5; }
  • 5. Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point. For example, say we want to see the first 5 even numbers starting from 0. well, we know that evens numbers are 0, 2, 4, etc. The first 5 even numbers are 0, 2, 4, 6, 8. The first 8 even numbers are 0, 2, 4, 6, 8 ,10 ,12, 16 Example 5 solution 1: Pseudo Code: Read count Set x to 0; While(x < count) Set even to even + 2 x = x + 1 write even CH code: int x, count, even; x = 0; even = 0; cin>>count; while(x < count) { cout<<even; even = even+2; x = x+1; } Example 5 solution 2: Pseudo Code: Read count Set x to 0; While(x < count) Set even to even + 2 x = x + 1 write even CH code: int x, count, even; cout<<"0"; x = 1; cin>>count; while(x < count) { cout<<x*2; x = x+1; }
  • 6. Example 6: Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five numbers. c) Find the smallest (minimum) and largest (maximum) of the five entered numbers. d) Write out the results found from steps b and c with a message describing what they are. Pseudo Code: Write "please enter 5 numbers" Read n1,n2,n3,n4,n5 Write "The average is" Set avg to (n1+n2+n3+n4+n5)/5 Write avg If(n1 < n2) Set max to n2 Else Set max to n1 If(n3 > max) Set max to n3 If(n4 > max) Set max to n4 If(n5 > max) Set max to n5 Write "The max is" Write max If(n1 > n2) Set min to n2 Else Set min to n1 If(n3 < min) Set min to n3 If(n4 < min) Set min to n4 If(n5 < min) Set min to n5 Write "The min is" Write min CH code: cout<<"please enter 5 numbers"; int n1,n2,n3,n4,n5; cin>>n1>>n2>>n3>>n4>>n5; int avg = (n1+n2+n3+n4+n5)/5; cout<<"The average is "<<avg; int min, max; if(n1<n2) max=n2; else max=n1; if(n3>max) max=n3; if(n4>max) max=n4; if(n5>max) max=n5; cout<<"The max is "<<max; if(n1>n2) min=n2; else min=n1; if(n3<min) min=n3; if(n4<min) min=n4; if(n5<min) min=n5; cout<<"The min is "<<min;
  • 7. Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order. Homework 1 solution 1: Pseudo Code: Read num1, num2, num3 If (num1 < num2) If(num2 < num3) Write num1 , num2, num3 Else If(num3 < num1) Write num3, num1, num2 Else Write num1, num3, num2 else If(num1 < num3) Write num2 , num1, num3 Else If(num3 < num2) Write num3, num2, num1 Else Write num2, num3, num1 CH code: int num1, num2, num3; cin>> num1>>num2>>num3; if (num1 < num2) { if(num2 < num3) { Cout<< num1<<" "<<num2<<" "<<num3; } else { if(num3 < num1) { Cout<< num3<<" "<<num2<<" "<<num2; } else { Cout<< num1<<" "<<num3<<" "<<num2; } } } else { If(num1 < num3) { Cout<< num2<<" "<<num1<<" "<<num3; } else { If(num3 < num2) { Cout<< num3<<" "<<num2<<" "<<num1; } else { Cout<< num2<<" "<<num3<<" "<<num1; } } } Homework 1 solution 2: Pseudo Code: Read num1, num2, num3 If (num1 < num2 < num3) Write num1 , num2, num3 else If (num1 < num3 < num2) Write num1 , num2, num3 else If (num2 < num1 < num3) Write num2 , num1, num3 else If (num2 < num3 < num1) Write num2 , num3, num1 else If (num3 < num1 < num2) Write num3 , num1, num2 else If (num3 < num2< num1) Write num3 , num2, num1 CH code: int num1, num2; cin>> num1>>num2>>num3; if(num1 < num2 && num2 < num3 && num1 < num3) { cout<<num1<<" "<<num2<<" "<<num3; } else if(num1 < num2 && num2 > num3 && num1 < num3) { cout<<num1<<" "<<num3<<" "<<num2; } else if(num1 > num2 && num2 < num3 && num1 < num3) { cout<<num2<<" "<<num1<<" "<<num3; } else if(num1 > num2 && num2 < num3 && num1 > num3) { cout<<num2<<" "<<num3<<" "<<num1; } else if(num1 < num2 && num2 > num3 && num1 > num3) { cout<<num3<<" "<<num1<<" "<<num2; } else if(num1 > num2 && num2 > num3 && num1 > num3) { cout<<num3<<" "<<num2<<" "<<num1; }
  • 8. Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the sum and when a negative number is encountered, stop adding numbers and write out the final result. Pseudo Code: Read x Set sum to 0; While(x >= 0) Set sum to x + sum Read x CH code: int x, sum; sum = 0; cin>>x; while(x >= 0) { sum = sum + x cin>>x; }