Created
October 18, 2016 18:27
-
-
Save DenisZm/14b484410ed56530429d7ab7ee2d8d7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
import java.util.Scanner; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class ArithmeticProgression { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
System.out.print("Input n: "); | |
int n = in.nextInt(); | |
System.out.println((n * (n + 1)) /2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
import java.util.Scanner; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class ArithmeticProgressionRecursion { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
System.out.print("Input n: "); | |
int n = in.nextInt(); | |
ArithmeticProgressionRecursion ap = new ArithmeticProgressionRecursion(); | |
System.out.println(ap.sum(n)); | |
} | |
public int sum(int n){ | |
return (n>1) ? n+sum(n-1): n; //ternary operator if-then-else | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class BartSimpson { | |
public static void main(String[] args) { | |
BartSimpson bart = new BartSimpson(); | |
if (args.length < 1) { | |
bart.usage(); | |
} else { | |
try { | |
int count = Integer.parseInt(args[0]); | |
bart.say(count); | |
} catch (NumberFormatException ex) { | |
bart.usage(); | |
} | |
} | |
} | |
public void say(int c) { | |
for (int i = 0; i < c; i++){ | |
System.out.println("i will adopt best practices"); | |
} | |
} | |
public void usage() { | |
System.out.println("usage: BartSimpson count \n" + | |
"\t count: integer number"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
import java.util.Scanner; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class DivisibleBy { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
System.out.print("Input n: "); | |
int n = in.nextInt(); | |
System.out.print("Input m: "); | |
int m = in.nextInt(); | |
for (int i = 1; i <= n; i++) { | |
if ((i % m) == 0) System.out.println(i); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class MultiplicationTable { | |
public static void main(String[] args) { | |
for (int i = 1; i < 10; i++) { | |
for (int j = 1; j < 10; j++) { | |
System.out.printf("%4d", (i * j)); | |
} | |
System.out.print("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class MultiplicationTableHash { | |
public static void main(String[] args) { | |
for (int i = 1; i < 10; i++) { | |
for (int j = 1; j < 10; j++) { | |
if (i < j) { | |
System.out.printf("%4s", "#"); | |
} else { | |
System.out.printf("%4d", (i * j)); | |
} | |
} | |
System.out.print("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class NullToTwentyEvens { | |
public static void main(String[] args) { | |
for (int i = 0; i <= 20;) { | |
System.out.println(i); | |
i +=2; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class OddEven { | |
public static void main(String[] args) { | |
int a = (int) (Math.random() * 100); | |
System.out.println(a); | |
if ((a % 2) == 0) { | |
System.out.println("even"); | |
} else { | |
System.out.println("odd"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package homework2; | |
/** | |
* Created by Denis on 18.10.2016. | |
*/ | |
public class TenToNull { | |
public static void main(String[] args) { | |
for (int i = 10; i >= 0; i--) { | |
System.out.print(i); | |
if (i != 0) { | |
System.out.print(", "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment