Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 4.18
- import java.util.Scanner;
- public class LabProgram
- {
- public static void main(String[] args)
- {
- //Create the scanner class
- Scanner sc = new Scanner(System.in);
- //Declare a variable userString of type string.
- String userString;
- //Declare a boolean variable result.
- boolean result = true;
- //Take the input from the user.
- userString = sc.next();
- //converts strings to sequence of characters.
- for(char ch: userString.toCharArray())
- {
- //check whether the specified character is a digit.
- if(!Character.isDigit(ch))
- {
- result = false;
- }
- }
- if(result)
- {
- System.out.println("yes");
- }
- else
- {
- System.out.println("no");
- }
- }
- }
- 4.19
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String password = in.nextLine();
- String modified = "";
- for(int i = 0; i < password.length(); ++i) {
- if(password.charAt(i) == 'i') {
- modified += "!";
- } else if(password.charAt(i) == 'a') {
- modified += "@";
- } else if(password.charAt(i) == 'm') {
- modified += "M";
- } else if(password.charAt(i) == 'B') {
- modified += "8";
- } else if(password.charAt(i) == 'o') {
- modified += ".";
- } else {
- modified += password.charAt(i);
- }
- }
- modified += "q*s";
- System.out.println(modified);
- }
- }
- 4.20
- import java.util.Scanner;
- public class LabProgram {
- public static String RemoveSpaces (String userString) {
- String result = "";
- for(int i = 0; i < userString.length(); ++i) {
- if(userString.charAt(i) != ' ') {
- result += userString.charAt(i);
- }
- }
- return result;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println(RemoveSpaces(in.nextLine()));
- }
- }
- 4.21
- import java.util.Scanner;
- public class LabProgram {
- public static String RemoveSpaces (String userString) {
- String result = "";
- for(int i = 0; i < userString.length(); ++i) {
- if(userString.charAt(i) != ' ') {
- result += userString.charAt(i);
- }
- }
- return result;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println(RemoveSpaces(in.nextLine()));
- }
- }
- 4.22
- import java.util.Scanner;
- public class LabProgram
- {
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- int n = keyboard.nextInt();
- if(n < 20 || n > 98)
- {
- System.out.print("Input must be 20-98");
- System.out.println("");
- }
- else
- {
- System.out.print(n);
- while(n % 11 != 0)
- {
- n = n - 1;
- System.out.print(" " + n);
- }
- System.out.println(" ");
- }
- keyboard.close();
- }
- }
- 4.23
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int start = in.nextInt(), end = in.nextInt();
- if (start > end) {
- System.out.println("Second integer can't be less than the first.");
- } else {
- while (start <= end) {
- System.out.print(start + " ");
- start += 10;
- }
- System.out.println();
- }
- }
- }
- 4.24
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String s;
- while (true) {
- s = in.nextLine();
- if (s.equalsIgnoreCase("quit") || s.equalsIgnoreCase("q")) {
- break;
- }
- for (int i = s.length()-1; i >= 0; --i) {
- System.out.print(s.charAt(i));
- }
- System.out.println();
- }
- }
- }
- 4.25
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String args[]){
- Scanner scanner = new Scanner(System.in);
- String s = scanner.nextLine();
- boolean flag = true;
- // i is the starting index of the string s
- // j is the ending index of the string s
- int i = 0, j = s.length()-1;
- // Checking the value if character at index i with the character at index j
- // If characters match, then move i to its next position and j t its previous position
- // If characters does not match then the string is not a palindrome.
- // In the case of does not match update the value of flag to false and break the loop
- while(i<j){
- if(s.charAt(i)==' '){
- i++;
- }
- else if(s.charAt(j)==' '){
- j--;
- }
- else {
- if(s.charAt(i)!=s.charAt(j)){
- flag = false;
- break;
- }
- i++;
- j--;
- }
- }
- if (flag) {
- System.out.println(s+" is a palindrome");
- } else {
- System.out.println(s+" is not a palindrome");
- }
- }
- }
- 4.26
- import java.util.Scanner;
- public class LabProgram {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int x1, y1, n1, x2, y2, n2;
- x1 = in.nextInt();
- y1 = in.nextInt();
- n1 = in.nextInt();
- x2 = in.nextInt();
- y2 = in.nextInt();
- n2 = in.nextInt();
- for (int x = -10; x <= 10; x++) {
- for (int y = -10; y <= 10; y++) {
- if (x1*x + y1*y == n1 && x2*x + y2*y == n2) {
- System.out.println(x + " " + y);
- return;
- }
- }
- }
- System.out.println("No solution");
- }
- }
- 4.27
- import java.util.Scanner;
- //declare class DrawRightTriangle
- public class DrawRightTriangle
- {
- //main method
- public static void main(String[] args)
- {
- //Creating a scanner object to take the input from the user
- Scanner scnr = new Scanner(System.in);
- //Declaring variable to store the input
- char triangleChar;
- int triangleHeight;
- //Prompting for a character that a user wants to print on the screen
- System.out.println("Enter a character:");
- triangleChar = scnr.next().charAt(0);
- //Prompting for the height of the
- //triangle that a user wants to print
- System.out.println("Enter triangle height:");
- triangleHeight = scnr.nextInt();
- System.out.println("");
- //Use nested for-loops to print the right angle traingle
- ///for the user specified character
- for(int i=0; i<triangleHeight; i++)
- {
- for(int j=0; j<=i; j++)
- {
- //This prints on the same line
- System.out.printf("%s ",triangleChar);
- }
- //print the newline for each row
- System.out.println();
- }
- }
- }
- 4.28
- import java.util.Scanner;
- public class DrawHalfArrow {
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- int arrowBaseHeight = 0;
- int arrowBaseWidth = 0;
- int arrowHeadWidth = 0;
- System.out.println("Enter arrow base height:");
- arrowBaseHeight = scnr.nextInt();
- System.out.println("Enter arrow base width:");
- arrowBaseWidth = scnr.nextInt();
- System.out.println("Enter arrow head width:");
- arrowHeadWidth = scnr.nextInt();
- while (arrowHeadWidth<=arrowBaseWidth) {
- System.out.println("arrow head width is not larger than arrow base width");
- System.out.println("Enter arrow head width:");
- arrowHeadWidth = scnr.nextInt();
- }
- System.out.println();
- StringBuilder row = new StringBuilder();
- for (int x = 1; x <= arrowBaseWidth; x++) {
- row.append("*");
- }
- for (int i = 1; i <= arrowBaseHeight; i++) {
- System.out.println(row);
- }
- int tmp = arrowHeadWidth;
- for (int y = 1; y <= arrowHeadWidth; y++) {
- for (int z = tmp; z > 0; z--) {
- System.out.print("*");
- }
- tmp -= 1;
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement