total of two code use DRJAVA and make sure it is work code 1 import java.util.Sc
ID: 3935413 • Letter: T
Question
total of two code
use DRJAVA and make sure it is work
code 1
import java.util.Scanner;
import java.lang.Math;
class Lesson_22_Activity_One {
public static void main(String[] args)
{
1. Write the code to take a String and print it with one letter per line.
Sample run:
Enter a string:
bought
b
o
u
g
h
t
code 2
import java.util.Scanner;
import java.lang.Math;
class Lesson_22_Activity_One {
public static void main(String[] args)
{
Write the code to take a String and print it diagonally.
Sample run:
Enter a string:
bought
b
o
u
g
h
t
Explanation / Answer
Question 1:
Lesson_22_Activity_One.java
import java.util.Scanner;
import java.lang.Math;
class Lesson_22_Activity_One {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = scan.next();
for(int i=0; i<s.length(); i++){
System.out.println(s.charAt(i));
}
}
}
Output:
Enter a string:
bought
b
o
u
g
h
t
Question 2:
Lesson_22_Activity_One.java
import java.util.Scanner;
import java.lang.Math;
class Lesson_22_Activity_One {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = scan.next();
for(int i=0; i<s.length(); i++){
for(int j=0; j<i; j++){
System.out.print(" ");
}
System.out.println(s.charAt(i));
}
}
}
Output:
Enter a string:
bought
b
o
u
g
h
t
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.