In: Computer Science
Cpp challenge
Description
The purpose of this challenge is to use the WHILE loop to control program flow. This challenge will use the WHILE loop in various ways.
Requirements
Write all your code for the following steps in one file.
Do Not Use
You must use the WHILE statement. You may not use any other looping mechanism.
Sample Interaction / Output
Enter a string to be repeated: coffee How many times to show? 5 coffee coffee coffee coffee coffee 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 100 90 80 70 60 50 40 30 20 10 Enter a number: 3 3 x 2 = 6 Enter a number: 7 7 x 2 = 14 Enter a number: -2 Enter a string: yo Enter a string: hi Enter a string: hola Enter a string: sup Enter a string: bye
#include<iostream>
using namespace std;
int main(){
string txt;
int n,i;
cout<<"Enter a string to be printed: ";
cin>>txt;
cout<<"How many times to show? ";
cin>>n;
i=0;
while(i<n){
cout<<txt<<endl;
i++;
}
i=1;
//print 1 to 20
while(i<=20){
cout<<i<<" ";
i++;
}
cout<<endl;
i=100;
//printing 0-100 which divisable by 10
while(i>=0){
if(i%10==0)
cout<<i<<" ";
i--;
}
cout<<endl;
//reading nubmers and print *2
while(true){
cout<<"Enter a number: ";
cin>>n;
if(n<0)
break;
cout<<n<<" * "<<"2 = "<<n*2<<endl;
}
//reading string until uesr gives bye
while(true){
cout<<"Enter a string: ";
cin>>txt;
if(txt=="bye")
break;
}
}
Note : If you like my answer please rate and help me it is very Imp for me