In: Computer Science
Question 1:
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:
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:
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.
Program1:
import java.util.*;
class Question1
{
public static void main(String args[])
{
int no1,no2;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
no1=sc.nextInt();
System.out.print("Enter the second number: ");
no2=sc.nextInt();
System.out.println("Sum: "+(no1+no2));
System.out.println("Difference: "+(no1-no2));
System.out.println("Product: "+(no1*no2));
double avg=(double)(no1+no2)/2;
System.out.println("Average: "+avg);
System.out.println("Maximum of the two: "+Math.max(no1,no2));
System.out.println("Miniimum of the two:
"+Math.min(no1,no2));
}
}
Program2:
import java.util.*;
class Question2
{
public static void main(String args[])
{
double initialBal,annualInterestRate;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the initial balance: ");
initialBal=sc.nextDouble();
System.out.print("Enter the annual Interest Rate: ");
annualInterestRate=sc.nextDouble();
double drate=annualInterestRate/100;
double balance1=initialBal * (Math.pow((1+drate/12),1));
System.out.printf("Balance after first month:
%.2f\n",balance1);
double balance2=initialBal * (Math.pow((1+drate/12),2));
System.out.printf("Balance after second month:
%.2f\n",balance2);
double balance3=initialBal * (Math.pow((1+drate/12),3));
System.out.printf("Balance after third month:
%.2f\n",balance3);
}
}
Program3:
import java.util.*;
class Question3
{
public static void main(String args[])
{
String str;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
str=sc.next();
//to get first and last character, using charAt() and length()
functions.
System.out.println("First letter: "+str.charAt(0));
System.out.println("Last letter:
"+str.charAt(str.length()-1));
}
}