-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07. Functions&Methods.java
More file actions
163 lines (136 loc) · 4.8 KB
/
07. Functions&Methods.java
File metadata and controls
163 lines (136 loc) · 4.8 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
/*
The syntax for function declaration is:
returnType functionName(type arg1, type arg2...) {
//operations
}
Why are functions used?
1. If some functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call it everywhere. This helps reduce code redundancy.
2. Functions make maintenance of code easy as we have to change at one place if we make future changes to the functionality.
3. Functions make the code more readable and easy to understand.
A function is a block of code that performs a specific task. It takes an input and gives output after an operation.
- returnType can be int, float, String etc.
- void -> no return.
- If a returnType of a function is void, it means that function returns nothing.
- functionName can be anything except Java Keywords.
- arg1, arg2 -> these are basically inputs.
- we can pass multiple (it can be different types too, like int, float, String etc.) arguments in a function.
The main function is a special function as the computer starts running the code from the beginning of the main function. Main function serves as the entry point for the program.
*/
/*1. Print a given name in a function.*/
import java.util.*;
public class Functions {
public static void printMyName(String name) {
System.out.println(name);
return;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
printMyName(name); //we called the function here
}
}
/*2. Make a function to add 2 numbers and return the sum.*/
import java.util.*;
public class SumOfTwo {
public static int sumOfTwo(int a, int b) {
int sum = a+b;
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first value, a = ");
int a = sc.nextInt();
System.out.print("Enter second value, b = ");
int b = sc.nextInt();
int sum = sumOfTwo(a, b);
System.out.print("Sum of the numbers = "+ sum);
}
}
/*3. Make a function to multiply 2 numbers and return the product.*/
import java.util.*;
public class MultiplyOfTwo {
public static int mulOfTwo(int a, int b) {
return a*b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first value, a = ");
int a = sc.nextInt();
System.out.print("Enter second value, b = ");
int b = sc.nextInt();
System.out.print("Product of the numbers = "+ mulOfTwo(a, b));
}
}
/*4. Find the factorial of a number.*/
import java.util.*;
public class Factorial {
public static int fact(int n) {
int factorial = 1;
for(int i=n; i>=1; i--) {
factorial = factorial * i;
}
return factorial;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
System.out.print("Factorial of the number is: " + fact(n));
}
}
/*5. Make a function to check if a number is prime or not.*/
import java.util.*;
public class PrimeNumber {
public static boolean isPrime(int num) {
if(num <= 1) {
return false;
}
for(int i=2; i<=num/2; i++) {
if((num % i) == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
if(isPrime(n)) {
System.out.println(n + " is a Prime Number.");
} else {
System.out.println(n + " is not a Prime Number.");
}
}
}
/*6. Make a function to check if a given number n is even or not.*/
import java.util.*;
public class EvenNumber {
public static void checkEven(int n) {
if(n % 2 == 0) {
System.out.println(n + " is an Even Number.");
} else {
System.out.println(n + " is not an Even Number.");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
checkEven(n);
}
}
/*7. Make a function to print the table of a given number n.*/
import java.util.*;
public class Table {
public static void tableOfNum(int n) {
for(int i=1; i<=10; i++) {
System.out.println(n + " x " + i + " = " + (n*i));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
tableOfNum(n);
}
}