Question

Using a while loop, write a program that reads 10 integer numbers. The program shall count...

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

Homework Answers

Answer #1

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):


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
Write a program in C that does the following: Reads 10 real numbers from the keyboard...
Write a program in C that does the following: Reads 10 real numbers from the keyboard using a loop and displays the sum of the numbers and the sum of the square of the numbers.
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display 5 lines of smiley faces. The following would be the output of your program: Note: Sample program is in the "Important Documents", Module 6 folder) Hello:                          Bye!
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
All in C language... Write a complete program that declares an integer variable, reads a value...
All in C language... Write a complete program that declares an integer variable, reads a value from the keyboard into that variable, and writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces. Besides the numbers, nothing else should be written to standard output except for spaces separating the values. I got this... and it's wrong #include<stdio.h> int main() { int num; scanf("%d", &num); printf("%d %d %d",num,2*num,num*num); return 0; }
Write a program that reads three integer values from the keyboard using the Scanner class representing,...
Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
Lab Assignment | Count vowels Write a program that will use a while loop to count...
Lab Assignment | Count vowels Write a program that will use a while loop to count and display the number of vowels in the string provided as input by the end-user. This program will consider the letters a, e, i, o, and u (both upper and lower case) to be vowels. The user may enter a single word, a sentence, a paragraph, or nothing at all as input. Be sure to test each possibility. Python 3 please!
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
Write a program to input a sequence of 8 integers, count all the odd numbers and...
Write a program to input a sequence of 8 integers, count all the odd numbers and sum all the negative integers. One integer is input and processed each time around the loop. Python Programming