Question

In: Computer Science

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

Note: If the library <bits/stdc++.h> is not present on your local machine. You can alternatively use <iostream> and <stdio.h> for running the below code snippet(just uncomment them and comment out <bits/stdc++.h>)

C++ Code snippet:

#include<bits/stdc++.h>
//#include<stdio.h>
//#include<iostream>
using namespace std;
int main()
{
  
    int num = 1;                 // initializing the variables
    int count = 0;               // initializing count = 0
    int n1 = 14;
    int n2 = 54;
    int n3 = 123;
    int flag1 = 0;              // initializing the flags to check for conditions
    int flag2 = 0;
    int flag3 = 0;

    while(num <= 10000){        // looping until 10000
      flag1 = 0;                // reseting flags
      flag2 = 0;
      flag3 = 0;
      if(num%n1==0){             // if num is divisible by n1
        count += 1;
        flag1 = 1;               // set flag1 to 1
      }
      if (num%n2==0){
        count += 2;
        flag2 = 1;               // set flag2 to 1
      }
      if (num%n3==0){
        count += 3;
        flag3 = 1;               // set flag3 to 1
      }
      if (!(flag1||flag2||flag3)){ // if all flags are 0, then none of the above conditions
        count += num;              // got executed, so increment count by num
      }
      num += 1;
    }
    cout << count << " ";        // return result

  return 0;
}

Code Output :


Related Solutions

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...
Python Language: Similar to Project 3, write a program that loops a number from 1 to...
Python Language: Similar to Project 3, write a program that loops a number from 1 to 10 thousand and keeps updating a count variable according to these rules: 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. Before the loop begins,...
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Please code C# 6. Write a program that generates a random number between 0 and 10...
Please code C# 6. Write a program that generates a random number between 0 and 10 and asks the user to guess that number. The program then informs the user if they guessed correctly or incorrectly then displays the correct answer.
Write a program in C++ that generates a random number between 1 and 10 and asks...
Write a program in C++ that generates a random number between 1 and 10 and asks the user to guess it. Your program should continue until the user guesses the correct number. With each guess the user makes the program tells the user if the guess is too high or too low. To generate a random number between 1 and 10 you need the following code: /* initialize random seed: */ srand (time(NULL)); /* generate secret number between 1 and...
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...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5? Write a program to perform the summation of 10 even number starting from 2? Write a program to perform the summation of 10 odd number starting from 2? Write a program to perform the summation of 10 number starting from 2 and increment is given by user? Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number and adds even numbers for 1 to the user entered number. 2. Write another piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 3. Write another piece of code that asks user for a file name and then add up all the numbers for the file.
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT