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 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.
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...
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...
Write a program that prompts the user to input a string. The program then uses the...
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 of your program and...
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,...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a program that: a.Asks the user for their first name.b.Asks the user for their...
Write a program that: a.Asks the user for their first name. b.Asks the user for their age. c.Asks the user for their last name. d.Print out to the user the following information: i.Number of characters in the first & last name combined ii.The full name all in upper case letters iii.The full name all in lower case letters iv.The first letter of the name v.The age Compile and test your code in NetBeans and then on Hackerrank at www.hackerrank.com/csc127-chapter2-classwork then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT