Java Program to Check Prime Number

Write a Java program to check prime number. Given an input integer, We have to write an efficient code to check whether a number is prime or not.

Before writing a program, let’s quickly understand what is a prime number.

Prime Number

A prime number is a number that is greater than 1 and it has no positive divisors other than 1 and itself.

For example – 3, 13, 7  is a prime number, as it’s divisible by 1 and itself. Similarly, 29, 19 etc. are also prime numbers.

6 is not a prime number as it’s divisible by 1, 2, 3 and 6.

 2 is the only even prime number.

 

How to check whether a number is prime or not in Java

Find Common Elements in Two Arrays – Intersection of two Arrays

Given two sorted arrays, Write a code to find common elements in two arrays. In other words, we have to write a code to find the intersection of two arrays.

Find Common Elements in Two Arrays

Let’s assume following two sorted arrays arr1[] and arr2[].

int arr1[] = {2,  3,  4,  5,  6};
int arr2[] = {4,  6,  7,  8,  9};

So the common elements in these two arrays is 4 and 6.

Generate Prime Numbers between 1 to N – Sieve of Eratosthenes

Write an efficient program to generate prime numbers between 1 to N (Where N is 10, 100, 1000 etc). This question can also be asked like this, Generate prime numbers between 1 to 100 or 1 to 10 etc.

Suppose the value of N is 10, So the prime numbers between 1 to 10 is 2, 3, 5, 7.  Now let’s learn the most efficient algorithm (Sieve of Eratosthenes) to generate Prime Numbers between 1 to N. Before solving this program, let’s understand what is a prime number.

Find the Second Largest Number in an Array

Write a program to find the second largest number in an array. Given an unsorted array, we have to write a code to find the second largest number in an array. Also, we need to focus on time complexity. As the time complexity of an algorithm is very important in terms of an interview.

For example – Let’s take an array.

Input – arr[] = { 4, 1, 5, 2, 8, 0, 3, 55}

Output – 8

The second largest element in this array is 8.  Think for a moment, how do you find the second largest number in an array?  There are multiple ways to solve this problem. Which approach you prefer and why?

In this tutorial, we cover following things –

  • How to find the second largest number in an array using sorting.
  • C program to find the second largest number in an array.
  • C++ program to find the second largest number in an array.
  • PHP code to find the second largest number in an array.

Top Five Websites to Learn Programming Online

Top Five Websites to Learn Programming Online. There is no age for learning and when it comes to learn programming. The online resources are best since you get to know the latest stuff and tricks. Another important reason why you would like learning programming online is that you won’t need any teacher as such and you will in turn save money. So basically which websites are best to learn programming online? We assimilated the 5 best websites from which you can easily learn programming/coding and achieve your purpose.

Multiply Two Numbers without using * (Multiplication Operator)

Write a C program to multiply two numbers without using * multiplication operator.

Given two numbers, both numbers are positive.  Write a program to multiply two numbers without using * multiplication operator.  Think for a moment how to solve this problem .

You can use multiple approaches to find the solution of a problem.

C Program to Multiply Two Numbers without using *

C Program to Multiply Two Numbers without using *

C Program to Reverse a Linked List using Recursion

Write a c program to reverse a linked list using recursion. Given a singly linked list, we have to write a code to reverse a linked list using recursion. This question is very important in terms of interviews.

In my previous post, I have explained C program to reverse a singly linked list. In this post, We are going to write a code to reverse a linked list using recursion.  If you are not familiar with the concept of recursion then check my previous tutorial on recursion vs iteration.

The time complexity to reverse a linked list using recursion is O(n). 

Find Missing number in Array – Programming Problem

How to find missing number in array of 1 to n. This question is generally asked in technical interviews. The problem statement is something like that. Given an array of numbers from 1 to 100. The array is sorted. One number is missing from an array. You have to find the missing number from an array. Also there is no duplicates in an array.

Swap Two Numbers using Call by Reference in C

How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value. In this post let’s understand what is call by reference and how to swap two numbers using call by reference method.