Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lab 2 Question 1
- package l2q1;
- import java.util.Scanner;
- public class L2Q1 {
- public static void main(String[] args) {
- double c,f;
- Scanner kb = new Scanner(System.in);
- System.out.print("Please enter value of Fahrenheit : ");
- f=kb.nextDouble();
- c=(f-32)/1.8;
- System.out.printf("%.2f Fahrenheit = %.2f Celcius\n",f,c);
- }
- }
- Lab 2 Question 2
- package l2q2;
- import java.util.Scanner;
- public class L2Q2 {
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- double P,D,R,Y,M;
- System.out.println("This program will calculate monthly payment for car loan");
- System.out.print("The price of the car : ");
- P=kb.nextDouble();
- System.out.print("\nDownpayment : ");
- D=kb.nextDouble();
- System.out.print("\nInterest rate in % : ");
- R=kb.nextDouble();
- System.out.print("\nLoan duration in year : ");
- Y=kb.nextDouble();
- M=(P-D)*(1+R*Y/100)/(Y*12);
- System.out.printf("\nThe monthly payment is : RM%.2f", M);
- }
- }
- Lab 2 Question 3
- package l2q3;
- import java.util.Random;
- //Way to declare an array nombor[]=new int[limit_goes_here];
- public class L2Q3 {
- public static void main(String[] args) {
- int nombor[]=new int[3],sum=0,ha;
- float ave;
- System.out.println("Random number generator");
- Random cipta = new Random();
- for(ha=0;ha<3;ha++)
- {
- nombor[ha]=cipta.nextInt(101);
- System.out.printf("%d\n",nombor[ha]);
- sum+=nombor[ha];
- }
- System.out.printf("\nThe sum is %d",sum);
- ave=(float) (sum/ha);
- System.out.printf("\nThe average is %.2f\n",ave);
- }
- }
- Lab 2 Question 4
- package l2q4;
- import java.util.Scanner;
- public class L2Q4{
- public static void main (String args[]){
- int coins[]={50,20,10,5,1},i,piece,rm;
- double money;
- System.out.print("Please enter money : ");
- Scanner kb=new Scanner(System.in);
- money=kb.nextDouble();
- money=money*100.0;
- rm=(int)money;
- for(i=0;i<5;i++)
- {
- piece=rm/coins[i];
- if(piece==0)
- continue;
- System.out.printf("\n%d cents = %d pieces",coins[i],piece);
- rm-=(coins[i]*piece);
- }
- }
- }
- Lab 2 Question 5
- package l2q5;
- import java.util.Random;
- public class L2q5 {
- public static void main(String[] args) {
- int temp=0,sum=0,rndm;
- Random jeng=new Random();
- rndm=jeng.nextInt(10001);
- System.out.println(rndm);
- while(rndm>0){
- temp=rndm%10;
- sum+=temp;
- rndm/=10;
- }
- System.out.printf("\nThe sum is %d\n",sum);
- }
- }
- Lab 2 Question 6
- package l2q6;
- import java.util.Scanner;
- public class L2q6 {
- public static void main(String[] args) {
- float P,R,N,Investment;
- Scanner kb=new Scanner(System.in);
- System.out.print("Please enter value of P : ");
- P=kb.nextFloat();
- System.out.print("Please enter value of R (Percentage) : ");
- R=kb.nextFloat();
- System.out.print("Please enter value of Year : ");
- N=kb.nextFloat();
- Investment=(float) (P*(1-Math.pow((R/100),(N+1)))/(1-(R/100)));
- System.out.printf("The investment will be : RM%.2f\n",Investment);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment