Menu
Topics Index
...
`

Sum of numbers divisible by 5 or 7


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find the sum of the numbers divisible by 5 or 7 till a given number.

Input (Integer) Output (Integer)
50 5 + 7 + 10 + 14 + 15 + 20 + 21 + 25 + 28 + 30 + 35 + 40 + 42 + 45 + 49 + 50 = 436
NOTE: Although 35 is divisible by 5 and 7, it should added only once
15 5 + 7 + 10 + 14 + 15 = 51



class SumOfNumbersDivisibleByFiveAndSeven
{
    public static void main(String s[])
    {
        System.out.println("Sum of matched numbers till 50 is " + sum_of_matched_numbers(50));

    }


    public static int sum_of_matched_numbers(int input)
    {
        int output = 0;
        return output;
    }

}
Topic: for Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

in this program 1 to given number, each values checked with modulo 5 and 7 using if condition.initially output assigned with 0. use same 1 if condition check both condition using logical OR || operator, here we need 1 for loop to increment value from 1 to givn num, thn inside for loop give if condition i%5==0||i%7==0 thn if case pass add output with number which satisfies if condition thn returns output

Posted by Maheshwari Natarajan    2014-11-18 06:23:33


Put "for" iteration from 1 to input.    In the for check the input is divided by 5 or 7 using "if" condition..    If condition is true add that number to the output..     Finally after closing for loop    return output at the end of method...

Posted by Chandra Sekhar Reddy    2014-11-18 06:26:08


for(int i=0; i<=input; i++){
if(i%5==0 || i%7==0){
output+=i;
}
}

Posted by Amar Kumar Biradar    2014-11-18 06:36:29


Amar Kumar Biradar, no direct code please. Only hints required. :-)

Posted by Merit Campus    2014-11-18 06:42:46


ok :)

Posted by Amar Kumar Biradar    2014-11-18 10:34:49


Take an integer variable and assign it a value 2(since we start checking the number which is divisible by 5 or 7 with 2 till the last number given as parameter to sum_of_matched_numbers method).Now loop through that variable and check if the current value in the variable is divisible by 5 or 7)(Hint-A number is divisible by 5 or 7 when the modulus of the variable with 5 or 7 is zero).Check two conditions using 'if',with each condition separated by logical 'OR' operator(one condition for divisibility with 5 and the other condition for divisibility with 7 separted by 'OR').If any one of  the condition or both conditions are true then add the current value of the variable to 'output'.The final result will be in variable 'output'.

Posted by Sudha Snigdha    2014-11-18 11:49:18


here we are getting the value from the user into input variable...so that here we need to travel
0-to till the given num means(Input)
looping in that range we need to check every num whether this num id divisible by 5 or 7..if it is than we need to add this num to a variable...Tak an int varible and it must initilize that varible to 0

"we will conform the num is divisible something means we need to perfoem modulees(%) on it which will give the remainder....if the given remainder is 0 than we can say that the num is divisible..

Ex: 30%5=0 than we can say that 30 is divisible by 5

here we have condtion that we need to add num if it is divisible by 5 r 7...we have 2 condtions here even though if 1 condtion is true than we need to add.. so that use ||(Logical Or) in if contion..it will return true even thogh 1 condtion is true...

Posted by Uday Kumar    2014-11-19 00:32:24


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Sudha Snigdha,  for 'Second Correct Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-19 07:35:30


© meritcampus 2019

All Rights Reserved.

Open In App