Question

In: Computer Science

Question 1: Write a program to receive a string from the user and compares the first...

Question 1:

Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter.

For example:

Run 1:

Enter a word: abcdabc

First and Second Half Same

Run 2:

Enter a word: abcde

First and Second Half Different

Hint: use the Math.floor() and length() to get the halves of the string and use the equals() to perform the similarity check inside if-else statement.

**********************************************************************************

Question 2:

Write a program that reads three different integers from the users and prints “all the same” if they are all equal, “all different”, if three numbers are different and “neither” for all other cases.

For example:

Enter first number: 3

Enter first number: 2

Enter first number: 6

All Different.

**********************************************************************************

Question 3:

Write a program that reads three different floating point numbers from the users and prints the largest of those three:

For example:

First number: 2.36

Second number: 2.99

Third number: 2.78

Largest: 2.99

Solutions

Expert Solution

ANSWER 1:-


import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("Enter a word:");
   String str=input.nextLine();
   int n=str.length();
   int mid=n/2;
   String half=str.substring(0,mid);
   String sub;
  
   if (str.length() % 2==0)
   sub=str.substring(mid);
   else
   sub=str.substring(mid+1);
   if (sub.equals(half))
   System.out.println("First and Second Half Same");
   else
   System.out.println("First and Second Half Different");
  
   }
}

OUTPUT:-

ANSWER 2:-

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("Enter first number:");
   int num1=input.nextInt();
   System.out.print("Enter second number:");
   int num2=input.nextInt();
   System.out.print("Enter third number:");
   int num3=input.nextInt();
  
   if (num1 == num2 && num2 == num3)
   System.out.println("all the same");
   else if (num1 != num2 && num2!=num3)
   System.out.println("all different");
   else
   System.out.println("neither");
   }
}

OUTPUT

Answer 3:-

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("First number:");
   float num1=input.nextFloat();
   System.out.print("Second number:");
   float num2=input.nextFloat();
   System.out.print("Third number:");
   float num3=input.nextFloat();
  
   if (num1>num2){
   if (num1>num3)
   System.out.println("Largest:"+num1);
   else
   System.out.println("Largest:"+num3);
   }
   else{
   if (num2>num3)
   System.out.println("Largest:"+num2);
   else
   System.out.println("Largest:"+num3);
   }
   }
}

OUTPUT:-


Related Solutions

Write a program to receive a string from the user and compares the first and second...
Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter. For example: Run 1: Enter a word: abcdabc First and Second Half Same Run 2: Enter a word: abcde First and Second Half Different Hint: use the Math.floor() and length() to get the halves of...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Question 1: 5pts Write a program to receive two integers as user input and then print:...
Question 1: 5pts Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: 10pts An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT