Question

In: Computer Science

CODE MUST BE IN C++ (please use for loop) write a program that loops a number...

CODE MUST BE IN C++ (please use for loop)
write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules:

n1 = 14
n2 = 54
n3 = 123

if the number is divisible by n1, increase count by 1
if the number is divisible by n2, increase count by 2
if the number is divisible by n3, increase count by 3
if none of the above conditions match for the number, increase count by the number.

The program must produce one numeric output (it should be the value of count)

Solutions

Expert Solution

LOOK AT THE CODE AND COMMENTING FOR BETTER UNDERSTANDING.

SCREENSHOT OF THE CODE IS:

The output of above program is:

code for copy:

#include<iostream>
using namespace std;
int main()
{
    //declare all the variables
    int n,count=0,n1=14,n2=54,n3=123,i;
    //for loop ranges from 1 to 10000
    for( i=1;i<=10000;i++)
    {
        // modulo operator used to check whether the number(i) is divisible by n1 or not.if  divisible increment count by 1
        if (i%n1==0)
        {
            count+=1;
        }
        // modulo operator used to check whether the number(i) is divisible by n2 or not.if divisible increment count by 2
        if(i%n2==0)
        {
            count+=2;
        }
        // modulo operator used to check whether the number(i) is divisible by n3 or not.if divisible increment count by 3
        if(i%n3==0)
        {
            count+=3;
        }
        // if all the above cases fail then increment the count by number(i)
        if(i%n1!=0 && i%n2!=0 && i%n3!=0)
        {
            count+=i;
        }
    }
    //print the count to output screen
    cout<<"The value of count is : "<< count <<endl;

}

I HOPE THIS WILL HELP YOU... IF YOU HAVE ANY DOUBTS REGARDING IT PLEASE LET ME KNOW IN COMMENT SECTION


Related Solutions

CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program as follows: Ask the user for an integer representing a number of integers to be sorted. Create an array of integers of the size provided by user. Initialize the array to zeros. Ask the user for and populate the array with user input. Output the array elements on one line separated by a space. Write a function name “supersort” that takes an integer pointer...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT