473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Java program that adds the numbers between 1 and 1000

how do i wright a java program that adds the numbers between 1 and 1000 but skips 300 and 500 any help on this matter i would greatly appreciate

thank you
Mar 4 '08 #1
28 4882
BigDaddyLH
1,216 Expert 1GB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

MODERATOR
Mar 4 '08 #2
BigDaddyLH
1,216 Expert 1GB
Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

MODERATOR
Mar 4 '08 #3
JosAH
11,448 Expert 8TB
how do i wright a java program that adds the numbers between 1 and 1000 but skips 300 and 500 any help on this matter i would greatly appreciate

thank you
Do you know how to add those numbers *without* skipping those two numbers?
Please show us some code because we're not doing it for you.

kind regards,

Jos
Mar 4 '08 #4
// adds the numbers between 1 and 1000 excluding 300 and 500
//

int=sum;
sum=1;




this is as far as i can get i know i need a if and a while statement I know i need the while statement within my loop { }
Mar 4 '08 #5
nomad
664 Expert 512MB
// adds the numbers between 1 and 1000 excluding 300 and 500
//

int=sum;
sum=1;




this is as far as i can get i know i need a if and a while statement
have you read up on counters (++), arithmetic operators and expressions?
and if or else statements

nomad
Mar 4 '08 #6
BigDaddyLH
1,216 Expert 1GB
// adds the numbers between 1 and 1000 excluding 300 and 500
//

int=sum;
sum=1;




this is as far as i can get i know i need a if and a while statement
If that's as far as you can get, that's nowhere. You need to read your textbook or take some basic tutorials:

http://java.sun.com/docs/books/tutorial/java/index.html

A forum isn't the right place to get help you you can't even crawl.
Mar 4 '08 #7
I'm Just Learning Java I Know A Few Things Like You Need ; After Certain Things Also For This Program Ill Be Using A If, And A While Statement Within My Loop , I Just Dont Know How To Get It Going
Mar 4 '08 #8
int sum = 0;
if (int i = 1; i <= 1000; ++i) {
sum += i;
while( )

}




is this better
Mar 4 '08 #9
BigDaddyLH
1,216 Expert 1GB
I'm Just Learning Java I Know A Few Things Like You Need ; After Certain Things Also For This Program Ill Be Using A If, And A While Statement Within My Loop , I Just Dont Know How To Get It Going
Then you should go carefully through the tutorial. There are examples of all that.
Mar 4 '08 #10
JosAH
11,448 Expert 8TB
int sum = 0;
if (int i = 1; i <= 1000; ++i) {
sum += i;
while( )

}

is this better
Not really no; I think you're just guessing what your program should be without
knowing exactly what to do. Who gave you the idea to put that (incorrect) if-
statement there? An if statement takes a boolean value/expression and if it's
true the body of the if-statement will be executed. You actually put in an entire
for-clause in there. Try again and look up what for-loops can do. btw, I have no
idea what that while( ) thing does in there ;-)

kind regards,

Jos
Mar 4 '08 #11
If that's as far as you can get, that's nowhere. You need to read your textbook or take some basic tutorials:

http://java.sun.com/docs/books/tutorial/java/index.html

A forum isn't the right place to get help you you can't even crawl.
atleast I dont stutter when I SPEAK LOL
Mar 4 '08 #12
BigDaddyLH
1,216 Expert 1GB
atleast I dont stutter when I SPEAK LOL
So true. I dictate these replies and my secretary types them in.
Mar 5 '08 #13
JosAH
11,448 Expert 8TB
So true. I dictate these replies and my secretary types them in.
I simply whistle my replies in my good ol' audio coupler.

kind regards,

Jos
Mar 5 '08 #14
ok my friends is this closer to what i need for this program


//Sums all the numbers from 1 to 1000
//But excludes the numbers 300 and 500
//

public class Robert1
{
public static void main(String[]ARGS)
{
int sum;
int i;
for(i=1;sum=0;i<1000;i++)
if(300=500+i;)
}
}
is this right yet thanks
Mar 6 '08 #15
BigDaddyLH
1,216 Expert 1GB
Does it compile? No it doesn't. Take a look at this:

Expand|Select|Wrap|Line Numbers
  1. if(300=500+i;)
The if statement should take a boolean expression -- an expression that's true or false. But "300=500+i;" isn't well-formed. For one, "=" means assignment, so that code is trying to assign 500 plus i to the number 300! Also, expression don't involve a semicolon. Semicolons are used to turn simple expressions into statements, so no semicolons are needed to form expressions.
Mar 6 '08 #16
so would i do this then


if(i==300) (i==500)
Mar 6 '08 #17
if(i==300)
system.outprintln
Mar 6 '08 #18
JosAH
11,448 Expert 8TB
if(i==300)
system.outprintln
Please don't just try to blindly guess until you've got something right (by accident);
it is never going to work. What is it you're trying to express?

kind regards,

Jos
Mar 6 '08 #19
im trying to exclude 300 and 500 from my program


//Sums all the numbers from 1 to 1000
//But excludes the numbers 300 and 500
//

