Help The program has to be in c cpp and must compile as such
Help!!! The program has to be in c++ (.cpp) and must compile as such. I use linux.
Different loops to solve the same problem
Each of the following three tasks can be solved using a loop:
Ask user how many times they want the computer to print \"HI\". Print \"HI\" that many times.
Print a countdown from 10 to 1, then print \"Liftoff!\"
Ask the user for name. Don\'t stop asking until they type \"Barney\"
Question to ponder for #1: What if the user answers 0? Do all three loops behave the same way?
Solve each task three times using the three different types of loops in the language: for-loop, while-loop, do-while loop. Combine all of the code into a single main-function. You should have a total of nine loops in your program when you are done (three for each of the three tasks).
Solution
#include <iostream>
using namespace std;
int main()
{
int n;
cout << \"How many times do you want to print \'HI\': \";
cin >> n;
int i;
for(i=0; i<n; i++){
cout<<\"HI\"<<endl;
}
i =0;
while(i < n){
cout<<\"HI\"<<endl;
i++;
}
i =0;
do{
cout<<\"HI\"<<endl;
i++;
}while(i < n);
int j;
for(j=10;j>0; j--){
cout<<j<<endl;
}
cout<<\"Liftoff!\"<<endl;
j=10;
while(j>0){
cout<<j<<endl;
j--;
}
cout<<\"Liftoff!\"<<endl;
j=10;
do{
cout<<j<<endl;
j--;
}while(j>0);
cout<<\"Liftoff!\"<<endl;
string name;
name=\"\";
for (;name !=\"Barney\";){
cout<<\"Enter the name: \";
cin >> name;
}
name=\"\";
while(name !=\"Barney\"){
cout<<\"Enter the name: \";
cin >> name;
}
name=\"\";
do{
cout<<\"Enter the name: \";
cin >> name;
}while(name !=\"Barney\");
return 0;
}
Output:
HI
HI
HI
HI
HI
HI
10
9
8
7
6
5
4
3
2
1
Liftoff!
10
9
8
7
6
5
4
3
2
1
Liftoff!
10
9
8
7
6
5
4
3
2
1
Liftoff!
Enter the name: suresh
Enter the name: Barney
Enter the name: suresh
Enter the name: Barney
Enter the name: suresh
Enter the name: Barney


