469,327 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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 4365
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

Post your reply

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

Similar topics

reply views Thread by abcd | last post: by
73 posts views Thread by RobertMaas | last post: by
40 posts views Thread by Eitan | last post: by
6 posts views Thread by tigrfire | last post: by
Banfa
2 posts views Thread by Banfa | last post: by
7 posts views Thread by Sanny | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.