473,548 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Project Euler

6 New Member
Hi.

Im new to Java and attempted to increase my knowledge by working on Project Euler..I figured that If Im able to solve as many problems from the project as possible with Java I will get more familiar with its syntax and the possibilities that lie therein...
Im in problem no 1

Problem no 1:

Find the sum of numbers between 0 and 1000 that are divisible by 3 or 5 and display it


and here's my code.
Expand|Select|Wrap|Line Numbers
  1. mport java.lang.String;
  2. import java.io.*;
  3.  
  4.  
  5. public class ProjectEuler {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.     int sum, counter, temp;
  10.         for( counter = 0; counter < 1000; counter ++)
  11.  
  12.           while (((counter % 3) == 0) | ((counter % 5) == 0))
  13.               {
  14.                 sum = 0;
  15.                 temp = 0;
  16.                 temp = counter;
  17.                 sum = sum + temp;
  18.               }
  19.  
  20.     System.out.println( "The sum of the numbers is: " +  sum);
  21.  
  22.  
The error that I keep getting is:

variable sum might not have been initialized
Feb 2 '10 #1
8 3848
RedSon
5,000 Recognized Expert Expert
You must put code tags around your code. Please do not forget to do it again.
Feb 2 '10 #2
RedSon
5,000 Recognized Expert Expert
You need to initialize sum outside of the while loop. What happens if counter mod 3 or 5 is never equal to zero?
Feb 2 '10 #3
Mathismylove
6 New Member
sorry but i don't understand what you mean by putting tags. About your other question. If counter mod 3 or 5 is not equal to zero then counter is not assigned to temp and hence is not included in the sum of the variables. Unless I'm missing something. Thanks.
Feb 3 '10 #4
RedSon
5,000 Recognized Expert Expert
Read the forum help pages if you don't understand about code tags. As for the other bit, the compiler is telling you that the sum variable needs to be set before entering the loop. You can still reset it in the loop, but the compiler can't figure out exactly what you are doing. All it sees is that you are using a variable that has not been initialized in a loop which may or may not be iterated through.
Feb 4 '10 #5
Mathismylove
6 New Member
i adjusted my code to have the sum initialized outside the for loop(before it actually!), but still I can't see any output on the output window. Its building and compiling without errors but its not giving me any output...
Maybe I should create an applet to display the output but I dont even know how to do that. What am I missing?
Feb 5 '10 #6
RedSon
5,000 Recognized Expert Expert
You need to reexamine your loops.

What happens to your loops when counter is zero?
Feb 5 '10 #7
Time
77 New Member
Once your counter variable satisfies the while loop condition; no other condition provided for the loop to break; come out of itself.
Feb 6 '10 #8
Mathismylove
6 New Member
interesting insights guys..thanks redson and time...im on it!
Feb 8 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1542
by: public heath vb developer | last post by:
We have a solution with 29 projects including a main menu and 28 dlls. One of the projects (Project B) was created by copying an existing project (Project A), making changes including the assembly name, root namespace, form name, and internal code. When the new project (Project B) was added to the solution and a reference to it was attempted to...
5
11022
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been created in the wrong folder on the hard drive. Before I discovered these files, .NET kept trying to create a new project with _1 following the project...
0
1485
by: Patty O'Dors | last post by:
Hi I am experiencing the following issue when building an ATL component that is tested with c#: The ATL project builds first, then the C# project, as the ATL DLL is a dependency of the C# project. The C# project has got a control from the ATL DLL on one of its forms, and as such, whenever VS is loaded with the C# project, it has got the DLL...
2
4769
by: Rudy Ray Moore | last post by:
How can I modify the project build order of a multi-project workspace under "Visual Studio .net 2003 7.1 c++"? I tried to modify the .sln by hand to influence the build order, but it didn't seem to help. It looks like it's in reverse alphabetical order with upper case characters winning. Contents from my "Output" window: Build started:...
4
2881
by: Brad | last post by:
I'm not one to rant or flame....so please excuse me while I do so for this once. I've now spent a bit of time working with VS2005 beta 2 to see how it functions for web development, especially how our current extensive number of .Net 1.1 web apps convert to it. After a week's time I dont's mind the converted app code that was broken and had...
29
2789
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. How do I move this project/solution from a local development xp pro computer which uses IIS/localhost, to another xp pro computer that is on a Windows 2003...
10
9077
by: Protoman | last post by:
Hi! I'm trying to compute Euler's totient function for an extremely simple RSA program I'm writing. How, exactly, is it calculated? Thanks!
7
6850
by: dkultasev | last post by:
Hello, could anyone help with euler circuit ? I tried to find it, found some, but they are not working. Tried to write my own one, but it supposed to be slow with big graphs because it trying all possibles situations. Prefer to get something on c++ or java, but other languages are welcome also. Tnanks
1
3306
by: luna18 | last post by:
i like to do programming but i am not a computer student.. =) i m trying to write a program to determine a euler circuit.but end up all stuck... i tyr to take the input as graph and if it is a euler circuit it will output an euler circuit. can any1 give me some starting point or hints ? thanks for viewing...
25
3764
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not let me down, but it has proved surprisingly slow on one puzzle. The puzzle is: p is the perimeter of a right angle triangle with integral length...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.