public class Robert1
{
public static void main(String[]ARGS)
{
int sum;
int i;
for(i=1;sum=0;i<1000;i++)
if(

im stuck right here
Mar 6 '08 #20
how do i wright a java program that adds the numbers between 1 and 1000 but skips 300 and 500 any help on this matter i would greatly appreciate

thank you
//Try the followin code

package addnumber;

/**
*
* @author Deepak
*/
public class AddNumber {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
long sum = 0;

for (int i=1; i<10001; i++) {
if (i !=300 && i !=500){ // ignore 300 and 500
sum = (sum + i);
}
}
System.out.println("Sum of number 1 to 1000 excluding 300 and 500 is: " + sum);
// TODO code application logic here
}

}
Mar 7 '08 #21
Laharl
849 Expert 512MB
We don't spoonfeed code here, trdeepak. We give hints and help, but we do not just give code, with or without an explanation. The idea is to get the OP to understand on his or her own, not to just dump code that can be c/p'd and handed in.
Mar 7 '08 #22
i did most of the code all ready look for yourself
Mar 7 '08 #23
Laharl
849 Expert 512MB
You've got a solid start, it's true. However, you don't have any of the actual code to carry out this task. Look at this tutorial from Sun for a good start on where to go from here. Look at if statements in particular.
Mar 7 '08 #24
how do i wright a java program that adds the numbers between 1 and 1000 but skips 300 and 500 any help on this matter i would greatly appreciate

thank you
Hi Robert

This may help u

Expand|Select|Wrap|Line Numbers
  1. public class Add {
  2.  
  3.  
  4.     public static void main(String[] args) {
  5.     long sum = 0;
  6.  
  7.     for (int i=1; i<=1000; i++) {
  8.         if(i==300 || i==500){
  9.             }
  10.         else{
  11.             sum = (sum + i);
  12.             }
  13.         }
  14.  
  15.     System.out.println("Sum of number 1 to 1000 excluding 300 and 500 is: " + sum);
  16.     }
  17.  
  18.     }
Mar 7 '08 #25
JosAH
11,448 Expert 8TB
@sujansarathi: we don't spoonfeed code here; read the rules of the game by
clicking the 'Help' link in the top right corner of this page. The OP doesn't learn
anything by copying and pasting boiler plate code. Don't do that anymore.

Since the cat is out of the bag, I propose another solution to the OP; no
explanation though but it implements the hint I gave in my first reply:

Expand|Select|Wrap|Line Numbers
  1. public class Add {
  2.  
  3.     public static void main(String[] args) {
  4.     long sum = 1000L*1001L/2;
  5.  
  6.     System.out.println("Sum of number 1 to 1000 excluding 300 and 500 is: " + (sum-300-500));
  7.     }
  8. }
kind regards,

Jos (mod)
Mar 7 '08 #26
karthickkuchanur
156 100+
//Try the followin code

package addnumber;

/**
*
* @author Deepak
*/
public class AddNumber {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
long sum = 0;

for (int i=1; i<10001; i++) {
if (i !=300 && i !=500){ // ignore 300 and 500
sum = (sum + i);
}
}
System.out.println("Sum of number 1 to 1000 excluding 300 and 500 is: " + sum);
// TODO code application logic here
}

}
I think u might be wrongf in this condition
i !=300 && i !=500) any way nothing problem
Mar 8 '08 #27
how do i wright a java program that adds the numbers between 1 and 1000...
When you have to solve a programming problem, I find it helpful to list out in words what you are trying to do;
  • the answer is the sum of numbers 1 to 1000
  • except 300 and 500
  • somehow, either add the numbers individually
  • just do not add 300 and 500
  • or figure a loop that will increment the number to add to the answer
  • figure out a way to check in the loop if 300 or 500 has come up and do not add them to the answer

So... possible program one: Brute force
answer = 1
answer = answer + 2
answer = answer + 3
.....
answer = answer + 299
// skip 300
answer = answer + 301
.....
answer = answer + 499
// skip 500
answer = answer + 501
... a thousand lines later ...
answer = answer + 1000
// It will work, but kind of dumb eh?

Possible solution two: A Loop of some sort

for (every number between 1 and 1000)
if it is a number I don't want, skip it (300 or 500)
otherwise, answer = answer + the current number
end of loop
// Perhaps an easier approach (at least easier to to type it in)

Possible solution three: look up Karl Friedrich Gauss's answer
  • very cool
  • no loops
  • three steps

Just remember, if you can't write the problem down clearly in words, you don't understand the problem, and most likely, your solution will not work. Also, if your solution seems like a lot of work (like writing a thousand plus lines of code in the brute force approach) then there is probably a better way to solve it.

The Java language has many ways to loop, (while, do-while, for) and many conditional statements, (switch-case, if, if-else). Your job is to learn how to use these statements by putting them in some sort of order (a program) that will solve your problem. Feel free to ask how a particular statement works, but don't be discouraged if the answer is to look it up (we will help you with where to look it up). Just don't expect a solution to your homework, and if you do use a hint you find here, give credit where credit is due; like:

// solution based on help from the Java forum....
Mar 8 '08 #28
sukatoa
539 512MB
or (int i=1; i<10001; i++) {
if (i !=300 && i !=500){ // ignore 300 and 500
sum = (sum + i);
the if statement always satisfied until the end of the loop... ;-)
Mar 8 '08 #29

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
6
by: Patrick | last post by:
Hello all! I am porting an application from C++ to Java and have run into a problem using the DataInputStream reader object. The file I am trying to read in is anywhere from 20 to 60 MB and has a...
40
by: Eitan | last post by:
Hello, I know Java-Applet (written for JBuilder). I would like to know about dotnet technology, pros and cons in comparation to Java-Applet technololgy. Can I write a program in dotnet, like...
6
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm...
6
by: LTO | last post by:
What is going on with MS VC compiler team? The following trivial program will give wrong answer of 9 instead of 10. Perhaps MS is spending too much time on managed world and forget about the real...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
7
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.