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.
c++ language
CODE IS PROVIDED WITH COMMENTS
CODE:
#include <iostream>
using namespace std;
int main() {
int x[10]; //declaring an array
int i;
int count1=0; //declaring and initialzing the variables
int count2=0;
int count3=0;
cout<<"enter the 10 elements\n"<<endl;
for(i=0;i<10;i++)
{
cin>>x[i]; //inputting the array elements
}
int j=0;
while(j<10) //while lopp to check whether multiple or not
{
if(x[j]%2==0) //condition to be a multiple of 2
{
count1=count1+1; //increasing the count for each multiple of 2
}
if(x[j]%3==0) //condition to be a multiple of 3
{
count2=count2+1; //increasing the count for each multiple of 3
}
if((x[j]%2 != 0) & (x[j]%3 != 0)) //condition for a value to be not a multiple of either 2 or 3
{
count3=count3 + 1; //increasing the count for every true value
}
j=j+1; //increasing the j value
}
cout<<"there are "<<count1<<" multiples of 2\n"; //printing the count of the multiples of 2
cout<<"there are "<<count2<<" multiples of 3\n"; //printing the count of multiples of 3
cout<<"there are "<<count3<<" numbers which are not multiples of either 2 or 3";
return 0;
}
INPUT & OUTPUT:
CODE (screen shot):
Get Answers For Free
Most questions answered within 1 hours.