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 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...
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...
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.
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...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop index Can use a non-unit stride Are required for any kind of animations An accumulator: MUST start at zero Can be used to keep track of a quantity each time through the loop Can be used to calculate how a sum changes with each loop Should be coded, for loops are more efficient For loops: Perform a set task a set number of times...
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT