In: Computer Science
Question 1: 5pts
Write a program to receive two integers as user input and then print:
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.
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: