Question

In: Computer Science

Answer as soon as possible Given a number, find the sum of all the unique multiples...

Answer as soon as possible

Given a number, find the sum of all the unique multiples of particular
   numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we
get 3, 5, 6, 9, 10, 12, 15, and 18.
  
   The sum of these multiples is 78.

Given Code:

public int getSumOfMultiples(int i, int[] set) {
       // Solution
       return 0;
   }
  

Solutions

Expert Solution


import java.util.*;
class Main
{
public int getSumOfMultiples(int i, int[] set) {
int sum = 0;
for(int i1 = 1; i1 < i; i1++)
{
for(int j = 0; j < set.length; j++)
{

if(i1 % set[j] == 0)
{
sum = sum + i1; // sum of all multiples
break;
}
}
}
return sum; // returns the sum
}

   public static void main(String[] args)
   {
Scanner s = new Scanner(System.in);
System.out.print("Enter the number : ");
int n = s.nextInt(); // read the number
  
System.out.print("Enter the size of the set : ");
int size = s.nextInt(); // read the set size
  
int set[] = new int[size];
System.out.print("Enter the elements in the set : ");
for(int i = 0; i < size; i++)
{
set[i] = s.nextInt(); // read the size
}
  
Main m = new Main();
int sum = m.getSumOfMultiples(n, set);
System.out.println("Sum of multiples : " + sum); // print the result
  
   }
}

OUTPUT :



Related Solutions

find the difference between the sum of the first 900 multiples of three and the sum...
find the difference between the sum of the first 900 multiples of three and the sum pf the first 900 multiples of four
CAN YOU PLEASE ANSWER ALL QUESTIONS AND PLEASE ANSWER AS SOON AS POSSIBLE 1. The processing,...
CAN YOU PLEASE ANSWER ALL QUESTIONS AND PLEASE ANSWER AS SOON AS POSSIBLE 1. The processing, where different kinds of information are processed in different brain structures, is called: a. Stream segregation b. Serial processing c. Distributed processing d. Parallel processing 2. In vision, what does dark adaptation mean? a. Decrease in color discrimination that occurs after a period in the dark b. The increased sensitivity of the eye that occurs when being in the dark for a long time...
Please answer as soon as possible (Chemical Engineering) Given The reactions C3H8 → C3H6 + H2...
Please answer as soon as possible (Chemical Engineering) Given The reactions C3H8 → C3H6 + H2 C3H8 + 2H2 → 3CH4 The feed contains 90.0 mole% propane(C3H8) and the balance inert. The fractional conversion of propane is 0.603 and the fractional yield of propylene is 0.541. Calculate the molar composition of the product gases and selectivity of ethylene to methane production.
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1/...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1/ What is one similarity and one difference between voluntary motor system that innervate the head versus voluntary motor system that innervate the body? 2/ Does olfactory bulb (direct) relay to primary sensory cortex via the thalamus? 3/ Write a short paragraph using the following terms: opiates; endorphins, pain relief. 4/ In your own words, explain one way in which neuroplasticity allows learning and memory...
Can you please answer all questions and please answer as soon as possible THANK YOU 1/...
Can you please answer all questions and please answer as soon as possible THANK YOU 1/ what do you think wernicke’s area of an infant develops prior to Broca’s? 2/ Create a short paragraph using the following terms: fovea, cones, rods, peripheral retina, acuity, center of the visual field? 3/ Why cone receptors are able to send information about different frequencies of light? 4/why do you think it is easier to name a taste in food than a smell in...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1-...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1- What is one similarity and one difference between voluntary motor system that innervate the head versus voluntary motor system that innervate the body? 2- Does olfactory bulb (direct) relay to primary sensory cortex via the thalamus? 3- Write a short paragraph using the following terms: opiates; endorphins, pain relief. 4- In your own words, explain one way in which neuroplasticity allows learning and memory...
CAN YOU PLEASE ANSWER ALL QUESTION AND PLEASE ANSWER AS SOON AS POSSIBLE THANK YOU 1....
CAN YOU PLEASE ANSWER ALL QUESTION AND PLEASE ANSWER AS SOON AS POSSIBLE THANK YOU 1. Behavioral activity is prone to a lot of variation; would Open field and Inhibitory Avoidance be valuable Primary and only endpoint measures? Explain why? 2. Lesions of the amygdala made after a delay from training result in partial retention of fear motivated Inhibitory avoidance task. On the other hand, pharmacological manipulations of the amygdala immediately after training abolished the strength of inhibitory avoidance learning....
Nursing related question answer them all I'll rate need it as soon as possible You are...
Nursing related question answer them all I'll rate need it as soon as possible You are assisting in the restoration of a mesial-occlusal-distal (MOD) amalgam on tooth #13. How many wedges should be set out for this procedure? Where is a wedge positioned in preparation for proper contour? From what direction do you position the wedge? HTM You are to prepare the universal retainer and matrix band for tooth #29. When assembling the band into the retainer with the diagonal...
Give detail answer of the parts given below as soon as possible. 'Socio-economic conditions (SEC) includes:...
Give detail answer of the parts given below as soon as possible. 'Socio-economic conditions (SEC) includes: 1. meaning of SEC, 2. Definition of SEC, 3. Concept of SEC, 4. Causes of good and poor SEC, 5. Consequences of good and poor SEC.
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer n , a non-negative integer where n ≤ 100 Output: integer x y , the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0 ≤ x , y . Example: Consider the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT