I have to write a code that will print out the sum of all numbers between 1 and 100 that 7 and 5 go into evenly. I can't really think of how to write it. This is what I have but it doesn't work.

import java.io.*;
public class work
 
  {public static input in = new input();
   
public static void main(String[] args) throws IOException
{
int sum;

sum = 0;

for (int x=1;x<=1000;x=+1)
if (x%5==//x%7==0){
sum=sum+1};
System.out.println(sum);

    }
}

Recommended Answers

All 6 Replies

sum of all numbers between 1 and 100 that 7 and 5 go into evenly

Which part of the problem are you having trouble with?
choosing the numbers. Can you manually compute the first few numbers?
summing the selected numbers. Can you add the selected number to the previous sum?
Printing out the sum

A suggestion for testing: only look at the first 20 or 30 numbers until you get the algorithm working. Then change the number of numbers to the required value.

Got the code working now. Line 13 had to be fixed(missing number and parentheses)
Its working perfectly now.

import java.io.*;
public class work
 
  {public static input in = new input();
   
public static void main(String[] args) throws IOException
{
int sum;

sum = 0;

for (int x=1;x<=1000;x=x+1)
if (x%5==0)//(x%7==0)
{sum=sum+x;}
System.out.println(sum);

    }
}

Did you manually verify the output and see if it is correct?

Did you manually verify the output and see if it is correct?

To be honest no I did not manually checked. Why? is it incorrect?

It's up to you to verify your work.

Member Avatar for hfx642

So... What you're saying is...
if X can be divided by 7 evenly
AND (although, you may mean OR)
X can be divided by 5 evenly
then, add X to the sum.
???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.