Posts

Showing posts with the label Java-Program

Java program to find common elements between two arrays in java

Image
This is again a very useful and frequently used in project and also very popular in core Java written interview question , not only in core Java but yes finding common elements between array is in any language always on demand. We know that, array is something in any programming language which is really a powerful and day by day used in project. I think without array no project will complete, almost every project has array.. Lets compare two array to find the common elements, of course you can find out common elements with multiple ways and few of them are here listed in below program. We have two array something like, int [] array1 = [1,2,3,4,5,6]; int [] array2 = [8,1,3,9,4,5]; Expected output: 1,3,4,5 package com.javamakeuse.poc; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * * @author javamakeuse * */ public class ArrayCompare { public static void compareArray(Integer[] array1, Integer[] array2) { for (int i = 0; i < arra...

Java program to check even or odd number

Image
Very basic Java coding practice or Java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in core Java written interview questions , although this question is not as demanded as how to balanced the parentheses using Java program. How to check a number is even or odd? package com.javamakeuse.poc; import java.util.Scanner; /** * * @author javamakeuse * */ public class EvenOrOdd { public static void evenOrOdd() { System.out.println("Enter an Integer number:"); try (Scanner in = new Scanner(System.in)) { int num = in.nextInt(); // if input number is divisible by 2 if (num % 2 == 0) { System.out.println("Entered number is even"); } else { System.out.println("Entered number is odd"); } } } public static void main(String[] args) { evenO...

Java program to find duplicate elements in an array

Image
Ok, sometimes while working, we face this problem to find the duplicate elements from an array, and also sometime interviewer just write on paper and asking you to write a program or code logic to find the duplicate elements from a given array and expecting some better solution from you or at-least expecting more than one solutions to discuss about the pros and cons of those. Of-course you can find duplicate elements from an array with number of different approach, but finding the better one is of-course a tedious task, to find better one you need good understating of time and space complexity and the parameters to calculate the time and space. Like we have seen a solution of balancing parentheses with o(n) time at here . You can find duplicate elements from an array at-least via two different ways but not only the limited and those are. Brute force method(O(n^2) Set technique(O(n)) Brute force: Brute force method also know as exhaustive search and easy to implement, it com...

Java program to check string palindrome

Image
Nowadays in any interview beat experienced or fresher you will face at least 2-3 coding interview question as part of first round of technical written interview . And to checking string is palindrome or not is one of the popular coding interview question like balancing a parentheses or finding factorial of given number . So i would suggest everyone to do practice on such coding questions. Although, I have separately posted a complete set-up of Java interview questions here and in this post going to show you a simple Java program to check if string is palindrome or not. Code to check if string is palindrome or not package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class Palindrome { public static boolean isPalindrome(String s) { int n = s.length(); for (int i = 0; i < (n / 2); ++i) { if (s.charAt(i) != s.charAt(n - i - 1)) { return false; } } return true; } public static boolean isPalindrome2(String str) { if (str.equals(new Stri...

Java program to print vowels in a string

Image
Very common java coding interview question for freshers. You will face this question while appearing in campus interview, this type of questions in first round of technical written interview. This question is also known as homework question for some of you. In my previous post, i had shared how to check if a string contains unique characters or not at here . And also here is the list of written interview questions . Nowadays only theory is not enough for even experienced candidates almost every organizations has come up with their set of predefined written questions to check the logical ability and coding standards of candidates. So be ready with some basic practical questions.. Printing vowels package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class Vowel { public static void vowel(String str) { String lowerCase = str.toLowerCase(); for (int i = 0; i < lowerCase.length(); i++) { if ((lowerCase.charAt(i) == 'a') || (lowerCase.charAt(i) =...

Java program to check string has all unique character

Image
This question is very popular in the first round of technical interview for the freshers and also it's kind of homework activity. The coding practice is necessary to be logical on the logical questions like how to balanced parentheses and find the second largest element from an array . My suggestion for freshers to do always practice on such question like calculating Fibonacci series , finding factorial of given number and all. In this post, going to show you some simple approach to check if string contains duplicate characters. package com.javamakeuse.poc; import java.util.HashSet; /** * * @author javamakeuse * */ public class UniqueChars { public static boolean isUnique(String str) { String lowerCase = str.toLowerCase(); HashSet<Character> charSet = new HashSet<Character>(lowerCase.length()); for (int i = 0; i < lowerCase.length(); i++) { if (!charSet.add(lowerCase.charAt(i))) { return false; } } return true; } public static v...

Java program to reverse a string

Image
One of the popular interview question, and most of the time interviewer asked this question while first round of technical interview specially for freshers. Interviewer ask you to check your logical ability, once you provide the solution for this question, interview start asking you to provide some other way to reversing a string and start discussing why this approach is good and why other not and so on.. Although, I had already shared a list of written Java interview questions here and in this article going to show you how to reverse a string with multiple options. Reverse a String Example, Input: gnirts a esrever ot woh Expected Output: how to reverse a string Input: esuekamavaj Expected Output: javamakeuse package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class ReverseString { // using built in function public static String reverseString(String str) { return new StringBuilder(str).reverse().toString(); } // using character public stat...

Java program to add two numbers

Image
Very basic java coding practice or java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in core Java written interview questions , although this question is not as demanded as how to balanced the parentheses using Java program. package com.javamakeuse.poc; import java.util.Scanner; /** * * @author javamakeuse * */ public class Number { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.println("Enter first number "); int a = scanner.nextInt(); System.out.println("Enter second number"); int b = scanner.nextInt(); int c = a + b; System.out.printf("sum of %d and %d is %d", a, b, c); } } } OUTPUT : Enter first number 4 Enter second number 4 sum of 4 and 4 is 8

Java program to find area of circle

Image
While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, adding two numbers and all. And sometimes this type of questions also asked in core Java written interview questions , although this question is not as demanded as how to balanced the parentheses using Java program. Calculating Area of Circle Formula to calculate area of circle: area = PI * radius * radius package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } // area = PI * radius * radius public double printArea() { return Math.PI * radius * radius; } public static void main(String[] args) { Circle circle = new Circle(); circle.setRadius(12); System.out.println("Area of circle is => " + circle.printArea()); } } OUTPUT : Area of circle is =...

Java program to find sum of digits

Image
Very basic coding practice and core Java written interview questions, sometimes faculty gives this question as homework to their students to check the basic understanding Java syntax and basic calculation logic similar to finding a factorial of a given number and printing Fibonacci series . In this post, going to show very simple program to calculate the sum of given digits or say sum of given array elements. package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class SumOfDigits { // printing sum of digits from string public static void printSumOfDigits(String digits) { int sum = 0; for (String digit : digits.split(",")) { sum = sum + Integer.parseInt(digit); } System.out.println("Sum of digits (" + digits + ") is = " + sum); } // printing sum of array elements public static void printSumOfArrayEle(int[] numArray) { int sum = 0; for (int num : numArray) { sum = sum + num; } System.out.println(...

Java program to convert binary to decimal

Image
Java coding practice and sometimes asked in core Java written interview question . For freshers it's important to do practice on such coding exercise like how to balanced parentheses and finding factorial of a given number . In this article, i'm going to show two simple way to convert binary values into decimal numbers, in previous post, I had shared the just op positive how to convert decimal to binary here . package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class BinaryToDecimal { // converting binary to decimal via parseInt method public static void binaryToDecimal(String binary) { System.out.println(Integer.parseInt(binary, 2)); } // converting binary to decimal via custom implementation public static void binaryToDecimal2(String binary) { double j = 0; for (int i = 0; i < binary.length(); i++) { if (binary.charAt(i) == '1') { j = j + Math.pow(2, binary.length() - 1 - i); } } System.out.println((int) j); ...

Java program to convert decimal to binary

Image
Java programming interview question, specially for freshers, to check the candidates logical analysis and basic knowledge of coding. Some of the popular coding interview questions like how to balanced parentheses and finding second largest numbers from an array . In this post, I'm going to show you a simple Java code to convert decimal to binary. Converting decimal to binary in Java Using built in function called as toBinaryString() and Your own calculation to compute binary. package com.javamakeuse.poc; /** * * @author javamakeuse * */ public class DecimalToBinary { // converting decimal to binary using toBinaryString() public static void convertDecimalToBinary(int num) { System.out.println("Binary form of " + num + " is = " + Integer.toBinaryString(num)); } // converting decimal to binary via calculating reminder values public static void convertDecimalToBinary2(int num) { if (num == 0) { System.out.println(0); } else { Stri...

Java program to print prime numbers

Image
Java programming interview question, specially for freshers, to check the candidates logical analysis and basic knowledge of coding. Some of the popular coding interview questions like how to balanced parentheses and finding second largest numbers from an array . In this post, I'm going to show you a simple Java code to print prime numbers. What is Prime number? A prime number is a natural number greater than 1, that has no any positive divisors other than 1 and itself. For example 2,3,5,7,11,13,17,19,23,29,31.... package com.javamakeuse.poc; public class PrimeNumbers { // printing prime numbers till given number public static void printPrimeNumbers(int number) { int count; for (int i = 1; i <= number; i++) { count = 0; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { count++; break; } } if (count == 0) { System.out.println(i + " is a Prime number"); } } } public static void main(String[] args) { printP...

Best Fibonacci Program Using Java

Image
One of the popular Java coding interview question, this question is very common and almost all the companies ask this type of question specially in campus selection, this type of question is used to check the candidates logical analysis capacity and basic understanding of programming. In earlier post, I had shared a list of Java written interview question and in this article going to show you different way to write Fibonacci program. What is Fibonacci Series? In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones."copied from the wiki Example, 0,1,1,2,3,5,8,13,21,34,55,89... package com.javamakeuse.poc; import java.math.BigInteger; public class Fibonacci { public static void main(String[] args) { printFibonacci(10); } public static void printFibonacci(int n) { BigInteger a = BigInteger.ZERO; ...

Send Mail Using Gmail smtp in Java

Image
In this tutorial, I am going to share you a simple Java program to send mail using gmail server. If your account is more secured using second level of security with gmail account than to send email using application/program you have to be disabled the second level security. While sending email with below given java program if you are getting javax.mail.AuthenticationFailedException exception that means still your gmail account is more secured to disable/turn off you can follow this link, https://www.google.com/settings/security/lesssecureapps dependency for javax.mail api <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> SendMailExample.java package com.javamakeuse.dmd; import java.util.Properties; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress...

Hello World Program in Java

Image
To learn any programming language we start with Hello World program. After completion of my Hibernate tutorial series now planing to start series of java tutorial, and in this tutorial, I will show you how to create your first hello world program in java. I am using eclipse to create this basic program, you just need to follow the below steps. Open your IDE Go to File->New->Java Project(provide the project name) Right click on created project->New->Class(give the name of class) It will open an editor, where you can write your code. Below is the complete Hello World program. HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!!!"); } } To run, right click on the class and select run as java application or click on run symbol on the menu bar. Here , I have explained each part of the main method.

Java Program to find factorial of a number

Image
We have previously listed core java written interview question s. Sometimes interviewer asked you to write a program to find the factorial value of a given number. So In this post, I am simply creating a program to calculate the factorial of given number using a recursion function, about recursion function we separately post a tutorial. FactorialExample.java package com.javamakeuse.poc; public class FactorialExample { // recursion method to calculate factorial // ex. !5 = 5*4*3*2*1 public static int findFactorial(int num) { if (num < 1) { throw new IllegalArgumentException("Not a valid number"); } else if (num == 1) { return 1; } else { return num * findFactorial(num - 1); } } public static void main(String[] args) { System.out.println("Factorial of given number is - " + findFactorial(5)); } } OUT PUT: Factorial of given number is - 120

Java program to find second largest number in an array

Image
Java programing interview question to check your logic like how to print repeated numbers in an array here and how to balanced your parentheses . In my previous post, I have shared small list of java 7 features with practical example here and in this post going to show you simple program to find the second largest number in an array using two different approach one is traditional way and another one is using one of the collection api. ArrayUtils .java package com.javamakeuse.poc; import java.util.Set; import java.util.TreeSet; public class ArrayUtils { // without using collection framework public static int getSecondLargestNum(int[] numbers) { int largestNum = numbers[0]; int secondLargest = numbers[0]; for (int i = 0; i < numbers.length; i++) { if (numbers[i] > largestNum) { secondLargest = largestNum; largestNum = numbers[i]; } else if (numbers[i] > secondLargest) { secondLargest = numbers[i]; } } return secondLargest; } // usin...