Problem F1 Write a do while loop to require the user to enter two integers; the
ID: 3671013 • Letter: P
Question
Problem F1
Write a do while loop to require the user to enter two integers; the second integer must be equal to, or larger than, the first integer. Both integers must be at least 1 and not larger than 20. If they do not enter correct integers, give them error messages and make them do it again until they are correct.
After the acceptable integers have been entered, use a for loop to print a table of integers and their square roots for all integers from the first integer specified by the user to the last integer specified by the user, inclusive. Align the table and print 4 decimal positions
A sample table follows, for integers 7 to 9:
Test the program twice:
First test, first enter: first integer = 2, second integer = 0
and after that is rejected: first integer = 2, second integer = 4
Second test, first enter: first integer = 21, second integer = 5
and after that is rejected: first integer = 5, second integer = 5
Problem F2 = Chapter 5 Programming Challenge 6
Make the speed and the time both integers.
Test data, run the program twice:
First test, speed -2 and time -1; when that is rejected, speed -1 and time 0; when that is rejected, speed 0 and time 1.
Second test speed 20, time 5
Problem F3
Provied a loop with a menu. Repeat until the user selects quit. Prompt the user to select one of the menu items. Test for numbers that are not in the menu, provide an error message, and then continue. The menu shall contain:
After the user selects a pattern, allow them to specify the size. If the size is smaller than 1 or larger than 9, give them an error message and repeat the specification of the size.
Once the user has correctly selected a pattern and a size, print the pattern using the size specified.
Pattern 1 is a square. Example for size 4:
Pattern 2 is a triangle. Example for size 5:
Pattern 3 is a square with a diaginal of numbers in stars. Example for size 6:
Pattern 4 is a square with a reversed diaginal of numbers in stars. Example for size 7:
Test the program once using each of these values in order:
Pattern 9; when that is rejected try:
Pattern 1, size 11; when that is rejected use:
Pattern 1, size 3
Pattern 2, size 4
Pattern 3, size 5
Pattern 4, size 6
Quit
(CODEBLOCKS C++)
Explanation / Answer
#include <iostream>
using namespace std;
int main ()
{
int x,y;
boolean c = true;
do
{
cout<<"Enter Number1 "<<endl;
cin>>x;
cout<<"Enter Number2 "<<endl;
cin>>y;
if((x<0)&&(y<0))
{
cout<<"Please enter positive numbers "<<endl;
c=false;
}
elseIF((y>=x)&&(x>1)&&(X<20)&&(y>1)&&(y<20))
c=true;
else
{
cout<<"Enter Numbers again ";
c=false;
} while(c)
/*For print a table of integers and their square roots for all integers from the first integer to last integer*/
cout<<"Integer SquareRoot "<<endl;
for(i=x;i<=y;i++)
{
cout<<x<< " "<<Math.sqrt(x)); //sqrt(x) returns double value
return o;
}
}//main
F2:
#include <iostream>
using namespace std;
int main ()
{
int speed,time,t,s;
boolean c = true;
do
{
cout<<"Enter speed "<<endl;
cin>>speed ;
cout<<"Enter Time "<<endl;
Cin>>time ;
if((speed<0)&&(time<0))
c=false;
else
cout<<"Enter Time"<<endl;
cin>>t;
cout<<"Enter speed "<<endl;
Cin>>s ;
c=true;
} while(c)
return o;
}//main
Problem F3:
#include <iostream>
using namespace std;
int main() {
int I,j,o,n;
cout<<"Enter number";
cin>>n;
cout<<" 1. Square pattern"<<endl;
cout<<" 2. Triangle pattern "<<endl;
cout<<" 3. Diaginal pattern"<<endl;
cout<<" Reverse diaginal pattern"<<endl;
cout<<" 5.Quit"<<endl
cout<<"Enter option for print the appropriate pattern";
cin>>o;
switch(o) {
case 1:
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++)
{
cout<<j;
}
cout<<endl;
}
break;
case 2:
for(int i=0;i<=n;i++){
for(int j=0;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
break;
case 3:
for(int i=0;i<n;i++)
{
for (int j=0;j<=i;j++)
{
if(i==j)
{
cout<<n;
}
else
Cout<<" * ";
}
}
break;
case 4:
for(int i=0;i<n;i++)
{
for (int j=n;j>=i;j--)
{
if(i==j)
{
cout<<n;
}
else
Cout<<" * ";
}
}
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Invaild option");
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.