Write a program that uses a while loop to print the integers 1 to 100, one numbe
ID: 3904659 • Letter: W
Question
Write a program that uses a
while
loop to print the integers 1 to 100,
one number on each line, and set the number’s field width as 3. When a number is
divisible by 3, print “Blue” on the same line, and separate them by a space. Follow the
same format, when a number is divisible by 5, print “Berry”; and when a number is
divisible by 15, print “Blueberry”. Note that a number divisible by 15 will also be
divisible by 3 and 5. In this case, only “Blueberry” should be printed. A message, “The
following is from a
while
loop”, should be printed on the first line. Three messages, one
on each line, should be printed to report the total numbers of “Blue”, “Berry”, and
“Blueberry” respectively. Finally, in the same program, add a
for
loop and a
do...while
loop: each accomplishes the same task as the
while
loop. The output of your program
should look exactly like the following:
The following is from a while loop:
1
2
3 Blue
4
5 Berry
6 Blue
7
8
9 Blue
10 Berry
11
12 Blue
13
14
15 Blueberry
16
17
18 Blue
19
20 Berry
21 Blue
22
23
24 Blue
25 Berry
26
27 Blue
28
29
30 Blueberry
31
32
33 Blue
34
35 Berry
36 Blue
37
38
39 Blue
40 Berry
41
42 Blue
43
44
45 Blueberry
46
47
48 Blue
49
50 Berry
51 Blue
52
53
54 Blue
55 Berry
56
57 Blue
58
59
60 Blueberry
61
62
63 Blue
64
65 Berry
66 Blue
67
68
69 Blue
70 Berry
71
72 Blue
73
74
75 Blueberry
76
77
78 Blue
79
80 Berry
81 Blue
82
83
84 Blue
85 Berry
86
87 Blue
88
89
90 Blueberry
91
92
93 Blue
94
95 Berry
96 Blue
97
98
99 Blue
100 Berry
There are 27 Blues.
There are 14 Berries.
There are 6 Blueberries.
The following is from a for loop:
1
2
3 Blue
4
5 Berry
6 Blue
7
8
9 Blue
10 Berry
11
12 Blue
13
14
15 Blueberry
16
17
18 Blue
19
20 Berry
21 Blue
22
23
24 Blue
25 Berry
26
27 Blue
28
29
30 Blueberry
31
32
33 Blue
34
35 Berry
36 Blue
37
38
39 Blue
40 Berry
41
42 Blue
43
44
45 Blueberry
46
47
48 Blue
49
50 Berry
51 Blue
52
53
54 Blue
55 Berry
56
57 Blue
58
59
60 Blueberry
61
62
63 Blue
64
65 Berry
66 Blue
67
68
69 Blue
70 Berry
71
72 Blue
73
74
75 Blueberry
76
77
78 Blue
79
80 Berry
81 Blue
82
83
84 Blue
85 Berry
86
87 Blue
88
89
90 Blueberry
91
92
93 Blue
94
95 Berry
96 Blue
97
98
99 Blue
100 Berry
There are 27 Blues.
There are 14 Berries.
There are 6 Blueberries.
The following is from a do...while loop:
1
2
3 Blue
4
5 Berry
6 Blue
7
8
9 Blue
10 Berry
11
12 Blue
13
14
15 Blueberry
16
17
18 Blue
19
20 Berry
21 Blue
22
23
24 Blue
25 Berry
26
27 Blue
28
29
30 Blueberry
31
32
33 Blue
34
35 Berry
36 Blue
37
38
39 Blue
40 Berry
41
42 Blue
43
44
45 Blueberry
46
47
48 Blue
49
50 Berry
51 Blue
52
53
54 Blue
55 Berry
56
57 Blue
58
59
60 Blueberry
61
62
63 Blue
64
65 Berry
66 Blue
67
68
69 Blue
70 Berry
71
72 Blue
73
74
75 Blueberry
76
77
78 Blue
79
80 Berry
81 Blue
82
83
84 Blue
85 Berry
86
87 Blue
88
89
90 Blueberry
91
92
93 Blue
94
95 Berry
96 Blue
97
98
99 Blue
100 Berry
There are 27 Blues.
There are 14 Berries.
There are 6 Blueberries.
Explanation / Answer
Below is the C++ code I hope that i have provided sufficient comments for your better understanding
while loop -
#include<bits/stdc++.h>
using namespace std;
int main()
{
int blue=0,berry=0,blueberry=0;
cout<<"The following is from a while loop:"<<endl;
int i=1;
while(i<=100)
{
//print the number
cout<<i<<" ";
//check for the given condition
//print the statement accordingly
//increment the count accordingly
if(i%15==0)
{
cout<<"Blueberry";
blueberry++;
}
else if(i%5==0)
{
cout<<"Berry";
berry++;
}
else if(i%3==0)
{
cout<<"Blue";
blue++;
}
//change the line
cout<<endl;
//increment i
i++;
}
cout<<"There are "<<blue<<" Blues."<<endl;
cout<<"There are "<<berry<<" Berries."<<endl;
cout<<"There are "<<blueberry<<" Blueberries."<<endl;
return 0;
}
for loop -
#include<bits/stdc++.h>
using namespace std;
int main()
{
int blue=0,berry=0,blueberry=0;
cout<<"The following is from a for loop:"<<endl;
for(int i=1;i<=100;i++)
{
//print the number
cout<<i<<" ";
//check for the given condition
//print the statement accordingly
//increment the count accordingly
if(i%15==0)
{
cout<<"Blueberry";
blueberry++;
}
else if(i%5==0)
{
cout<<"Berry";
berry++;
}
else if(i%3==0)
{
cout<<"Blue";
blue++;
}
//change the line
cout<<endl;
}
cout<<"There are "<<blue<<" Blues."<<endl;
cout<<"There are "<<berry<<" Berries."<<endl;
cout<<"There are "<<blueberry<<" Blueberries."<<endl;
return 0;
}
do while loop -
#include<bits/stdc++.h>
using namespace std;
int main()
{
int blue=0,berry=0,blueberry=0;
cout<<"The following is from a do...while loop:"<<endl;
int i=1;
do
{
//print the number
cout<<i<<" ";
//check for the given condition
//print the statement accordingly
//increment the count accordingly
if(i%15==0)
{
cout<<"Blueberry";
blueberry++;
}
else if(i%5==0)
{
cout<<"Berry";
berry++;
}
else if(i%3==0)
{
cout<<"Blue";
blue++;
}
//change the line
cout<<endl;
//increment i
i++;
}while(i<=100);
cout<<"There are "<<blue<<" Blues."<<endl;
cout<<"There are "<<berry<<" Berries."<<endl;
cout<<"There are "<<blueberry<<" Blueberries."<<endl;
return 0;
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.