Include a pseudocode or flowchart of your process. Use a loop structure to write a C++ program that
A) Finds and displays how many multiples of 8 there are in between 1 to 1000 .
B) Displays all the multiples of 8 between 1 ~ 1000 , separated with commas.
C) Finds and displays the sum of all the multiples of 8 in between 1 to 1000.
Answer:
C++ code as per your requirement
1)
//header
#include <iostream>
//namespace standard
using namespace std;
//main
int main() {
//intializing the count to 0
int multiplesOfEightCount=0;
//for loop from 1 to1 1000
for(int i=1;i<=1000;i++){
//checking if i is multiple of 8
if(i%8==0){
//if yes increment the count variable
multiplesOfEightCount++;
}
}
//printing to the user
cout<<"There are "<<multiplesOfEightCount<<" of 8 in betweeen 1 to 1000"<<endl;
}
Editor:
output:
B)
Raw code:
//header
#include <iostream>
//namespace standard
using namespace std;
//main
int main() {
//for loop from 1 to1 1000
for(int i=1;i<=1000;i++){
//checking if i is multiple of 8
if(i%8==0){
cout<<i<<",";
}
}
}
editor:
output:
c)
Raw code:
//header
#include <iostream>
//namespace standard
using namespace std;
//main
int main() {
// initialzing sum as 0
int sum=0;
//for loop from 1 to1 1000
for(int i=1;i<=1000;i++){
//checking if i is multiple of 8
if(i%8==0){
sum=sum+i;
}
}
cout<<"The sum of all the multiples of 8 in between 1 to 1000: "<<sum<<endl;
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.
Get Answers For Free
Most questions answered within 1 hours.