In: Computer Science
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
All the explanations is given in the comments of the code itself.
Code--
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,m2,m3,nm;
int i=0;
m2=0;
m3=0;
nm=0;
//run a loop 10 times
while(i<10)
{
//prompt the user to enter a
number
cout<<"Enter a number:
";
cin>>x;
//check if the number is multiple
of 2
if(x%2==0)
{
//increament m2
by 1
m2=m2+1;
}
//check if the number is multiple
of 3
if(x%3==0)
{
//increament m2
by 1
m3=m3+1;
}
//if not multiples of either 2 or
3
if((x%2!=0)||(x%3!=0))
{
//increament nm
by 1
nm=nm+1;
}
i++;
}
//display the output
cout<<endl<<"Multiples of 2 =
"<<m2<<endl;
cout<<"Multiples of 3 =
"<<m3<<endl;
cout<<"Not Multiples of 2 or 3 =
"<<nm<<endl;
}
Code Screenshot--
Output Screenshot--
Note--
Please upvote if you like the effort.