30 Coding Java Questions
30 Coding Java Questions
30 Coding Java Questions
int temp = x;
x = y;
y = temp;
}
}
package PracticeQuestions;
// using +, - operator
int x = 10;
int y = 20;
x = x+y; // x = 10+20 = 30
y = x-y; // y = 30-20 = 10;
x = x-y; // x = 30-10 = 20;
}
}
Reverse a number:
package PracticeQuestions;
import java.util.Scanner;
int revNum = 0;
while(num!=0) {
System.out.println(revNum);
}
Reverse a String:
1. package PracticeQuestions;
2. package PracticeQuestions;
import java.util.Scanner;
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;
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;
int count = 0;
while(num!=0) {
num = num/10;
count= count+1;
}
package PracticeQuestions;
import java.util.Scanner;
int evenDigit = 0;
int oddDigit = 0;
while(num!=0) {
if(dig % 2==0) {
evenDigit = evenDigit+1;
num = num/10;
} else {
oddDigit = oddDigit+1;
num = num/10;
}
import java.util.Scanner;
int sum = 0;
while(num!=0) {
num = num/10;
}
int count = 0;
while(sum!=0) {
int num1 = sum%10;
count = count + 1;
sum = sum/10;
}
package PracticeQuestions;
import java.util.Scanner;
import java.util.Scanner;
int num1 = 0;
int num2 = 1;
int sum = 0;
num1 = num2;
num2 = sum;
System.out.println(sum);
}
}
import java.util.Scanner;
int factors = 0;
if (num > 1) {
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;
Factorial of a Number
package PracticeQuestions;
import java.util.Scanner;
int fact = 1;
while(num!=0) {
import java.util.Scanner;
int x = sc.nextInt();
int sum = 0;
int[] arr = new int[x];
for(int a: arr) {
sum = sum + a;
}
System.out.println(sum);
}
}
import java.util.Scanner;
int x = sc.nextInt();
int count = 0;
System.out.println(count);
}
}
Equalities of Arrays:
package PracticeQuestions;
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;
int x = sc.nextInt();
import java.util.Scanner;
int x = sc.nextInt();
int[] arr = new int[x];
int n = x+1;
int sum = (n * (n+1))/2;
import java.util.Scanner;
System.out.println();
int count = 0;
}
}