tdulik

Java - first lesson

Sep 27th, 2016
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package firstproject;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     static void Pythagoras() {
  8.         double a=3, b=4, c;
  9.         Scanner s = new Scanner(System.in);
  10.         System.out.println("Please input a:");
  11.         a=s.nextDouble();
  12.         System.out.println("Please input b:");
  13.         b=s.nextDouble();
  14.         c=Math.sqrt(a*a + b*b);
  15.         // TODO Auto-generated method stub
  16.         System.out.print("Result="+c);
  17.         s.close();     
  18.     }
  19.    
  20.     static double factorial(double N) {
  21.        
  22.         double result=1;
  23.         while (N>1) {
  24.             result = result*N;
  25.             N--;
  26.         }
  27.         return result;
  28.     }
  29.    
  30.     static double fibonacci(double N) {
  31.        
  32.         double result=0, previous=0, last=1;
  33.         if (N==1) return 1;
  34.         while (N>1) {
  35.             result = last + previous;
  36.             previous = last;
  37.             last = result;
  38.             N--;
  39.         }
  40.         return result;
  41.     }
  42.    
  43.     static boolean is_prime(int number) {
  44.         //it it is prime, then>
  45.         int N=number/2;
  46.         while (N>1) {
  47.             if (number % N == 0)
  48.                 return false;
  49.             N--;
  50.         }
  51.         return true;
  52.        
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         //homework: use is_prime to print all prime numbers < N,
  57.         // where N is given by user
  58.         System.out.println("is_prime(6)="+is_prime(6));
  59.     }
  60.  
  61. }
RAW Paste Data