Verilen Sayının Karesini Hesaplama
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; public class ilkProje{ public static void main(String[] args) { System.out.println("Enter a number to find its square: "); Scanner scanner = new Scanner(System.in); double number = scanner.nextDouble(); System.out.println("The square of "+number +"is "+calcSquare(number)); } public static double calcSquare(double num) { double square; square = num*num; return square; } } |
Verilen Sayının Üssünü Hesaplama
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; public class ilkProje{ // Calculate the power of given number. public static void main(String[] args) { System.out.println("Enter an integer and a power respectively: "); Scanner scanner = new Scanner(System.in); int integer = scanner.nextInt(); int power = scanner.nextInt(); System.out.println("The result is = "+ calcPower(integer,power)); } public static int calcPower(int number, int power) { int result=1; for(int i=1;i<=power;i++) { result= result * number; } return result; } } |
Faktoriyel, Permütasyon ve Kombinasyon Hesaplama
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.Scanner; public class ilkProje{ // Write a program that can calculate factorial, combination and permutation. public static void main(String[] args) { System.out.println("Enter a number to find its factorial "); Scanner scanner = new Scanner(System.in); int integer = scanner.nextInt(); System.out.println("The factorial of " +integer + " is "+ factorial(integer)); System.out.println("Enter 2 number like n and r respectively to find C(n,r) and P(n,r): "); Scanner scanner1 = new Scanner(System.in); int n1 = scanner.nextInt(); Scanner scanner2 = new Scanner(System.in); int n2 = scanner.nextInt(); System.out.println("The combination is "+ combination(n1,n2)); System.out.println("The permutation is "+ permutation(n1,n2)); } public static int factorial(int number) { int result=1; for(int i=1;i<=number;i++) { result= result *i; } return result; } public static int combination(int n, int r) { if(n>0 && r>0 && n >= r) { return (factorial(n)/(factorial(n-r)*factorial(r))); } else return 0; } public static int permutation(int n, int r) { if(n>0 && r>0 && n >= r) { return (factorial(n)/factorial(n-r)); } else return 0; } } |
Kullanıcının girdiği sayı dizisinin array kullanarak ortalamasını hesaplama
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class ilkProje{ // Write a program that calculate the average of given numbers with using array. public static void main(String[] args) { System.out.println("How many number you will enter to find its average? "); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [] numbers = new int[n]; for(int i=0;i<n; i++) { numbers[i]=scanner.nextInt(); } int sum=0; for(int j=0; j<n; j++) { sum = sum + numbers[j]; } int average = sum/n; System.out.println("The average of these numbers is "+ average); } } |
Fibonacci Örneği
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class reyhan { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter a number: "); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int answer = fibonacci(number); System.out.println("Answer is "+ answer); } public static int fibonacci(int n) { int t1=0, t2=1, sum=0; for(int i=1; i<n ; i++) { sum=t1+t2; t1=t2; t2= sum; } return sum; } } |