java interview Questions
Відкрити в Telegram
java interview questions for beginners Any promotion for rajp6133@gmail.com
Показати більшеКраїна не вказанаТехнології та додатки41 622
1 732
Підписники
Немає даних24 години
Немає даних7 днів
+8230 день
Триває завантаження даних...
Схожі канали
Хмара тегів
Немає даних
Виникли проблеми? Будь ласка, оновіть сторінку або зверніться до нашого support-менеджера.
Вхідні та вихідні згадування
---
---
---
---
---
---
Залучення підписників
грудень '24
грудень '24
+164
в 0 каналах
листопад '240
в 0 каналах
Get PRO
жовтень '24
+106
в 0 каналах
Get PRO
вересень '24
+229
в 0 каналах
Get PRO
серпень '24
+152
в 0 каналах
Get PRO
липень '24
+292
в 0 каналах
Get PRO
червень '24
+256
в 0 каналах
Get PRO
травень '24
+287
в 0 каналах
Get PRO
квітень '24
+240
в 0 каналах
Get PRO
березень '24
+160
в 0 каналах
Get PRO
лютий '24
+268
в 0 каналах
Get PRO
січень '24
+155
в 0 каналах
Get PRO
грудень '23
+120
в 0 каналах
Get PRO
листопад '23
+133
в 0 каналах
Get PRO
жовтень '23
+130
в 0 каналах
Get PRO
вересень '23
+86
в 0 каналах
Get PRO
серпень '23
+65
в 0 каналах
Get PRO
липень '23
+77
в 0 каналах
Get PRO
червень '23
+60
в 0 каналах
Get PRO
травень '23
+54
в 0 каналах
Get PRO
квітень '23
+42
в 0 каналах
Get PRO
березень '23
+68
в 0 каналах
Get PRO
лютий '23
+1 080
в 0 каналах
| Дата | Залучення підписників | Згадування | Канали | |
| 13 грудня | +1 | |||
| 12 грудня | +2 | |||
| 11 грудня | +5 | |||
| 10 грудня | +6 |
Дописи каналу
What is the difference between Collection and Collections?
The differences between the Collection and Collections are given below.
The Collection is an interface whereas Collections is a class.
The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However, Collections class is to sort and synchronize the collection elements.
The Collection interface provides the methods that can be used for data structure whereas Collections class provides the static methods which can be used for various operation on a collection.
| 2 | What is the difference between HashMap and TreeMap?
The differences between the HashMap and TreeMap are given below.
HashMap maintains no order, but TreeMap maintains ascending order.
HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.
HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have multiple null values. | 321 |
| 3 | What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.
HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but HashMap needs to convert into Set to be iterated.
HashSet implements Set interface whereas HashMap implements the Map interface
HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.
HashSet contains the only single number of null value whereas HashMap can hold a single null key with n number of null values. | 297 |
| 4 | What is the difference between Set and Map?
The differences between the Set and Map are given below.
Set contains values only whereas Map contains key and values both.
Set contains unique values whereas Map can contain unique Keys with duplicate values.
Set holds a single number of null value whereas Map can include a single null key with n number of null values. | 286 |
| 5 | What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
HashSet maintains no order whereas TreeSet maintains ascending order.
HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
HashSet performs faster than TreeSet.
HashSet is backed by HashMap whereas TreeSet is backed by TreeMap. | 283 |
| 6 | What are the functions of the JDBC Connection interface?
The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that return the instance of Statement, PreparedStatement, CallableStatement, and DatabaseMetaData. | 383 |
| 7 | What is the role of the JDBC DriverManager class?
The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver(). | 375 |
| 8 | How can we set null value in JDBC PreparedStatement?
By using setNull() method of PreparedStatement interface, we can set the null value to an index. The syntax of the method is given below.
void setNull(int parameterIndex, int sqlType) throws SQLException
| 373 |
| 9 | For more information visit
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html | 353 |
| 10 | What is the return type of Class.forName() method?
The Class.forName() method returns the object of java.lang.Class object | 365 |
| 11 | What are the JDBC statements?
In JDBC, Statements are used to send SQL commands to the database and receive data from the database. There are various methods provided by JDBC statements such as execute(), executeUpdate(), executeQuery, etc. which helps you to interact with the database. | 358 |
| 12 | What is JDBC?
JDBC is a Java API that is used to connect and execute the query to the database. JDBC API uses JDBC drivers to connect to the database. JDBC API can be used to access tabular data stored into any relational database. | 375 |
| 13 | What do you understand by copy constructor in Java?
There is no copy constructor in java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
In this example, we are going to copy the values of one object into another using java constructor.
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html | 363 |
| 14 | rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } //STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing can be done try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main } // end Example
On executing the above code, it will establish the connection to the database and retrieve the data present in the database.
Connecting to database... Creating statement... ID: 100, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 28, First: Sumit, Last: Mittal Goodbye!
7. Write a Java Program to find the Transpose of a given Matrix.
Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i].
package Edureka1; public class Transpose { static final int N = 4; // This function stores transpose // of A[][] in B[][] static void transpose(int A[][], int B[][]) { int i, j; for (i = 0; i< N; i++) for (j = 0; j <N; j++) B[i][j] = A[j][i]; } public static void main (String[] args) { int A[][] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}}; int B[][] = new int[N][N], i, j; transpose(A, B); System.out.print("Result matrix is n"); for (i = 0; i<N; i++) { for (j = 0; j<N; j++) System.out.print(B[i][j] + " "); System.out.print("n"); } } } | 1 |
| 15 | package Edureka1; import java.util.HashMap; import java.util.Map; public class Hashmap { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<>(); print(map); map.put("abc", 10); map.put("mno", 30); map.put("xyz", 20); System.out.println("Size of map is" + map.size()); print(map); if (map.containsKey("abc")) { Integer a = map.get("abc"); System.out.println("value for key \"abc\" is:- " + a); } map.clear(); print(map); } public static void print(Map<String, Integer> map) { if (map.isEmpty()) { System.out.println("map is empty"); } else { System.out.println(map); } } }
On executing the HashMap program, output goes like this:
map is empty Size of map is:- 3 {abc=10, xyz=20, mno=30} value for key "abc" is:- 10 map is empty
5. Write a Java program to print the nodes present in the Circular LinkedList
It follows the first thing first approach. Here, the node is an element of the list, and it has two parts that are, data and next. Data represents the data stored in the node and next is the pointer that will point to the next node. let’s now understand its implementation.
package Edureka1; public class CircularlinkedList { //Represents the node of list. public class Node{ int data; Node next; public Node(int data) { this.data = data; } } //Declaring head and tail pointer as null. public Node head = null; public Node tail = null; //This function will add the new node at the end of the list. public void add(int data){ //Create new node Node newNode = new Node(data); //Checks if the list is empty. if(head == null) { //If list is empty, both head and tail would point to new node. head = newNode; tail = newNode; newNode.next = head; } else { //tail will point to new node. tail.next = newNode; //New node will become new tail. tail = newNode; //Since, it is circular linked list tail will point to head. tail.next = head; } } //Displays all the nodes in the list public void display() { Node current = head; if(head == null) { System.out.println("List is empty"); } else { System.out.println("Nodes of the circular linked list: "); do{ //Prints each node by incrementing pointer. System.out.print(" "+ current.data); current = current.next; }while(current != head); System.out.println(); } } public static void main(String[] args) { CircularlinkedList cl = new CircularlinkedList(); //Adds data to the list cl.add(1); cl.add(2); cl.add(3); cl.add(4); //Displays all the nodes present in the list cl.display(); } }
On executing this program, the output will be as shown below:
Nodes of the circular linked list: 1 2 3 4
6. Write a Java program to connect to a SQL DataBase.
JDBC is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases. This application program interface lets you encode the access request statements, in Structured Query Language (SQL). They are then passed to the program that manages the database. It mainly involves opening a connection, creating a SQL Database, executing SQL queries and then arriving at the output. Let’s see an example code to create a database and establish a connection and run the queries.
package Edureka1; import java.sql.*; import java.sql.DriverManager; public class Example { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/emp"; // Database credentials static final String USER = "root"; static final String PASS = "edureka"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.cj.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,"root","edureka"); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //STEP 5: Extract data from result set while(rs.next()){ //Retrieve by column name int id = | 138 |
| 16 | = 2*i + 1 int r = 2*i + 2; // right = 2*i + 2 // If left child is larger than root if (l< n && arr[l] >arr[largest]) largest = l; // If right child is larger than largest so far if (r < n && arr[r] > arr[largest]) largest = r; // If largest is not root if (largest != i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } /* A utility function to print array of size n */ static void printArray(int arr[]) { int n = arr.length; for (int i=0; i<n; ++i) System.out.print(arr[i]+" "); System.out.println(); } // Driver program public static void main(String args[]) { int arr[] = {12, 11, 13, 5, 6, 7}; int n = arr.length; HeapSort ob = new HeapSort(); ob.sort(arr); System.out.println("Sorted array is"); printArray(arr); } }
Output:
5,6,7,11,12,13
3. Write a Java program to remove elements from an ArrayList
ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size. In the below program, I am first inserting elements into the ArrayList and then deleting the elements from the list based on the specification. Let’s understand the code.
package Edureka1; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class ArrayListExample { public static void main(String[] args) { List<String> programmingLanguages = new ArrayList<>(); programmingLanguages.add("C"); programmingLanguages.add("C++"); programmingLanguages.add("Java"); programmingLanguages.add("Kotlin"); programmingLanguages.add("Python"); programmingLanguages.add("Perl"); programmingLanguages.add("Ruby"); System.out.println("Initial List: " + programmingLanguages); // Remove the element at index 5 programmingLanguages.remove(5); System.out.println("After remove(5): " + programmingLanguages); // Remove the first occurrence of the given element from the ArrayList // (The remove() method returns false if the element does not exist in the ArrayList) boolean isRemoved = programmingLanguages.remove("Kotlin"); System.out.println("After remove(\"Kotlin\"): " + programmingLanguages); // Remove all the elements that exist in a given collection List<String> scriptingLanguages = new ArrayList<>(); scriptingLanguages.add("Python"); scriptingLanguages.add("Ruby"); scriptingLanguages.add("Perl"); programmingLanguages.removeAll(scriptingLanguages); System.out.println("After removeAll(scriptingLanguages): " + programmingLanguages); // Remove all the elements that satisfy the given predicate programmingLanguages.removeIf(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("C"); } }); System.out.println("After Removing all elements that start with \"C\": " + programmingLanguages); // Remove all elements from the ArrayList programmingLanguages.clear(); System.out.println("After clear(): " + programmingLanguages); } }
Output on execution of the program looks like:
Initial List: [C, C++, Java, Kotlin, Python, Perl, Ruby] After remove(5): [C, C++, Java, Kotlin, Python, Ruby] After remove("Kotlin"): [C, C++, Java, Python, Ruby] After removeAll(scriptingLanguages): [C, C++, Java] After Removing all elements that start with "C": [Java] After clear(): []
4. Write a program in Java to implement HashMap.
HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key). Let’s see how to implement HashMap logic in Java with the help of below program. | 103 |
| 17 | the number of rows: 5 * * * * *** * * *** *
This will be the output of Diamond-Shaped Pattern program. Now let’s move further and see what’s next.
7. Write a Java Program to reverse the letters present in the given String.
This Java program reverses letters present in the string entered by a user. For example, Hello People will be termed as olleH elpoeP. Let’s implement the same using Java.
package Edureka; public class stringreverse { public static void main(String[] args) { // TODO Auto-generated method stub String str = "Welcome To Edureka"; String[] strArray = str.split(" "); for (String temp: strArray){ System.out.println(temp); } for(int i=0; i<3; i++){ char[] s1 = strArray[i].toCharArray(); for (int j = s1.length-1; j>=0; j--) {System.out.print(s1[j]);} System.out.print(" "); } } }
The output of the above program will be as shown below:
Welcome To Edureka emocleW oT akerudE
8. Write a Java Program to check whether the given array is Mirror Inverse or not.
An array is called mirror–inverse if its inverse is equal to itself. Let’s now write a program and check whether the given array is mirror inverse or not.
package Edureka; //Java implementation of the approach public class MirrorInverse { // Function that returns true if // the array is mirror-inverse static boolean isMirrorInverse(int arr[]) { for (int i = 0; i<arr.length; i++) { // If condition fails for any element if (arr[arr[i]] != i) return false; } // Given array is mirror-inverse return true; } public static void main(String[] args) { int arr[] = { 1, 2, 3, 0 }; if (isMirrorInverse(arr)) System.out.println("Yes"); else System.out.println("No"); } }
Output: No
// If the given array was {3,4,2,0,1} then it would have printed yes as the output because the array is mirror inverse.
What are some Advance Java Programs?
1. Write a Java program to implement a Binary Search Algorithm.
It is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. Let’s now see how to implement a binary search algorithm.
package Edureka1; public class BinarySearch { // Java implementation of recursive Binary Search // Returns index of x if it is present in arr[l.. // r], else return -1 int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the // middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] >x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not present // in array return -1; } public static void main(String args[]) { BinarySearch ob = new BinarySearch(); int arr[] = { 2, 3, 4, 10, 40 }; int n = arr.length; int x = 40; int result = ob.binarySearch(arr, 0, n - 1, x); if (result == -1) System.out.println("Element not present"); else System.out.println("Element found at index " + result); } }
On executing the above program, it will locate the element present at the particular index
Element found at index 4
2. Write a Java program to implement HeapSort Algorithm.
Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. Then repeat the same process for the remaining element. Let’s write the program and understand its working.
package Edureka1; public class HeapSort { public void sort(int arr[]) { int n = arr.length; // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); // One by one extract an element from heap for (int i=n-1; i>=0; i--) { // Move current root to end int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; // call max heapify on the reduced heap heapify(arr, i, 0); } } void heapify(int arr[], int n, int i) { int largest = i; // Initialize largest as root int l = 2*i + 1; // left | 74 |
| 18 | package Edureka; public class Fibonacci { public static void main(String[] args) { //initializing the constants int n = 100, t1 = 0, t2 = 1; System.out.print("Upto " + n + ": "); //while loop to calculate fibonacci series upto n numbers while (t1<= n) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; } } }
On executing the above code, the output looks like :
Upto 100: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 +
4. Write a Java program to find out whether the given String is Palindrome or not.
A palindrome is a number, string or a sequence which will be the same even after you reverse the order. For example, RACECAR, if spelled backward will be same as RACECAR.
package Edureka; import java.util.Scanner; public class Palindrome { static void checkPalindrome(String input) { //Assuming result to be true boolean res = true; int length = input.length(); //dividing the length of the string by 2 and comparing it. for(int i=0; i<= length/2; i++) { if(input.charAt(i) != input.charAt(length-i-1)) { res = false; break; } } System.out.println(input + " is palindrome = "+res); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your Statement: "); String str = sc.nextLine(); //function call checkPalindrome(str); } }
When you run the code, it will check whether the given string is a palindrome or not as shown below:
Enter your Statement: RACECAR RACECAR is palindrome = true Enter your Statement: EDUREKA EDUREKA is palindrome = false
5. Write a Java program to calculate Permutation and Combination of 2 numbers.
It is the different arrangements of a given number of elements taken one by one, or some, or all at a time. Let’s have a look at its implementation.
package Edureka; import java.util.Scanner; public class nprandncr { //calculating a factorial of a number public static int fact(int num) { int fact=1, i; for(i=1; i<=num; i++) { fact = fact*i; } return fact; } public static void main(String args[]) { int n, r; Scanner scan = new Scanner(System.in); System.out.print("Enter Value of n : "); n = scan.nextInt(); System.out.print("Enter Value of r : "); r = scan.nextInt(); // NCR and NPR of a number System.out.print("NCR = " +(fact(n)/(fact(n-r)*fact(r)))); System.out.print("nNPR = " +(fact(n)/(fact(n-r)))); } }
On executing the above code, the output looks like as shown below:
Enter Value of n : 5 Enter Value of r : 3 NCR = 10 NPR = 60
6. Write a program in Java to find out Alphabet and Diamond Pattern.
Here, you can use the for loop to print various patterns in Java. I will be implementing two different patterns in this article. First one will be Alphabet A pattern and the next one will be Diamond shaped pattern. Let’s now see the implementation of the alphabet A pattern.
package Edureka; import java.util.Scanner; public class PatternA { // Java program to print alphabet A pattern void display(int n) { // Outer for loop for number of lines for (int i = 0; i<=n; i++) { // Inner for loop for logic execution for (int j = 0; j<= n / 2; j++) { // prints two column lines if ((j == 0 j == n / 2) && i != 0 // print first line of alphabet i == 0 && j != n / 2 || // prints middle line i == n / 2) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); PatternA a = new PatternA(); a.display(7); } }Output:Diamond Pattern Program in Javapackage Edureka; import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int n, i, j, space = 1; System.out.print("Enter the number of rows: "); Scanner s = new Scanner(System.in); n = s.nextInt(); space = n - 1; for (j = 1; j<= n; j++) { for (i = 1; i<= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j<= n - 1; j++) { for (i = 1; i<= space; i++) { System.out.print(" "); } space++; for (i = 1; i<= 2 * (n - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } }Enter | 80 |
| 19 | Write a Java Program to find the Transpose of a given Matrix.


Java Programs for Practice: Know the Simple Java Programs for Beginners
edureka.co


Whatsapp

Linkedin

Twitter

Facebook

Reddit
Copy Link
Java is one of the most popular programming languages that is being widely used in the IT industry. It is simple, robust and helps us to reuse the code. In this article, let’s see some of the important programs to understand Java fundamentals.
Java Full Course | Java Tutorial for Beginners | Java Online Training | Edureka

This Edureka Java Full Course will help you in understanding the various fundamentals of Java Programming and also helps you to became a master in Advanced Java concepts.
Below is the list of programs that I will be covering in this article.
What are the basic Java programs?
Calculator Program in Java
Factorial Program using Recursion
Fibonacci Series Program
Palindrome Program in Java
Permutation and Combination Program
Pattern Programs in Java
String Reverse Program in Java
Mirror Inverse Program in Java
What are some Advanced Java Programs?
Binary Search Program in Java
HeapSort Program in Java
Removing Elements from ArrayList
HashMap Program in Java
Circular LinkedList Program in Java
Java DataBase Connectivity Program
Transpose of a Matrix Program
Let’s get started !
What are the basic Java programs?
1. Write a Java program to perform basic Calculator operations.
When you think about a calculator, operations like addition, subtraction, multiplication, and division comes into the mind. Let’s implement the basic calculator operations with the help of the below program.
package Edureka; import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); // nextDouble() reads the next double from the keyboard double first = reader.nextDouble(); double second = reader.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = reader.next().charAt(0); double result; //switch case for each of the operations switch(operator) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; // operator doesn't match any case constant (+, -, *, /) default: System.out.printf("Error! operator is not correct"); return; } //printing the result of the operations System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); } }
When you execute the above program, the output looks like as shown below:
Enter two numbers: 20 98 Enter an operator (+, -, *, /): / 20.0 / 98.0 = 0.2
2. Write a Java program to calculate a Factorial of a number.
Factorial of a number is the product of all the positive numbers less than or equal to the number. The factorial of a number n is denoted by n!
Now, let’s write a program and find factorial of a number using recursion.
package Edureka; import java.util.Scanner; public class Factorial { public static void main(String args[]){ //Scanner object for capturing the user input Scanner scanner = new Scanner(System.in); System.out.println("Enter the number:"); //Stored the entered value in variable int num = scanner.nextInt(); //Called the user defined function fact int factorial = fact(num); System.out.println("Factorial of entered number is: "+factorial); } static int fact(int n) { int output; if(n==1){ return 1; } //Recursion: Function calling itself!! output = fact(n-1)* n; return output; } }
On executing the above program, you will get factorial of a number as shown below:
Enter the number: 12 Factorial of entered number is: 47900160
3. Write a Java program to calculate Fibonacci Series up to n numbers.
It is a series in which the next term is the sum of the preceding two terms. For Example: 0 1 1 2 3 5 8 13……. Let’s write a Java program to calculate the Fibonacci series. | 92 |
| 20 | All this program is very important for freshers | 289 |
