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,...
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.
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...
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
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’...
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.
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run output Please input the number of nodes: 6 Please input the adjacency relation: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run (to make program output more clear, I have put it in boldface): Please input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT