please answer the following question in C++ language 1. Answer each of the follo
ID: 3738806 • Letter: P
Question
please answer the following question in C++ language
1. Answer each of the following questions about loops. In each question LABEL the three part of loop.
a) Write a while statement that will ask the user for integers until the user enters the sentinel value of -1. Declare all needed variables.
b) Write a for statement that will print out all of the multiples of 3 between 1 and 200. Declare any needed variables.
c) Write a do while loop that will ask the user for characters and end when the character is equal to 'e'. Declare any needed variables.
Explanation / Answer
a) Write a while statement that will ask the user for integers until the user enters the sentinel value of -1. Declare all needed variables.
#include<iostream>
using namespace std;
int main() {
int n;
while(true) {
cout<<"Enter the number: "<<endl;
cin >> n;
if(n==-1) break;
}
return 0;
}
b) Write a for statement that will print out all of the multiples of 3 between 1 and 200. Declare any needed variables.
#include<iostream>
using namespace std;
int main() {
for(int i=1;i<=200;i++) {
if(i%3==0){
cout<<i<<" ";
}
}
cout<<endl;
return 0;
}
Output:
c) Write a do while loop that will ask the user for characters and end when the character is equal to 'e'. Declare any needed variables.
#include<iostream>
using namespace std;
int main() {
char c;
do{
cout<<"Enter the character: "<<endl;
cin >> c;
}while(c!='e');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.