1

I have to find the sum of all the multiples of 3 or 5 below N. example if we have to list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9,the sum of these multiples is 23.

Now the only problem left is that I want it to be able to read ALL the numbers THEN display the sum, for now it reads one number and display the sum right after it, any ideas?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution{
public static void main(String[] args) throws IOException {


    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int Nbr = Integer.parseInt(line);


        for(int j=0; j<Nbr;j++)
        {
            BufferedReader br2 = new BufferedReader(new   InputStreamReader(System.in));
            String line2 = br2.readLine();
            String[] numbers = new String[Nbr];
            numbers[j]= line2;
            System.out.println(Somme(Long.parseLong(numbers[j])));
        }

        }


public static long Somme(long Nn) {
    long s = 0;
    for (int i = 0; i < Nn; i++) {
        if (((i % 3) == 0) || ((i % 5) == 0)) {
            s = s + i;
        }
    }
    return (s);
}
}
Hadh
  • 317
  • 1
  • 7
  • 22

6 Answers6

1

I suggest you use a Scanner. I would also suggest you add to a sum by iterating from 3 to n in increments of 3. Then from 5 to n in increments of 5 (then exclude the multiples of 3, because they've already been added). Something like

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
        int n = scanner.nextInt();
        System.out.println(getSum(n));
    }
}

public static long getSum(int n) {
    long sum = 0;
    for (int i = 3; i < n; i += 3) {
        sum += i;
    }
    for (int i = 5; i < n; i += 5) {
        if (i % 3 != 0) { // <-- already added if i is divisible by 3
            sum += i;
        }
    }
    return sum;
}

Based on your comment below, change main to first read the int of counts, then store them in an array. Something like

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
        int toRead = scanner.nextInt();
        int[] vals = new int[toRead];
        for (int t = 0; t < toRead; t++) {
            if (scanner.hasNextInt()) {
                vals[t] = scanner.nextInt();
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int n : vals) {
            sb.append(getSum(n)).append(" ");
        }
        System.out.println(sb);
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • this works almost same as mine, what I want to do is, the first int that I'm going to read is the number of the numbers that I have to calculate the sum for, for example this input : 3 10 100 150 3 means there will be 3 numbers which are 10, 100 and 150, the output should be : 23 2318 5175. and not display the sum right after the input – Hadh Jul 02 '15 at 03:45
  • @Hadh Edited. Next time please include all relevant detail in your original post for more (and better) help sooner. – Elliott Frisch Jul 02 '15 at 03:51
0

Move your print statement outside the loop.

Before loop,

long[] sumArr = new long[Nbr];

inside loop,

sumArr[j] = Somme(Long.parseLong(numbers[j]));

after loop,

for (long sum : sumArr) {
    System.out.println(sum);
}
Codebender
  • 14,221
  • 7
  • 48
  • 85
0

If u want to read all Ns first then ,read them and put them in an array then try to find the sum

0

You can make ArrayList and add the sum to it then print it after the loop

AmgadMHM
  • 146
  • 8
0

As we know that we need to sum of those numbers which are divisible by 3 or 5 below n where n=10 or 100 or more (it is limit). But if we have n=20 so 15(15%5==0 && 15%3==0) will come twice so that we need to add only once so that we need to check a number is divisible by 15(since 5*3=15).

   long n1=0,n2=0;
    int i=1,j=1;
    while(3*i<n)
        {
        n1 +=3*i;
        i++;
        if(5*j<n)
            {
            n2+=5*j;
            if(5*j%15==0)
                n2 -=5*j;
            j++;
        }
      }

    System.out.println(n1+n2);
    }
Shree Prakash
  • 2,052
  • 2
  • 22
  • 33
0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum = 0;
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            long n = in.nextInt();
            for(int j=1;j < n;j++)
            if(j % 3 == 0 || j%5 == 0){
                sum+=j;
            }
            System.out.println(sum);
            sum = 0;
        }
        
        
        
    }
}

This is simple Solution

Kiran Jadhav
  • 29
  • 1
  • 1
  • 6