Coding Questions IN JAVA
Coding Questions IN JAVA
JAVA
import java.util.Scanner;
int reverse = 0;
String s = "";
while(number != 0)
s = s + Integer.toString(pick_last);
System.out.print(s);
sc.close();
}
Write the code to find the Fibonacci series upto the
nth term.
C
JAVA
public class Main
{
public static void main (String[]args)
{
int nextTerm;
}
}
JAVA
import java.util.Scanner;
public class gcd_or_hcf
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from the user
System.out.print("Enter the first number : ");
int num1 = sc.nextInt();
//input from the user
System.out.print("Enter the second number : ");
int num2 = sc.nextInt();
int n = 1;
System.out.print("HCF of "+num1+" and "+num2+" is ");
if( num1 != num2)
{
while(n != 0)
{
//storing remainder
n = num1 % num2;
if(n != 0)
{
num1 = num2;
num2 = n;
}
}
//result
System.out.println(num2);
}
else
System.out.println("Wrong Input");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
JAVA
import java.util.Scanner;
public class perfect_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//declare a variable to store sum of factors
int sum = 0;
for(int i = 1 ; i < number ; i++)
{
if(number % i == 0)
sum = sum + i;
}
//comparing whether the sum is equal to the given number or not
if(sum == number)
System.out.println("Perfect Number");
else
System.out.println("Not an Perfect Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
JAVA
import java.util.Arrays;
import java.util.Scanner;
public class CheckIfTwoStringsAreAnagramAreNot {
static boolean isAnagram(String str1 , String str2) {
String s1 = str1.replaceAll("[\\s]", "");
String s2 = str2.replaceAll("[\\s]", "");
boolean status=true;
if(s1.length()!=s2.length())
status = false;
else {
char[] a1 = s1.toLowerCase().toCharArray();
char[] a2 = s2.toLowerCase().toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
status = Arrays.equals(a1, a2);
}
return status;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two String :");
String s1 = sc.next();
String s2 = sc.next();
boolean status = isAnagram(s1,s2);
if(status)
System.out.println(s1+" and "+s2+" are Anagram");
else
System.out.println(s1+" and "+s2+" are not Anagram");
}
}
JAVA
import java.util.Scanner;
JAVA
import java.util.Scanner;
JAVA
// Time Complexity : O(N^2)
// Space Complexity : O(1)
class Main
{
static void bubbleSort(int a[])
{
int len = a.length; // calculating the length of array
for (int i = 0; i < len-1; i++)
for (int j = 0; j < len-i-1; j++)
if (a[j] > a[j+1]) //comparing the pair of elements
{
// swapping a[j+1] and a[i]
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
System.out.println();
}
System.out.println("Sorted array");
JAVA
//Java Program for Merge Sort
class Main {
// this function display the array
public static void display(int[] arr, int size)
{
for(int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// main function of the program
public static void main(String[] args)
{
int[] a = {12, 8, 4, 14, 36, 64, 15, 72, 67, 84};
JAVA
// Leap year program in Java
// If the year satisfies either of the conditions, it's considered a leap year -
// 1. The year must be divisible by 400.
// 2. The year must be divisible by 4 but not 100.public class Main
{
public static void main (String[]args)
{
else
System.out.println (year + " is not a Leap Year");
}
}
JAVA
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the string");
String str = sc.next (); //Taking input as a string from the user
int freq[] = new int[256];
JAVA
//Replace Substring in a String Java code
import java.util.Scanner;
public class ReplaceASubstringInAString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String : ");
String s1 = sc.nextLine();
System.out.print("Enter the String to be replaced : ");
String oldString = sc.nextLine();
System.out.print("Enter the new String : ");
String newString =sc.nextLine();
class Main {
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int[] arr = { 100, 2, 70, 12 , 90};
// Function Call
changeArr(arr);
JAVA
class Main {
/*Function to left rotate arr[] of size n by d*/
static void leftRotate(int arr[], int d, int n) {
for (int i = 0; i < d; i++) leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n) {
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1];
arr[n - 1] = temp;
}
/* utility function to print an array */
static void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public
static void main(String[] args) {
// RotateArray rotate = new RotateArray();
int arr[] = {1, 2, 3, 4, 5};
leftRotate(arr, 2, 5);
printArray(arr, 5);
}
}
JAVA
import java.util.Arrays;
class Main
{
public static void countFreq(int arr[], int n)
{
boolean visited[] = new boolean[n];
Arrays.fill(visited, false);
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
if(count==1)
System.out.println(arr[i]);
}
// Driver code
public static void main(String []args)
{
int arr[] = new int[]{10, 30, 40, 20, 10, 20, 50, 10};
int n = arr.length;
countFreq(arr, n);
}
}
JAVA
import java.util.*;
class Main
{
// Function to check if n is palindrome
static boolean isPalindrome(int n)
{
// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
divisor *= 10;
while (n != 0) {
int x = n / divisor;
int y = n % 10;
// Driver program
public static void main(String []args)
{
int []A = { 121, 2322, 54545, 999990 };
int n = A.length;
JAVA
//Java program to find factorial of a number
import java.util.Scanner;
public class LearnCoding
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();
if(num >= 0)
{
System.out.println(num + " Factorial: " + getFact(num));
}
else
System.out.println("Negative Number: No Factorial");
}
if(num == 1 || num == 0)
return 1;
JAVA
public class Main
{
public static void main (String[]args)
{
int num = 407, len;
// check if Armstrong
if (armstrong (num, len))
System.out.println(num + " is armstrong");
else
System.out.println(num + " is armstrong");
JAVA
public class Main
{
public static void main (String[]args)
{
int n = 10;
int sum = getSum (n);
System.out.println (sum);
}
JAVA
public class Main
{
public static void main (String[]args)
{
int n = 10;
int sum = getSum (n);
System.out.println (sum);
}
JAVA
import java.util.Scanner;
public class Palindrome{
reader.close();
return input.equals(reverseOfString);
}
JAVA
//Java program to convert Binary number to decimal number
import java.util.Scanner;
public class Binary_To_Decimal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number : ");
int binary = sc.nextInt();
//Declaring variable to store decimal number
int decimal = 0;
//Declaring variable to use in power
int n = 0;
//writing logic for the conversion
while(binary > 0)
{
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
System.out.println("Decimal number : "+decimal);
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
JAVA
//JAVA Program to check whether the character entered by user is Vowel or Consonant.
import java.util.Scanner;
public class vowelorconsonant
{
//class declaration
public static void main(String[] args)
{
//main method declaration
Scanner sc=new Scanner(System.in); //scanner class object creation
System.out.println(" Vowel");
else if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) //condition for the
consonants
System.out.println(" Consonant");
else
System.out.println(" Not an Alphabet");
JAVA
//Java program to check whether a number is Automorphic number or not
import java.util.Scanner;
public class automorphic_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//Convert the number to string
String s1 = Integer.toString(number);
//Calculate the length
int l1 = s1.length();
int sq = number * number;
String s2 = Integer.toString(sq);
int l2 = s2.length();
//Create Substring
String s3 = s2.substring(l2-l1);
if(s1.equals(s3))
System.out.println("Automorphic Number");
else
System.out.println("Not an Automorphic Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
JAVA
//Java program to print ASCII values of a character
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
//scanner class object creation
Scanner sc=new Scanner(System.in);
JAVA
import java.util.Scanner;
class RemoveCharactersInAtringExceptAlphabets {
JAVA
//Fibonacci Series using Recursion
class fibonacci
{
static int fibo(int n)
{
if (n <= 1)
return n;
return fibo(n-1) + fibo(n-2);
}