Print if a number is prime or not (Input n from the user). [In this problem you will learn how to check if a number is prime or not]
Part-1, Part-2, Part-3, Part-4, Part-5, Part-6, Part-7, Part-8, Part-9.
A function is a block of code that performs a specific task. Why are functions used? a. 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. b. Functions make maintenance of code easy as we have to change at one place if we make future changes to the functionality. c. Functions make the code more readable and easy to understand.
The syntax for function declaration is : return-type function_name (parameter 1, parameter2, …… parameter n){ //function_body } return-type The return type of a function is the data type of the variable that that function returns.
For eg - If we write a function that adds 2 integers and returns their sum then the return type of this function will be ‘int’ as we will return a sum that is an integer value. When a function does not return any value, in that case the return type of the function is ‘void’. function_name It is the unique name of that function. It is always recommended to declare a function before it is used.
Parameters A function can take some parameters as inputs. These parameters are specified along with their data types. For eg- if we are writing a function to add 2 integers, the parameters would be passed like – int add (int num1, int num2)
Explain a code working Time, and code spaces layers in storage tank. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input.
Types of notations 1. O-notation: It is used to denote asymptotic upper bound. For a given function g(n), we denote it by O(g(n)). Pronounced as “big-oh of g of n”. It is also known as worst case time complexity as it denotes the upper bound in which the algorithm terminates. 2. Ω-notation: It is used to denote asymptotic lower bound. For a given function g(n), we denote it by Ω(g(n)). Pronounced as “big-omega of g of n”. It is also known as best case time complexity as it denotes the lower bound in which the algorithm terminates. 3. 𝚯-notation: It is used to denote the average time of a program.
Comparison of functions on the basis of time complexity
It follows the following order in case of time complexity:
O(n n) > O(n!) > O(n3) > O(n2) > O(n.log(n)) > O(n.log(log(n))) > O(n) > O(sqrt(n)) > O(log(n)) > O(1)
Note: Reverse is the order for better performance of a code with corresponding time complexity, i.e. a program with less time complexity is more efficient.
Space Complexity Space complexity of an algorithm quantifies the amount of time takenby a program to run as a function of length of the input. It is directly proportional to the largest memory your program acquires at any instance during run time. For example: int consumes 4 bytes of memory.
Arrays in Java are like a list of elements of the same type i.e. a list of integers, a list of
booleans etc.
a. Creating an Array (method 1) - with new keyword
int[] marks = new int[3];
marks[0] = 97;
marks[1] = 98;
marks[2] = 95;
b. Creating an Array (method 2)
int[] marks = {98, 97, 95};
ALWAYS REMEMBER : Java Strings are Immutable.
Declaration
String name = "Ganesh";
Taking Input
Scanner sc = new Scanner(System.in);
String name = sc.next();
Concatenation (Joining 2 strings)
String firstName = "Ganesh";
String secondName = "Jadhav";
String fullName = firstName + " " + secondName;
System.out.println(fullName);
Print length of a String
String firstName = "Ganesh";
String secondName = "Jadhav";
String fullName = firstName + " " + secondName;
System.out.println(fullName.length());
Access characters of a string
String firstName = "Ganesh";
String secondName = "Jadhav";
String fullName = firstName + " " + secondName;
for(int i=0; i<fullName.length(); i++) {
System.out.println(fullName.charAt(i));
}
- Join LinkedIn: https://www.linkedin.com/in/ganesh-jadhav-951213225/
- Join Twitter: https://twitter.com/ganeshhh24