Monday, May 15, 2017

Sum of the numbers which are multiple of 3.



//Problem:Find the sum of all natural numbers which are less or equal to n and are 
//multiple of 3. 


/* This program calculates the sum of the multiples of 3  from 1 to the specified number. 
 * @author blankbug * 
*/

public class Lesson1 {
    private static int num = 12;
    public static void main(String[]args){
        System.out.println(multiple(num));

    }

    //define a method to calculate the sum of multiples of 3    
public static int multiple(int num){
        int total_sum = 0;
        for(int i=1;i<=num;i++){
            if(i%3 == 0){
                total_sum += i;
            }

        }
        return total_sum;
    }//end of a method multiple
}//end of a class Lesson1


Task 1: Implement this program to take user input and prints the sum of multiple of 3  
of all the number less or equal to the given number.

Task 2: Change this program so that it asks users a number and prints the sum of all 
the numbers less or equal to this number and are multiple of 7.

No comments: