## how do i repeatedly multiply a number in java by 2 until it reaches 1 million?

write a java program that takes two numbers as input and display the product of two numbers.

java program to multiply two numbers

multiply two strings in java

for loop multiplication

import java.util.Scanner; class Main { static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { int testNumber = userInput.nextInt(); do{ System.out.println(newNumber * 2); newNumber++; }while( testNumber < 1000000); } }

You have the right idea with your loop, but you have multiple problems with your variables.

- Your first problem is that you read in a variable from the user -
`testNumber`

, but then you are (incorrectly) manipulating a completely**different**variable -`newNumber`

. - Your second problem is that you are testing the
**unchanged**variable as your stop condition.

You probably want your loop to be something like:

do { testNumber = testNumber * 2; System.out.println(testNumber); } while(testNumber < 1000000);

**Conditionals and Loops,** This the the form of iteration you'll likely be using most often. Line 2 means get the current value of n, multiply it by three and add one, and assign the Let us write a function now to sum up all the elements in a list of numbers. 1, so the loop will continue running until it reaches its termination condition, (i.e. n == 1). The following Java program demonstrates multiplication of two numbers. This is a trivial example program, however it shows how to handle input and output, performing calculations and the use of library classes such as Scanner in Java language.

You need to update the number after you multiply it by 2:

newNumber = newNumber * 2; System.out.println(newNumber);

Also you are using `newNumber`

and `testNumber`

and `newNumber`

doesn't appear to be defined anywhere...

}while( ***testNumber***newNumber*** < 1000000);

You need to pick one because if you are updating `newNumber`

but comparing `testNumber`

in your loop you will have created an infinite loop.

The code you have shown shouldn't compile unless you are leaving something out of your post.

