SHARE
TWEET

Untitled

a guest Mar 20th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This class calculates the sum of all multiples of 3
  3.  * or 5 less than 1000
  4.  * @author Tewodros
  5.  * @version 1
  6.  */
  7. public class SumOfMultiplesOfThreeOrFive {
  8.     public static void main (String[] args){
  9.         int multiplesOfThree = 0;
  10.         int multiplesOfFive = 0;
  11.         int multiplesOfFifteen = 0;
  12.         int sum  = 0;
  13.         int i = 1;
  14.     //adding up all multiples of three first
  15.         while (multiplesOfThree + 3 <1000) {
  16.             multiplesOfThree = 3 * i;
  17.             sum += multiplesOfThree;
  18.             i++;
  19.         }
  20.         int j = 1;
  21.     //then add all multiples of 5
  22.         while (multiplesOfFive + 5 <1000) {
  23.             multiplesOfFive = 5 * j;
  24.             sum += multiplesOfFive;
  25.             j++;
  26.         }
  27.         //remove the duplicate multiples
  28.         int k = 1;
  29.         while (multiplesOfFifteen + 15 <1000) {
  30.             multiplesOfFifteen = 15 * k;
  31.             sum -= multiplesOfFifteen;
  32.             k++;
  33.         }
  34.         System.out.println("Sum = " + sum);
  35.     }
  36. }
RAW Paste Data
Top