Question

In: Computer Science

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:

  1. Sum
  2. Difference
  3. Product
  4. Average
  5. Maximum of the two
  6. 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 compounded monthly. Print out the balance for first three months.

For example:

Initial balance: 1000

Annual interest rate: 6.0

Balance after first month: 1005.00

Balance after second month: 1010.03

Balance after third month: 1015.08

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

Question 3: 5pts

Scan a string as user input and print the following: (i) the first character, (ii) The last character.

For example:

String: Java

First letter: J

Last letter: a

Hint: use the length() and charAt() functions from String class.

Solutions

Expert Solution

Question 1)


import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   System.out.print("Enter first integer : ");
   int n1=sc.nextInt(); //n1 as first integer input
   System.out.print("Enter second integer : ");
   int n2=sc.nextInt(); //n2 as second integer input
   System.out.println("Sum : "+(n1+n2)); //print sum
   System.out.println("Difference : "+(n1-n2)); //print diffference
   System.out.println("Product : "+(n1*n2)); //print product
   System.out.println("Average : "+(double)(n1+n2)/2); //print average
   System.out.println("Maximum : "+Math.max(n1,n2)); //print maximum using max function
       System.out.println("Minimum : "+Math.min(n1,n2)); //print Minimum using min function
   }
}

Output:

Question 2)


import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   System.out.print("Initial balance: ");
   double bal=sc.nextDouble(); //bal as initial balance
   System.out.print("Annual interest rate : ");
   double rate=sc.nextDouble(); //rate as annual interest rate
   double rate_p_m=rate/12; //convert into per month rate
   double bal1=bal+(bal*rate_p_m/100); //balance after 1st month
   double bal2=bal1+(bal1*rate_p_m/100); //after 2nd month
   double bal3=bal2+(bal2*rate_p_m/100); //after 3rd month
   System.out.println("Balance after first month: "+bal1); //print Balance after 1st month
   System.out.println("Balance after second month: "+bal2); //print balance after 2nd month
   System.out.println("Balance after third month: "+bal3); //print balance after 3rd month
     
   }
}

Output:

Question 3)

  
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   System.out.print("Enter string: ");
   String s=sc.nextLine(); //Read the line to store the string in s
   System.out.println("First letter: "+s.charAt(0)); //charAt(0) will give first letter
  
   //As strings are zero indexed ,the last index will be (length-1)
   System.out.println("Last letter: "+s.charAt(s.length()-1));
     
   }
}

Output:

  


Related Solutions

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....
Martin wants to create a program that requires the user to input 10 integers then print...
Martin wants to create a program that requires the user to input 10 integers then print the total number of even integers, highest even integer, lowest even integer, total number of odd integers, highest odd integer, and lowest odd integer. If, however, the user inputs zero (0), the program will display “You have not yet entered 10 integers.” Below is the sample output of the program Martin created. Enter the integers: Integer 1: 15 Integer 2: 9 Integer 3: 71...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
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...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT