Java program that adds the numbers between 1 and 1000

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #16
    Does it compile? No it doesn't. Take a look at this:

    [CODE=Java]if(300=500+i;)[/CODE]

    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.

    Comment

    • robert scherer
      New Member
      • Mar 2008
      • 10

      #17
      so would i do this then


      if(i==300) (i==500)

      Comment

      • robert scherer
        New Member
        • Mar 2008
        • 10

        #18
        if(i==300)
        system.outprint ln

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by robert scherer
          if(i==300)
          system.outprint ln
          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

          Comment

          • robert scherer
            New Member
            • Mar 2008
            • 10

            #20
            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

            Comment

            • trdeepak
              New Member
              • Mar 2008
              • 2

              #21
              Originally posted by robert scherer
              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.prin tln("Sum of number 1 to 1000 excluding 300 and 500 is: " + sum);
              // TODO code application logic here
              }

              }

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #22
                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.

                Comment

                • robert scherer
                  New Member
                  • Mar 2008
                  • 10

                  #23
                  i did most of the code all ready look for yourself

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #24
                    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.

                    Comment

                    • sujansarathi
                      New Member
                      • Mar 2008
                      • 6

                      #25
                      Originally posted by robert scherer
                      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

                      [code=java]
                      public class Add {


                      public static void main(String[] args) {
                      long sum = 0;

                      for (int i=1; i<=1000; i++) {
                      if(i==300 || i==500){
                      }
                      else{
                      sum = (sum + i);
                      }
                      }

                      System.out.prin tln("Sum of number 1 to 1000 excluding 300 and 500 is: " + sum);
                      }

                      }[/code]

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #26
                        @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:

                        [code=java]
                        public class Add {

                        public static void main(String[] args) {
                        long sum = 1000L*1001L/2;

                        System.out.prin tln("Sum of number 1 to 1000 excluding 300 and 500 is: " + (sum-300-500));
                        }
                        }[/code]

                        kind regards,

                        Jos (mod)

                        Comment

                        • karthickkuchanur
                          New Member
                          • Dec 2007
                          • 156

                          #27
                          Originally posted by trdeepak
                          //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.prin tln("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

                          Comment

                          • dwurmfeld
                            New Member
                            • Mar 2008
                            • 9

                            #28
                            Originally posted by robert scherer
                            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....

                            Comment

                            • sukatoa
                              Contributor
                              • Nov 2007
                              • 539

                              #29
                              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... ;-)

                              Comment

                              Working...