Solved

Posted on 2011-03-24

Math

How to write a program using this statement, int n = (int)Math.floor(Math.random()*100000+1);?

lass GuessingGame {

public static void main(String[] args) {

int n = (int)Math.floor(Math.random()*100000+1);

int sum = 0;

int digit;

number = x;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

System.out.println("Given number= "+number+"; sum of digits = "+sum);

}

}

How to write a program using this statement, int n = (int)Math.floor(Math.rando

lass GuessingGame {

public static void main(String[] args) {

int n = (int)Math.floor(Math.rando

int sum = 0;

int digit;

number = x;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

x = x/10;

digit = x % 10;

sum += digit;

System.out.println("Given number= "+number+"; sum of digits = "+sum);

}

}

3 Comments

Get Random Number between 2 and 100,000 and store in Guess

Set Low Guess = 0

Set High Guess = 100001

Start Loop

Ask User if Guess is correct and ask for lower or higher

If higher

move guess to low guess

if guess + 100 < high guess

add 100 to guess goto start loop

else if guess +10 < high guess

add 10 to guess goto start loop

else add 1 to guess goto start loop

if lower

move guess to high guess

if guess - 100 > low guess

subtract 100 from guess goto start loop

else if guess - 10 > low guess

subtract 10 from guess goto start loop

else subtract 1 from guess goto start loop

End Loop

NOTE - the more nested if's you put in the faster the program should get to the correct number.

```
public static void main(String[] args)
{
int n = (int)Math.floor(Math.random()*100000+1);
int sum = 0;
int digit;
int x = n;
while(x > 0)
{
digit = x % 10;
x /= 10;
sum += digit;
}
System.out.println("Given number= "+number+"; sum of digits = "+sum);
}
```

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Excel - convert k M or G suffix to real number | 6 | 114 | |

Octane 98 vs 95 petrol / gasoline : mileage, pros & cons | 10 | 106 | |

new car buying (price bargaining) ideas.. | 5 | 91 | |

Geomentry-Fundamental concepts-Angles | 3 | 66 |

This is a research brief on the potential colonization of humans on Mars.

Get a first impression of how PRTG looks and learn how it works. This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

- Linux
- Windows OS
- Networking
- Paessler
- Network Management
- Network Analysis, Network Operations

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**21** Experts available now in Live!