**7. Iteration,** Here we will see two Java programs, first program takes two integer numbers (entered Example 1: Program to read two integer and print product of them Example 2: Read two integer or floating point numbers and display the multiplication. Contribute your code and comments through Disqus. Previous: Write a program in Java to display the cube of the number upto given an integer. Next: Write a program in Java to display the n terms of odd natural number and their sum.

You can also make a recursive method for it.

public int reachMillion(int num) { if(num<=0) return -1; // indicating it is not possible. if(num>=1000000) // Base Condition denoting we have reached 1 million return num; return reachMillion(num*2); // recursive part to multiply by 2 until we reach 1 million }

**Java Program to Multiply Two Numbers,** Let's say we wanted to sum all 1, 2, and 3 digit prime numbers. used whenever you want to continue looping until some condition changes from true to false. Note that it's often helpful to run through loops by hand to understand how they work. which 2N is greater than one million), while loops are key, unless you can Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit. For example: persistence(39) => 3 # Because 3*9 = 27, 2*7 = 14, 1*4=4 # and 4 has only one digit.

class Main { private static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { int newNumber = 0; do{ System.out.println("Enter a positive number: "); try{ newNumber = userInput.nextInt(); }catch(Exception ignored){ } System.out.println(""); }while(newNumber <= 0); System.out.println("----- " + newNumber + " multiply by 2 ------"); while(newNumber <= 1_000_000){ System.out.print("2 * " + newNumber +" = "); newNumber <<= 1;//in some compilers left shift is faster than multiply System.out.println(newNumber); } }

**[PDF] 1 Loop Examples,** In this tutorial, we will learn how to use while and do while loop in Java with the Program to multiply matrix How about you need to print a sentence one million times? can simply write the print statement one time and run it for any number of times. Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10. You have the right idea with your loop, but you have multiple problems with your variables. Your first problem is that you read in a variable from the user - testNumber, but then you are (incorrectly) manipulating a completely different variable - newNumber.

@brso05 has done well describing what went wrong here. I'd like to offer a complete example:

import java.util.Scanner; public class Main { private static Scanner userInputScanner = new Scanner(System.in); public static void main(String[] args) { System.out.print("Please input a number: "); int userInputNumber = userInputScanner.nextInt(); System.out.println(); int newNumber = userInputNumber; while (newNumber < 1_000_000) { newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left System.out.println(newNumber); } } }

##### Try it online!

**Beware!** That code does not handle any bad user input. For instance, if you give it `0`

, it will loop forever, and if you give it `foo`

, it will crash. In case you want to handle all the edge cases of user input, this will do that:

import java.util.*; public class Main { private static Scanner userInputScanner = new Scanner(System.in); public static void main(String[] args) { int userInputNumber; // while(true) { System.out.print("Please input a number: "); if (userInputScanner.hasNext()) { // The user gave us something, but we don't know if it's a number String rawUserInput = userInputScanner.next(); try { userInputNumber = Integer.parseInt(rawUserInput); // If that previous line runs, the user has given us an integer! System.out.println(); if (userInputNumber > 0) { // The user has given a valid number. Break out of the loop and multiply it! break; } else { // The user has given a bad number. Tell them why and ask again. System.out.println("The number has to be greater than 0."); } } catch (NumberFormatException exception) { // The user has given us something, but it wasn't an integer System.out.println(); System.out.println("That is not a number: " + exception.getMessage()); } } else { // There is no input, so we can't do anything. return; } } // Done looping through user input int newNumber = userInputNumber; while (newNumber < 1_000_000) { newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left System.out.println(newNumber); } } }

##### Try it online!

**Java while and dowhile Loop,** with the given Comparator · Expected number of moves to reach the end of a board We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. import java.io. Recursively add a to get the multiplication of two numbers. For example, there is a range of data, Row 1 includes years, Row 2 includes data values, and you want to sum all values in Row 2 until the corresponding cell in Row 1 is reached 2014 as shown as below screenshot. Sum numbers until a certain value reached in adjacent cell

**Write you own Power without using multiplication(*) and division ,** Given a number n, count minimum steps to minimize it to 1 according to the following criteria: As per greedy approach we may choose the step that makes n as low as possible and continue the same, till it reaches 1. Eg: Given n = 10 , Greedy –> 10 /2 = 5 -1 = 4 /2 = 2 /2 = 1 ( 4 steps ). Java program to minimize n to 1. Hii,, I need to make a program that takes in a user’s number and keep dividing it by 2 until it is 1.I need to display the division steps i'm kinda new to c++ and i do not know how to keep making it divide by 2 until 1

**Minimum steps to minimize n as per given condition,** In this way millions of trees go to make up the bulk companies are at full liberty to The : creasing in number and importance, no specific enumeration i l i THE Washingto 2 Diplogrsplh. thgfi. .. gig Dryers. c eap spa 1. gyestuif, fluorescent. of Africa, until it reaches a point about the second degree oi north latitude (long. So we know that it's not 1 doubled a whole number of times. The best we can say at that point is 2 10 <1080<2 11. A little further investigation will tell you that 1080=2 10 +56. If your calculator cannot do base 2 logarithms then you'll have to use the natural logarithm ( ln or log e) and to do that you'll need to know that log b (x)=ln(x)/ln(b).

**Scientific American,** 1. How much effort does it take to do long multiplication of two numbers? 2. How can we do better Going from right to left, we repeatedly do addition of digits. Its result 10 × u 1 million digits, long multiplication needs almost 4 trillion basic operations. (of which 1 trillion again until we reach a single digit. With Karatsuba's A "do until" loop is a control structure that executes a block of code repeatedly until a certain Boolean conditional statement (e.g. end_of_file == TRUE) becomes true.

##### Comments

`newNumber *= 2;`

`System.out.println(newNumber);`

- Note that you can write
`1_000_000`

instead of`1000000`

for better readability (since Java 7). - Beat me to the punch. ;)
- While this works, it doesn't help the asker understand why their code doesn't work, nor why yours does.
- Note that there is also the compound-assignment operator
`<<=`

. So you could do`newNumber <<= 1;`

. - I'd be careful with statements like
`<< 1`

being faster than`* 2`

. It also heavily depends on compiler and CPU optimization. - Why not just a
`while`

loop, then? - I just want to show, how to do with
`do-while loop`

.