30 Coding Java Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

 Swapping a number:

 public class SwapNum {


public static void main(String[] args) {

//using third temp. variable


int x = 10;
int y = 20;

System.out.println("Befor swapping " + x + " " + y);

int temp = x;
x = y;
y = temp;

System.out.println("After swapping " + x + " " + y);

}
}

 package PracticeQuestions;

public class SwapNum {


public static void main(String[] args) {

// using +, - operator

int x = 10;
int y = 20;

System.out.println("Befor swapping " + x + " " + y);

x = x+y; // x = 10+20 = 30
y = x-y; // y = 30-20 = 10;
x = x-y; // x = 30-10 = 20;

System.out.println("After swapping " + x + " " + y);

}
}

 Reverse a number:

package PracticeQuestions;

import java.util.Scanner;

public class ReverseNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");

int num = sc.nextInt();

int revNum = 0;
while(num!=0) {

revNum = revNum*10 + num%10; // 0*10 + 1234%10 -->


0+4n --> 4

num = num/10; // 1234/10--> 123

System.out.println(revNum);
}

 Reverse a String:

1. package PracticeQuestions;

public class ReverseString {


public static void main(String[] args) {

String str = "ABCD";


System.out.println("Before reverse a String : " + str);

String rev = "";

for(int i = str.length()-1; i>=0; i--) {

rev = rev + str.charAt(i);


}

System.out.println("After reverse a String : " +rev);


}
}

2. package PracticeQuestions;

public class ReverseString {


public static void main(String[] args) {

String str = "ABCD";


char[] ch = str.toCharArray();

System.out.println("Before reverse a String : " + str);

String rev = "";

for(int i = ch.length-1; i>=0; i--) {

rev = rev +ch[i];


}

System.out.println("After reverse a String : " +rev);


}
}
 how to check a number is palindrome number:

a palindrome number is same when its digits become reverse.


package PracticeQuestions;

import java.util.Scanner;

public class PalindromeNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");

int num = sc.nextInt();

int rev = 0;

while(num!=0) {

rev = rev*10+num%10;

num = num/10;
}

System.out.println(rev);

if(rev==num) {
System.out.println("Given number is a palindrome
number");
} else {
System.out.println("Given number is not a palindrome
number");

}
}
 how to check a String is palindrome number:

package PracticeQuestions;

import java.util.Scanner;

public class PalindromeStr {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter a String: ");

String str = sc.next();

System.out.println("Before reverse a String : " + str);

String rev = "";

for (int i = str.length() - 1; i >= 0; i--) {

rev = rev + str.charAt(i);


}

System.out.println("After reverse a String : " + rev);

if (rev.equals(str)) {
System.out.println("The Given String is palindrome");
} else {
System.out.println("The Given String is not
palindrome");

}
}

}
Count Number Of Digits in A Number

package PracticeQuestions;

import java.util.Scanner;

public class CountDigits {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int num = sc.nextInt();

int count = 0;
while(num!=0) {

num = num/10;

count= count+1;
}

System.out.println("count of digits: " + count);


}
}

Count Number Of EVEN & ODD Digits in


A Number

package PracticeQuestions;

import java.util.Scanner;

public class OddEvenNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");


int num = sc.nextInt();

int evenDigit = 0;
int oddDigit = 0;

while(num!=0) {

int dig = num%10;

if(dig % 2==0) {
evenDigit = evenDigit+1;
num = num/10;
} else {
oddDigit = oddDigit+1;
num = num/10;
}

System.out.println("Number of even digits: " + evenDigit);


System.out.println("Number of odd digits: " + oddDigit);
}
}

Count Sum Of Digits in a Number


package PracticeQuestions;

import java.util.Scanner;

public class CountSumDigit {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int num = sc.nextInt();

int sum = 0;
while(num!=0) {

int rem = num%10;

sum = sum + rem;

num = num/10;
}

System.out.println("Sum of digits of a number: " + sum);

int count = 0;
while(sum!=0) {
int num1 = sum%10;

count = count + 1;

sum = sum/10;
}

System.out.println("Count of digits of a sum of number: " +


count);
}
}

Find Largest Of 3 Numbers

package PracticeQuestions;

import java.util.Scanner;

public class LargestNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter number 1: ");

int num1 = sc.nextInt();

System.out.println("Enter number 2: ");

int num2 = sc.nextInt();


System.out.println("Enter number 3: ");

int num3 = sc.nextInt();

if (num1 > num2 && num1 > num3) {


System.out.println("Largest number is: " + num1);
} else if (num2 > num1 && num2 > num3) {
System.out.println("Largest number is: " + num2);
} else {
System.out.println("Largest number is: " + num3);
}
}
}

 Generate Fibonacci series


package PracticeQuestions;

import java.util.Scanner;

public class FibonacciNum {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the numbers of fibbonaci series:


");

int fiboNum = sc.nextInt();

int num1 = 0;
int num2 = 1;

int sum = 0;

for (int i = 1; i < fiboNum; i++) {

sum = num1 + num2;

num1 = num2;

num2 = sum;

System.out.println(sum);
}
}

 Check Given Number is Prime Or Not


