find the sum of the multiples of 3 and 5 below 1000

前端 未结 12 972
我在风中等你
我在风中等你 2021-01-18 08:04

Ok guys, so I\'m doing the Project Euler challenges and I can\'t believe I\'m stuck on the first challenge. I really can\'t see why I\'m getting the wrong answer despite my

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 08:51

    If number is 10 then multiple of 3 is 3,6,9 and multiple of 5 is 5,10 total sum is 33 and program gives same answer:

    package com.parag;
    
    /*
     * @author Parag Satav
     */
    public class MultipleAddition {
    
        /**
         * @param args
         */
        public static void main( final String[] args ) {
        // TODO Auto-generated method stub
    
        ArrayList x = new ArrayList();
        ArrayList y = new ArrayList();
        int totalforthree = 0;
        int totalforfive = 0;
        int number = 8;
    
        int total = 0;
    
        for ( int temp = 1; temp <= number; temp++ ) {
            if ( temp % 3 == 0 ) {
                x.add( temp );
                totalforthree += temp;
            }
    
            else if ( temp % 5 == 0 ) {
                y.add( temp );
                totalforfive += temp;
            }
        }
    
        total = totalforfive + totalforthree;
        System.out.println( "multiples of 3 : " + x );
        System.out.println( "multiples of 5 : " + y );
        System.out.println( "The multiples of 3 or 5 up to " + number + " are: " + total );
    
    }
    
    }
    

提交回复
热议问题