ok, so i know this is stupid and probably the easiest thing in java but my brain is not working right now. so i'll ask, how do you add the numbers after you divide them in a for loop. for example:
i ask a user to enter a number, the program will then divide the number into half each time it iterate upto 5 times and then at the end it will add them all together.
For example, the user enters 5. The loop would add (5+2.5+1.25+0.625+0.3125) over the course of all iterations and then print out 9.6875

here's my code:

import java.util.Scanner;

public static void main(String[] args) {


    Scanner Keyboard = new Scanner(System.in);
    System.out.print("Please enter your number: ");
    double userNum = Keyboard.nextDouble();
    double total = 0.0;

    for(int i = 0; i <= 4; i++){                    //runs the loop 
        userNum = (userNum/2.0);           // divides user number by 2 each time it runs through the loop


        System.out.println(userNum + userNum);   //prints the number and its subsequent reults

        }

it does the division just fine but how do i add integers together??

thanks

Recommended Answers

All 7 Replies

Add each new result to a vector (array) and then after the division loop, loop over the vector, and add each result for a final value. I leave the coding to you as a simple exercise.

commented: sorry, but i cant seem to follow your directions. they're not clear. +0

You really don't need to downvote a person who is trying to help you "learn to fish". What about my post didn't you understand? Downvoting should be used only when you think someone is totally out of line. I don't think I was.

i'm creating a running total program. instead of assigning new arrays and methods. keeping it as simple as one possibly can.

You already have most of the right stuff there. You are correct in thinking that you do not need a vector, array or list.

Think of how you would total the values stored in an array. You need to do something similar here. You need to add values to the "total" variable inside the loop.

Here are some things you should think about:
1. Should you add userNum to total before or after the assignment statement that divides userNum in half?
2. Consider initializing total to something other than zero. That might help you get the result you want.
3. Should you print the total inside the loop or after exiting the loop?

thanks JeffGrigg, thats very clear actually.

total += userNum;

close the loop and then print the statement in total
obviously the total has to be done after all calculations in done inside the loop.

Scanner Keyboard = new Scanner(System.in);
    System.out.print("Please enter your number: ");
    double userNum = Keyboard.nextDouble();
    double total = 0.0;

    for(int i = 0; i < 5; i++){
        userNum = (userNum/2.0);
        total += userNum;
        System.out.println(userNum + userNum);

        }

    System.out.println("your total is " + total);

still not done since its not printing the correct reslut perhaps as you said i have initalize total to something other that zero.

That code works except for the initialization of total and the loop limiter. An accumulator in this case is simpler than keeping a register of all the results, but there may be times when you want that information for more complex calculations.

This should work for you:

    Scanner Keyboard = new Scanner(System.in);
    System.out.print("Please enter your number: ");
    double userNum = Keyboard.nextDouble();
    double total = userNum;

    // Only do 4 iterations - the input userNum is the first item.
    for(int i = 1; i < 5; i++)
    {
        // Print previous userNum amount.
        System.out.println(userNum);
        userNum = (userNum/2.0);
        total += userNum;
    }
    // Print final userNum.
    System.out.println(userNum);
    System.out.println("your total is " + total);

rubberman, thanks! For more complex calculation including factorials I mostly use methods and then then call the method in main, life is alot easier. this program is bugging me becoz i should know how to do it and yet I couldnt. Initially i wanted the number 5 to be part of the 5 number iteration. for example when i enter 5 as a user, it will print that number and all subsequent numbers below that after halving the number through operation.
but thanks anyways, i did something very simialr except for the print statement. your last print statement actually made it all clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.