Jump to content

Sum of numbers help.

- - - - -

  • Please log in to reply
3 replies to this topic

#1
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts

//E.M.C was here for Java 9th period 

import java.util.*;

public class numSum

{

    public static void main (String [] args)

{

    //Variables will be declared!

    Scanner input = new Scanner (System.in);

    int numb, start;

    

    System.out.print ("Input a number:");

    numb = input.nextInt ();


   //Printing of the number...

    while (numb >= 1)

    {

        System.out.println (numb);

        numb --;

    }

  

}

}



The assignment is: Create a NumbersSum application that prompts the user for a number and then displays the numbers 1 through the number entered, each on a separate line. Below the numbers, the sum is displayed.

I am having trouble with finding the sum of all the numbers. :( What is a good way to do that? Will anyone show and explain how to do it, please? :confused:

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Could you give a sample output? I Don't get what you must do..
Or is the sum only displayed once at the end?
like
5
4
3
2
1
---+
15

This would be

int numb = 5;

int total = 0;

while (numb >= 1){

        System.out.println (numb);

        total+=numb;

        numb --;

}

System.out.println (----+);

System.out.println (total);


Edited by wim DC, 02 November 2010 - 12:14 PM.


#3
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
Yes thank you! I was as I said having trouble figure out how to add those numbers together but thanks to your help I've got it, and in a few minutes I'm sure I can figure out why that works. :)


My final program was:
import java.util.*;
public class numSum
{
    public static void main (String [] args)
{
    Scanner input = new Scanner (System.in);
    int numb;
    int total = 0;
    
    System.out.print ("Input a number:");
    numb = input.nextInt ();


    while (numb >= 1){
        System.out.println (numb);
        total+=numb;
        numb --;
}
System.out.print("The sum is: ");
System.out.println (total);
  
}
}


#4
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
I just curious and tried in my ide.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package isuru;

/**
 *
 * @author Isuru
 */
import java.util.*;

public class Main {

    public static void main(String[] args) {
        //Variables will be declared!
        Scanner input = new Scanner(System.in);
        int numb;
        int total = 0;

        System.out.println("Enter a number: ");
        numb = input.nextInt();

        while(numb >= 1 ){
            System.out.println(numb);
            total += numb;
            numb--;
        }
        
        System.out.println("Total value is "+total);
    }
}

That's what I done. And oxano is right! :)
Lost!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users