File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package programmingInJava ;
2+
3+
4+ public class Binary {
5+
6+ public static void main (String [] args ) {
7+ int n = Integer .parseInt (args [0 ]);
8+
9+ int power = 1 ;
10+ while (power <= n /2 ) {
11+ power *= 2 ;
12+ }
13+
14+ while (power > 0 ) {
15+ if (n < power ) {
16+ System .out .print (0 );
17+ }
18+ else {
19+ System .out .println (1 );
20+ n -= power ;
21+ }
22+ power /= 2 ;
23+ }
24+ System .out .println ();
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package programmingInJava ;
2+
3+ public class Gambler {
4+
5+ public static void main (String [] args ){
6+ int stake = Integer .parseInt (args [0 ]);
7+ int goal = Integer .parseInt (args [1 ]);
8+ int trials = Integer .parseInt (args [2 ]);
9+
10+ int bets = 0 ;
11+ int wins = 0 ;
12+
13+ for (int t = 0 ; t < trials ; t ++) {
14+ int cash = stake ;
15+ while (cash > 0 && cash < goal ) {
16+ bets ++;
17+ if (Math .random () < 0.5 ) cash ++;
18+ else cash --;
19+ }
20+ if (cash == goal ) wins ++;
21+ }
22+
23+ System .out .println (wins + "wins of " + trials );
24+ System .out .println ("Percent of games won = " + 100.0 * wins / trials );
25+ System .out .println ("Avg # bets = " + 1.0 * bets / trials );
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 11package programmingInJava ;
22
3+
34public class HarmonicNumber {
5+
6+ public static void main (String [] args ) {
7+ int n = Integer .parseInt (args [0 ]);
8+
9+ double sum = 0.0 ;
10+ for (int i = 1 ; i <= n ; i ++) {
11+ sum += 1.0 / i ;
12+
13+ }
14+
15+ System .out .println (sum );
16+ }
417
518}
Original file line number Diff line number Diff line change 1+ package programmingInJava ;
2+
3+
4+ public class Squrt {
5+
6+ public static void main (String [] args ) {
7+ double c = Double .parseDouble (args [0 ]);
8+ double epsilon = 1e-15 ;
9+ double t = c ;
10+
11+ while (Math .abs (t - c /t ) > epsilon *t ) {
12+ t = (c /t + t ) / 2.0 ;
13+ }
14+ System .out .println (t );
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments