Add HW01 exercises
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
public class EvenSum {
|
||||||
|
|
||||||
|
public static int sumEvenIndices(int[] array) {
|
||||||
|
ArrayList<Integer> list = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int n : array) {
|
||||||
|
list.add(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
sum += list.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] nums = {4, 7, 2, 9, 5};
|
||||||
|
System.out.println("Sum = " + sumEvenIndices(nums));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
public class Factorial{
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner num = new Scanner(System.in);
|
||||||
|
long n = num.nextInt();
|
||||||
|
long result = 1;
|
||||||
|
|
||||||
|
for (int i=1 ; i <= n; i++){
|
||||||
|
result *= i;
|
||||||
|
}
|
||||||
|
System.out.println(n + "! = " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
public class HelloWorld {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
public class Palindrome {
|
||||||
|
static boolean isPalin(String s){
|
||||||
|
int i = 0;
|
||||||
|
int j = s.length()-1;
|
||||||
|
|
||||||
|
while (i < j) {
|
||||||
|
if (s.charAt(i) != s.charAt(j)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String str = sc.nextLine();
|
||||||
|
|
||||||
|
boolean isPal = isPalin(str);
|
||||||
|
System.out.println("is "+ str+ " palindrome? "+ isPal);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class PrimeFactors {
|
||||||
|
public static ArrayList<Integer> Factors(int num){
|
||||||
|
ArrayList<Integer> factors = new ArrayList<>();
|
||||||
|
|
||||||
|
if (num <= 1){
|
||||||
|
System.out.println("no prime factor!.");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (num%2 == 0){
|
||||||
|
factors.add(2);
|
||||||
|
num /= 2;
|
||||||
|
}
|
||||||
|
for(int i = 3; i*i <= num; i+=2){
|
||||||
|
while (num%i == 0){
|
||||||
|
factors.add(i);
|
||||||
|
num /= i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (num > 1 ){
|
||||||
|
factors.add(num);
|
||||||
|
}
|
||||||
|
return factors;
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int number = sc.nextInt();
|
||||||
|
ArrayList<Integer> factors = Factors(number);
|
||||||
|
if (!factors.isEmpty()){
|
||||||
|
System.out.println("factors = "+ factors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user