package PracticeQuestions;

import java.util.Scanner;

public class PrimeNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int num = sc.nextInt();

int factors = 0;

if (num > 1) {

for (int i = 1; i <= num; i++) {


if (num % i == 0) {
factors++;
}
}

if (factors == 2) {
System.out.println(num + " is a prime number");
} else if (factors != 2) {
System.out.println(num + " is not a prime
number");

} else {
System.out.println(num + " is not a prime number");

}
Print all prime numbers till given N number
package PracticeQuestions;

import java.util.Scanner;

public class PrimeNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int num = sc.nextInt();

for(int i = 2; i<=num; i++) {


int factors = 0;
for(int j = 1; j<=i; j++) {
if(i%j==0) {
factors++;
}
}
if(factors==2) {
System.out.println(i);
}

Factorial of a Number
package PracticeQuestions;

import java.util.Scanner;

public class FactorialNum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int num1 = sc.nextInt();

int num = num1;

int fact = 1;
while(num!=0) {

fact = fact * num;


num = num - 1;
}

System.out.println("Factorial of " + num1 + " is " + fact);


}
}

Find Sum of Elements in Array


package PracticeQuestions;

import java.util.Scanner;

public class SumArrayElements {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the size pf array: ");

int x = sc.nextInt();

int sum = 0;
int[] arr = new int[x];

for(int i = 0; i<arr.length; i++) {


arr[i] = sc.nextInt();
}

for(int a: arr) {
sum = sum + a;

}
System.out.println(sum);
}
}

Print Even & Odd Numbers from an Array


package PracticeQuestions;

import java.util.Scanner;

public class OddEvenNumArray {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the size pf array: ");

int x = sc.nextInt();

int[] arr = new int[x];

for (int i = 0; i < arr.length; i++) {


arr[i] = sc.nextInt();
}

for(int num: arr) {


if(num%2==0) {

System.out.println("even numbers: " + num);


} else {
System.out.println("odd numbers: " + num);
}
}
}
}
How To Count Occurrences of a Character
in a String
package PracticeQuestions;

public class EachOccuranceString {

public static void main(String[] args) {

String str = "Lalit Bizlain";

int count = 0;

for(int i = 0; i<str.length(); i++) {


if(str.charAt(i)=='a') {
count++;
}
}

System.out.println(count);
}
}

How To Remove White Spaces in a String


package PracticeQuestions;

public class RemoveWhiteSpaces {

public static void main(String[] args) {


String str = "Java is OOPs programming language";

System.out.println("Before removing white spaces " + str);

str = str.replaceAll("\\s", "");

System.out.println("After removing white spaces " + str);


}
}

Equalities of Arrays:
package PracticeQuestions;

public class ArraysEquality {


public static void main(String[] args) {
int[] arr1 = { 1, 2, 3, 4, 5};
int[] arr2 = { 1, 2, 3, 4, 5 };

boolean status = true;

if (arr1.length == arr2.length) {
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
status = false;
break;
}
}

} else {
status = false;
}

if(status) {
System.out.println("Arrays are equals");
} else {
System.out.println("Arrays are not equal");
}
}

Bubble sort:
package PracticeQuestions;

import java.util.Scanner;

public class ArraysMissingNum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

int[] arr = new int[x];


for(int i = 0; i<arr.length; i++) {
arr[i] = sc.nextInt();
}

for(int j = 0; j<arr.length; j++) {


for(int k = j+1; k<arr.length; k++) {
if(arr[j]>arr[k]) {
int temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}

for(int num = 0; num<arr.length; num++) {


System.out.println(arr[num]);
}
}
}

Missing number in array:


package PracticeQuestions;

import java.util.Scanner;

public class MissingNumArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int x = sc.nextInt();
int[] arr = new int[x];

for(int i = 0; i<arr.length; i++) {


arr[i] = sc.nextInt();
}

int n = x+1;
int sum = (n * (n+1))/2;

for(int num: arr) {


sum = sum - num;
}

int missingNum = sum;


System.out.println(missingNum);
}

Remove even elements from array and


return even elements array:
package PracticeQuestions;

import java.util.Scanner;

public class RemoveEvenFromArray {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size: ");
int x = sc.nextInt();

int[] arr = new int[x];

for(int i = 0; i < arr.length; i++) {


arr[i] = sc.nextInt();
}
System.out.println("Array before removing even elements");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

System.out.println();
int count = 0;

for(int j = 0; j < arr.length; j++) {


if(arr[j]%2==0) {
count++;
}
}

int[] evenArr = new int[count];


int index = 0;

System.out.println("Array of even elements");


for(int i = 0; i <arr.length; i++) {
if(arr[i]%2==0) {
evenArr[index] = arr[i];
System.out.print(evenArr[index] + " ");
index++;
}
}

}
}

You might also like