Java Coding Interview Questions with Answers
import java.util.Scanner;
public class Prime1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
int number = sc.nextInt();
sc.close();
boolean flag = false;
for(int i=2;i<=number/2;i++){
if(number%i==0){
flag=true;
break;
}
}
if(number<2 || flag){
System.out.println("Not Prime");
}else if(!flag){
System.out.println("Prime Number");
}
}
}
Output Screen:
Enter the number:23
Prime Number
import java.util.Scanner;
import java.lang.Math;
public class Prime2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
int number = sc.nextInt();
sc.close();
boolean flag = false;
double sqrt = Math.sqrt(number);
if(Math.floor(sqrt)==sqrt){
flag=true;
}
for(int i=2;i<=(int)sqrt;i++){
if(number%i==0){
flag=true;
break;
}
}
if(number<2 || flag){
System.out.println("Not Prime");
}else if(!flag){
System.out.println("Prime Number");
}
}
}
Output Screen:
Enter the number:37
Prime Number
Third Approach*****************
import java.util.Scanner;
public class Prime3{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
int number = sc.nextInt();
sc.close();
int i;
for(i=2;i<=number/2;i++){
if(number%i==0){
break;
}
}
if(number<2 || i< (number/2)+1){
System.out.println("Not Prime");
}else if (i== (number/2)+1){
System.out.println("Prime Number");
}
}
}
Output Screen:
Enter the number:12
Not Prime
Java Code to exchange the row into column.
public class Row_Column {
public static void main(String args[]){
int arr[][]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("After Exchange");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
System.out.print(arr[j][i]+" ");
}
System.out.println();
}
}
}
Output Screen:
1 2 3
4 5 6
7 8 9
After Exchange
1 4 7
2 5 8
3 6 9
Java code to sort an array.
public class Sort_Array {
public static void main(String[] args) {
int arr[]={1,34,9,14,90};
for(int i:arr)System.out.print(i+" ");
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[i]){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println();
for(int a:arr)System.out.print(a+" ");
}
}
Output Screen:
1 34 9 14 90
1 9 14 34 90
Java code to print fibonacci series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("How many number you want:");
int count=sc.nextInt();
sc.close();
int x=0,y=1,z;
System.out.print(x+", "+y);
for(int i=2;i<count;i++){
z=x+y;
System.out.print(", "+z);
x=y;
y=z;
}
}
}
Output Screen:
How many number you want:10
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Java code to reverse a number.
import java.util.Scanner;
public class Reverse_Number {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int number = sc.nextInt();
sc.close();
int reverse=0;
while(number!=0){
int remainder = number%10;
reverse = reverse*10+remainder;
number = number/10;
}
System.out.println("Reverse number is: "+reverse);
}
}
Output Screen:
Enter the number: 123456
Reverse number is: 654321
Java code to calculate factorial of number.
public class Factorial {
public static void main(String[] args) {
int number = 6;
System.out.println("Factorial of "+number+" is: "+factorial(number));
}
static int factorial(int n){
if(n==0||n==1) return 1;
else return n*factorial(n-1);
}
}
Output Screen:
Factorial of 6 is: 720
Best casino site 2021 | All you need to know!
ReplyDeleteThe best casino 더킹카지노 site 2021 온카지노 How do 카지노 you get started at an online casino? You have to choose which casinos you can trust online, whether you want to play