In: Computer Science
1. Write a for loop that will display all of the multiples of 3, one per line.
The starting point is a variable called lower, and the ending point is an integer called upper, inclusive.
The "lower" and "upper" variables have been properly declared and assigned values where the value of lower is less than or equal to the value of upper. Declare any other variables that are needed.
2. Write a "while" loop that does the same thing as the following "for" loop.
for (i=6;i<1000;i+=5)
cout<<i<<endl;
3. Write an if statement to print the message "The size is valid" if the variable named size is in the range 5 to 18, including 5 and 18. Otherwise print "The size is not valid".
ANSWERS;
1.)
CODE:
#include<iostream>
using namespace std;
int main()
{
int lower = 3;//declaring lower variable
int upper = 20;//declaring upper varaiable
int i;//loop variable
cout<<"Multiples of 3 : "<<endl;
//for loop to print Multiples of 3.
for(i=lower;i<=upper;i++)
{
//codition to check whether given number is divisible by 3.
if(i%3==0)
{
cout<<i<<endl;//printing number line by line.
}
}
}
CODE
ATTACHMENTS:
2.)
CODE:
#include<iostream>
using namespace std;
int main()
{
int i;//loop variable
i=6;//initialising value
//while loop that resembles for loop given.
while(i<1000)
{
cout<<i<<endl;//printing as needed.
i+=5;//incrementing variable.
}
}
CODE ATTACHMENTS:
3.)
CODE:
#include<iostream>
using namespace std;
int main()
{
int size;//declaring size variable
cout<<"Enter size : ";
cin>>size;//taking size from user
//if condition to check the size in range or not.
if(size>=5 && size<=18)
{
cout<<"The size is valid";
}
else
{
cout<<"The size is not valid";
}
}
CODE ATTACHMENTS;
Thank You.
Please comment for any queries.
Please rate it.