syad28

Lab 02 Programming

Sep 23rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. Lab 2 Question 1
  2.  
  3. package l2q1;
  4. import java.util.Scanner;
  5. public class L2Q1 {
  6.  
  7.     public static void main(String[] args) {
  8.         double c,f;
  9.         Scanner kb = new Scanner(System.in);
  10.         System.out.print("Please enter value of Fahrenheit : ");
  11.         f=kb.nextDouble();
  12.         c=(f-32)/1.8;
  13.         System.out.printf("%.2f Fahrenheit = %.2f Celcius\n",f,c);
  14.        
  15.        
  16.        
  17.     }
  18.    
  19. }
  20.  
  21. Lab 2 Question 2
  22.  
  23. package l2q2;
  24. import java.util.Scanner;
  25. public class L2Q2 {
  26.  
  27.     public static void main(String[] args) {
  28.         Scanner kb = new Scanner(System.in);
  29.         double P,D,R,Y,M;
  30.         System.out.println("This program will calculate monthly payment for car loan");
  31.         System.out.print("The price of the car : ");
  32.         P=kb.nextDouble();
  33.         System.out.print("\nDownpayment : ");
  34.         D=kb.nextDouble();
  35.         System.out.print("\nInterest rate in % : ");
  36.         R=kb.nextDouble();
  37.         System.out.print("\nLoan duration in year : ");
  38.         Y=kb.nextDouble();
  39.         M=(P-D)*(1+R*Y/100)/(Y*12);
  40.         System.out.printf("\nThe monthly payment is : RM%.2f", M);
  41.  
  42.     }
  43.    
  44. }
  45.  
  46. Lab 2 Question 3
  47.  
  48. package l2q3;
  49. import java.util.Random;
  50.  
  51. //Way to declare an array nombor[]=new int[limit_goes_here];
  52.  
  53. public class L2Q3 {
  54.     public static void main(String[] args) {
  55.         int nombor[]=new int[3],sum=0,ha;
  56.         float ave;
  57.         System.out.println("Random number generator");
  58.         Random cipta = new Random();
  59.         for(ha=0;ha<3;ha++)
  60.         {
  61.             nombor[ha]=cipta.nextInt(101);
  62.             System.out.printf("%d\n",nombor[ha]);
  63.             sum+=nombor[ha];
  64.         }
  65.         System.out.printf("\nThe sum is %d",sum);
  66.         ave=(float) (sum/ha);
  67.         System.out.printf("\nThe average is %.2f\n",ave);
  68.     }
  69.    
  70. }
  71.  
  72. Lab 2 Question 4
  73.  
  74. package l2q4;
  75. import java.util.Scanner;
  76. public class L2Q4{
  77.     public static void main (String args[]){
  78.        
  79.         int coins[]={50,20,10,5,1},i,piece,rm;
  80.         double money;
  81.         System.out.print("Please enter money : ");
  82.         Scanner kb=new Scanner(System.in);
  83.         money=kb.nextDouble();
  84.         money=money*100.0;
  85.         rm=(int)money;
  86.         for(i=0;i<5;i++)
  87.         {
  88.             piece=rm/coins[i];
  89.             if(piece==0)
  90.                 continue;
  91.             System.out.printf("\n%d cents = %d pieces",coins[i],piece);
  92.             rm-=(coins[i]*piece);
  93.         }
  94.     }
  95. }
  96.  
  97. Lab 2 Question 5
  98.  
  99. package l2q5;
  100. import java.util.Random;
  101. public class L2q5 {
  102.     public static void main(String[] args) {
  103.         int temp=0,sum=0,rndm;
  104.        
  105.     Random jeng=new Random();
  106.     rndm=jeng.nextInt(10001);
  107.    
  108.         System.out.println(rndm);
  109.     while(rndm>0){
  110.         temp=rndm%10;
  111.         sum+=temp;
  112.         rndm/=10;
  113.     }
  114.         System.out.printf("\nThe sum is %d\n",sum);
  115.     }
  116.    
  117. }
  118.  
  119. Lab 2 Question 6
  120.  
  121. package l2q6;
  122. import java.util.Scanner;
  123. public class L2q6 {
  124.     public static void main(String[] args) {
  125.    
  126.     float P,R,N,Investment;
  127.     Scanner kb=new Scanner(System.in);
  128.         System.out.print("Please enter value of P : ");
  129.         P=kb.nextFloat();
  130.         System.out.print("Please enter value of R (Percentage) : ");
  131.         R=kb.nextFloat();
  132.         System.out.print("Please enter value of Year : ");
  133.         N=kb.nextFloat();
  134.     Investment=(float) (P*(1-Math.pow((R/100),(N+1)))/(1-(R/100)));
  135.         System.out.printf("The investment will be : RM%.2f\n",Investment);
  136.     }
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment