Pages

Total Pageviews

Sunday, June 9, 2019

Lab 5 Exercise Questions at Java Programming

class Lab06_Q1 {
   public static void main(String[] args)
   {
      for(int i=10;i<50;i+=10)
      {
         for(int j=0;j<10;j++)
         {
            int r = i+j;
            System.out.print(r + " ");
         }
         System.out.println();
      }
   }
}


import java.util.Scanner;
import java.lang.Math;

class Lab06_Q2 {
   public static void main(String[] args)
   {
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter the accuracy: ");
      int accuracy = keyboard.nextInt();
      double dAccuracy = Math.pow(10, accuracy*-1);
      System.out.println("Accuracy : " + dAccuracy);
      double pi = 4;
      double piPrev = pi;
      double num = 3;
      int iteration = 1;
      boolean sign = false;
      do {
         piPrev = pi;
         if(sign)
         {
            pi += 4/num;
            sign = false;         
         }
         else
         {
            pi -= 4/num;
            sign = true;
         }
         iteration++;
         num += 2;
       
      } while (Math.abs(pi-piPrev) > dAccuracy);
     
      System.out.println("pi = " + pi);
      System.out.println("Iteration : " + iteration);
   }
}


import java.util.Scanner;
import java.lang.Math;

class Lab06_Q3 {
   public static void main(String[] args)
   {
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter index of the fibonacci to calculate: ");
      int fibIndex = keyboard.nextInt();
      int fibSecondPrev = 1;
      int fibFirstPrev = 1;
      int temp;
      int fibCurrent;
      System.out.println("F1 = 1");
      System.out.println("F2 = 1");
      for(int i=3;i<=fibIndex;i++)
      {
         fibCurrent = fibSecondPrev + fibFirstPrev;
         System.out.println("F"+i+" = "+fibCurrent);
         fibSecondPrev = fibFirstPrev;
         fibFirstPrev = fibCurrent;
      }
   }
}





public class lab6exer1 {
   public static void main(String [] args) {
 
      for (int i = 10; i < 50; i++) {  // loop variable
         System.out.println("output number"+i);
         }
     
      for (int j = i+i, j < i+10, j++) {
         System.out.println("output number"+j);
         }
      }
 }   
       
       
       
http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg


No comments:

Post a Comment