Skip to content

Commit 621511a

Browse files
Function
Duration 3 day's 17/09/2023 - 19/09/2023
1 parent c4542ee commit 621511a

File tree

9 files changed

+223
-0
lines changed

9 files changed

+223
-0
lines changed

Function Method/Assignment.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Assignment {
2+
public static float average(int a, int b, int c) {
3+
float avg = (a+b+c)/3;
4+
return avg;
5+
}
6+
public static boolean isEven(int n) {
7+
return (n%2 == 0)? true : false;
8+
}
9+
public static boolean palindrome(int n) {
10+
int reverse = 0, num = n;
11+
while (num>0) {
12+
int rem = num%10;
13+
reverse = (reverse * 10) + rem;
14+
num /= 10;
15+
}
16+
return (n == reverse)? true : false;
17+
}
18+
public static void method() {
19+
int a = 8, b = 9;
20+
double d = 45; // toRadians need double type of dataType
21+
System.out.println("Minimum : "+Math.min(a,b));
22+
System.out.println("Maximum : "+Math.max(a,b));
23+
System.out.println("Square root : "+Math.sqrt(a));
24+
System.out.println("Power : "+Math.pow(a,b)); //a^b
25+
System.out.println("Absolute : "+Math.abs(8.9));
26+
double c = Math.toRadians(d); // Used to convert an angle measured in degrees
27+
System.out.println("Sin : "+Math.sin(c)); // Trignometric table
28+
System.out.println("Tan : "+Math.tan(c));
29+
}
30+
public static int SumOfDigit(int n) {
31+
if (n == 1) { return 1; }
32+
return n + SumOfDigit(n-1);
33+
}
34+
}

Function Method/BinaryToDecimal.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class BinaryToDecimal {
2+
public static void main(String[] args) {
3+
4+
int binary = 10001000;
5+
6+
int count = -1, result = 0;
7+
while (binary > 0) {
8+
count++;
9+
int rem = binary%10;
10+
result = result + (rem) * ((int) Math.pow(2, count));
11+
binary /= 10;
12+
}
13+
System.out.print(result);
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class BinomialCofficient {
2+
/*
3+
* Bionomial Cofficient
4+
* C(n,r) = n! / (r! * (n-r)!)
5+
*/
6+
7+
static int factorial(int n) {
8+
if (n == 0) { return 1; }
9+
return n * factorial(n-1);
10+
}
11+
12+
public static int bionomial(int n, int r) {
13+
int n_fact = factorial(n);
14+
int r_fact = factorial(r);
15+
int nr_fact = factorial(n-r);
16+
17+
return n_fact / (r_fact * nr_fact);
18+
}
19+
20+
public static void main(String[] args) {
21+
System.out.print(bionomial(5, 2));
22+
}
23+
}

Function Method/CallByValue.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class CallByValue {
2+
// In call by value method, the value of the actual parameters is copied into the formal parameters.
3+
4+
// Example :- Swap
5+
6+
// Copied the actual parameter value only
7+
// Doing their job within the function
8+
// No Changed in actual parameter after execution of this function
9+
// This is a Call By Value
10+
public static void Swap (int a, int b) {
11+
int temp = a;
12+
a = b;
13+
b = temp;
14+
System.out.println("value of A : "+a);
15+
System.out.println("Value of B : "+b);
16+
}
17+
18+
public static void main(String[] args) {
19+
// Actual Parameter Value of A and B
20+
int a = 5, b = 10;
21+
22+
Swap(a,b);
23+
System.out.println("Value of A after function : "+a);
24+
System.out.print("Value of B after function : "+b);
25+
}
26+
}

Function Method/DecimalToBinary.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class DecimalToBinary {
2+
public static void main(String[] args) {
3+
int number = 7;
4+
int result = 0, count = 0;
5+
6+
while (number > 0) {
7+
int rem = number%2;
8+
result = result + (rem * ((int) Math.pow(10, count)));
9+
count++;
10+
number /= 2;
11+
}
12+
System.out.print(result);
13+
}
14+
}

Function Method/Factorial.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Factorial {
2+
// 5! = 5*4*3*2*1
3+
public static int factorial(int n) {
4+
if (n == 0) {
5+
return 1;
6+
}
7+
8+
// Here, I used Recursion for multiple the value of n with n-1
9+
return n * factorial(n-1);
10+
}
11+
12+
public static void main(String[] args) {
13+
System.out.print(factorial(5));
14+
}
15+
}

Function Method/Overloading.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Overloading {
2+
/* Function Overloading */
3+
4+
// Same returnType and functionName, Different parameter
5+
public static int sum(int a, int b) {
6+
return a+b;
7+
}
8+
public static int sum(int a, int b, int c) {
9+
return a+b;
10+
}
11+
12+
// Different returnType and parameter, Same functionName
13+
public static float sum(float a, float b) {
14+
return a+b;
15+
}
16+
17+
/*
18+
Different returnType, Same functionName and paramter
19+
public static long sum(int a, int b) {
20+
return a+b;
21+
}
22+
*/
23+
24+
}

Function Method/Prime.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Prime {
2+
3+
// Check if number is prime or not
4+
public static boolean isPrime(int n) {
5+
if (n == 1) {
6+
return false;
7+
} else if (n == 2){
8+
return true;
9+
}
10+
for (int i = 2; i < n; i++) {
11+
if (n % i == 0) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
18+
19+
// Optimized way to find prime
20+
public static Boolean Prime(int n) {
21+
if (n == 1) {
22+
return false;
23+
} else if (n == 2){
24+
return true;
25+
}
26+
27+
// Math.sqrt(n) = UnderRoot N; UnderRoot N * UnderRoot N = N So half it to minimize the complexity
28+
// 1*6
29+
// 2*3
30+
// 3*2
31+
// 6*1
32+
33+
for (int i=2; i<(n/2); i++) {
34+
if (n%i == 0) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
42+
// Print all Prime number
43+
public static void printPrime(int first, int last) {
44+
for (int n=first; n<=last; n++) {
45+
if(isPrime(n) == true) {
46+
System.out.print(n+" ");
47+
}
48+
}
49+
}
50+
51+
public static void main(String[] args) {
52+
printPrime(1,20);
53+
}
54+
}

Function Method/ProductOfa_b.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class ProductOfa_b {
2+
3+
// Created function
4+
// Name = product
5+
// Parameter = a and b = Formal Parameter
6+
// Return type = int
7+
public static int product (int a, int b) {
8+
return a*b;
9+
}
10+
11+
public static void main(String[] args) {
12+
int a = 5, b = 10;
13+
14+
// product function called in main function for use
15+
// Passes the argument or actual parameter
16+
System.out.print("Product : "+product(a, b));
17+
}
18+
}

0 commit comments

Comments
 (0)