-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.java
More file actions
175 lines (148 loc) · 5.76 KB
/
Copy pathfunctions.java
File metadata and controls
175 lines (148 loc) · 5.76 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.Scanner;
public class functions {
// Function without parameters
// This function prints Hello my World!!!!!!!!!!!!!! as output
public static void World() {
System.out.println("Hello my World!!!!!!!!!!!!!!");
return; // This return is optional here, as the method has no return type
}
// Function with parameters
// This function takes two parameters and prints their sum
public static int sum2(int num1, int num2) { // parameters
int sum = num1 + num2; // Calculate the sum of the two numbers
return sum; // Return the calculated sum
}
public static void swap(int a, int b) {
// swap
int temp = a; // Store the value of a in a temporary variable
a = b; // Assign the value of b to a
b = temp; // Assign the value of temp (original a) to b
System.out.println("After swapping, a = " + a + " and b = " + b); // Print the swapped values
}
public static int prod(int a, int b) {
int product = a * b; // Return the product of a and b
return product;
}
public static int factorial(int n) {
int f = 1; // Initialize f to 1 for each new calculation
for (int i = 1; i <= n; i++) {
f *= i; // Multiply f by the current value of i
}
return f; // Return the factorial value
}
public static int bino(int n, int r) {
int n_fact = factorial(n);
int r_fact = factorial(r);
int n_r_fact = factorial(n - r);
int bino = n_fact / r_fact * (n_r_fact);
return bino;
}
// Function overloading example
// function to call int multply for 2 parameters of int type
public static int multiply(int a, int b) {
int product = a * b; // Return the product of a and b
return product;
}
// function to call int multply for 3 parameters of int type
public static int multiply(int a, int b, int c) {
int product = a * b * c; // Return the product of a, b, and c
return product;
}
// function to call int multply for 2 parameters of float type
public static float multiply(float a, float b) {
float product = a * b; // Return the product of a and b
return product;
}
public static boolean isPrime(int n) {
if (n == 2) {
return true;
}
for (int i = 2; i <= n - 1; i++) {
if (n % i == 0) {
return false;
}
}
return true; // Return true only if n is prime
}
public static boolean OptisPrime(int n) {
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void PrintPrime(int n) {
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
public static void BinToDec(int binNum) {
int myNum = binNum;
int pow = 0;
int decNum = 0;
while (binNum > 0) {
int lastDig = binNum % 10;
decNum = decNum + (lastDig * (int) Math.pow(2, pow));
pow++;
binNum = binNum / 10;
}
System.out.print("Decimal of " + myNum + " is " + decNum);
}
public static void DecToBin(int Dec) {
int myNum = Dec;
int pow = 0;
int Bin = 0;
while (Dec > 0) {
int rem = Dec % 2;
Bin = Bin + (rem * (int) Math.pow(10, pow));
Dec = Dec / 2;
pow++;
}
System.out.print("Binary of " + myNum + " is " + Bin);
}
public static void main(String args[]) {
World(); //function call
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a=sc.nextInt(); //taking input from user
System.out.print("Enter second number: ");
int b=sc.nextInt(); //taking input from user
sc.close(); // Closing the scanner to prevent resource leak
int Sum = sum2(a,b); //arguments
System.out.println("The sum of " + a + " and " + b + " is: " + Sum );
//swap -value exchange
int A = 10, B = 20;
swap(A, B); // Call the swap function with a and b as arguments
//multiply
int product = prod(a, b);
System.out.println("The product of " + a + " and " + b + " is: " + product);
// Print the product of a and b
//factorial
int N = 4; // Example value for factorial calculation
int fact = factorial(N); // Calculate the factorial of n
System.out.println("The factorial of " + N + " is: " + fact); // Print the factorial result
//binomial coefficient
int n = 5; // Example value for n in binomial coefficient calculation
int r = 2; // Example value for r in binomial coefficient calculation
System.out.println("The binomial coefficient =" + bino(n, r)); // Print the binomial coefficient result
//function overloading
int x = 2, y = 3, z = 4;
System.out.println("The product of " + x + " and " + y + " is: " +
multiply(x, y)); // Call the multiply function with two arguments
System.out.println("The product of " + x + ", " + y + ", and " + z + " is: " + multiply(x, y, z)); // Call the multiply function with three arguments
float p = 2.5f, q = 3.5f;
System.out.println("The product of " + p + " and " + q + " is: " +
multiply(p, q));
System.out.println("Is prime?: " + isPrime(6));
System.out.println("Is prime?: " + OptisPrime(6));
PrintPrime(20);
BinToDec(101);
DecToBin(2);
}
}