Created
May 17, 2022 03:47
-
-
Save HaradaKumiko/54678f2fa6c01a99a67b0a20eccdf79b to your computer and use it in GitHub Desktop.
Latihan Soal: Struktur Data - Farhan Rivaldy 2111003
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
import java.util.Scanner; | |
import java.util.Stack; | |
public class CharReverseJava { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.println("Input Kata : "); | |
String kata = input.nextLine(); | |
Stack stack = new Stack(); | |
for (int i = 0; i < kata.length(); i++){ | |
stack.push(kata.charAt(i)); | |
} | |
String reverseKata = ""; | |
while (!stack.isEmpty()){ | |
reverseKata += stack.pop(); | |
} | |
System.out.println(reverseKata); | |
} | |
} |
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
import java.util.Scanner; | |
import java.util.Stack; | |
public class FaktorialJava { | |
public static void main(String[] args) { | |
int initFactorial = 1; | |
Scanner input = new Scanner(System.in); | |
System.out.println("Input Angka : "); | |
int angka = input.nextInt(); | |
int counter = angka; | |
Stack<Integer> stack = new Stack<Integer>(); | |
for (int i = 0; i < counter; i++){ | |
stack.push(angka--); | |
} | |
System.out.println("Faktorial dari : " + counter + " " + stack); | |
for (int j = 0; j < counter; j++){ | |
initFactorial *= stack.pop(); | |
} | |
System.out.println(initFactorial); | |
} | |
} |
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
import java.util.Scanner; | |
import java.util.Stack; | |
public class StringReverseJava { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.println("Input Kalimat : "); | |
String kalimat = input.nextLine(); | |
String[] words = kalimat.split(" "); | |
Stack stack = new Stack(); | |
for (int i = 0; i < words.length; i++){ | |
stack.push(words[i]); | |
} | |
while (!stack.isEmpty()){ | |
String reverseKalimat = (String) stack.pop(); | |
System.out.print(reverseKalimat + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment