1. Program to find whether a given number is even or odd.
public class P1_EvenNumber {
public static void main(String[] args) {
int num = 51;
// using mod(%) which gives us the remainder
// comparing remainder with zero
if (num % 2 == 0) {
System.out.println(num + " : Number is Even");
} else {
System.out.println(num + " : Number is Odd");
}
}
}
2. Program to find whether a given number is prime or not.
public class P2_PrimeNumber {
public static void main(String[] args) {
int num = 23;
int flag = 0;
if (num == 1) {
System.out.println(" 1 is neither prime nor composite");
} else {
// check for number less than half of it as it won't be completely divisible by
// number more than it
// starting with 2 as 1 is neither prime nor composite
for (int i = 2; i <= (num / 2); i++) {
// check divisible by i or not
// if divisible, then not prime
if (num % i == 0) {
flag++;
}
}
if (flag > 0) {
System.out.println(num + " is not a Prime Number");
} else {
System.out.println(num + " is a Prime Number");
}
}
}
}
3. Program to print the first 10 numbers of the Fibonacci Series.
public class P3_FibonacciSeries {
public static void main(String[] args) {
int i = 0;
int j = 1;
int z = 0;
//Print first 10 numbers of fibonacci series
int count = 10;
System.out.println(i);
System.out.println(j);
//Adding up values and resetting values of i and j with the latest values(previous)
for(int k =0; k < count-2; k++) {
z = i + j;
i = j;
j = z;
System.out.println(z);
}
}
}
4. Program to check whether a number is Armstrong number or not.
public class P4_ArmstrongNumber {
public static void main(String[] args) {
int inputNum = 370;
int sumOfCube = 0;
int num = inputNum;
while(num > 0) {
//calculate sumOfCube
//fetch last digit of number by using mod(%)
int lastDigit = num % 10;
//updating num by removing the last digit
num = num / 10;
//Add sum of cubes of each digit
sumOfCube = sumOfCube + (lastDigit * lastDigit * lastDigit);
}
if(sumOfCube == inputNum) {
System.out.println(inputNum + " is an ArmstrongNumber");
} else {
System.out.println(inputNum + " is not an Armstrong Number");
}
}
}
5. Program to find the factorial of a number.
public class P5_Factorial {
public static void main(String[] args) {
int num = 5;
int factorial = 1;
//multiply in decreasing order by decrementing till you reach 1
// we will apply a reverse loop
for(int i = num; i > 0; i--) {
factorial = factorial * i;
}
System.out.println("Factorial of " + num + " is : " + factorial);
}
}
6. Program to find the factorial of a number using recursion.
public class P6_FactorialUsingRecursion {
//Number to check factorial for
int num = 5;
//Number to store factorial
int result = 1;
public static void main(String[] args) {
FactorialUsingRecursion f = new FactorialUsingRecursion();
System.out.println(f.fact(f.num));
}
public int fact(int num) {
if (num > 0) {
result = result * num;
//calling recursively
fact(num-1);
}
return result;
}
}
7. Program to find missing numbers in an array between 1 to 10.
import java.util.Arrays;
public class P7_MissingNumbersInArray {
public static void main(String[] args) {
int[] input = { 5, 7, 6, 2 };
int index = 0;
Arrays.sort(input);
//Approach 1
System.out.println("Approach 1");
System.out.println("----------");
int limit = input[input.length-1];
for(int i = 1; i < limit; i++) {
if(input[index] == i) {
index++;
} else {
System.out.println(i);
}
}
while(limit < 10) {
System.out.println(++limit);
}
//Approach 2
System.out.println("Approach 2");
System.out.println("----------");
int[] output = new int [11];
for(int i : input) {
output[i] = 1;
}
for(int i=1; i<output.length; i++) {
if( output[i] == 0) System.out.println(i);
}
}
}
8. Program to find the reverse of a string.
public class P8_ReverseAString {
public static void main(String[] args) {
String originalString = "Miss TecHack";
String reversedString = "";
//convert string to char array
char[] charArray = originalString.toCharArray();
//Applying a reverse loop
for(int i = charArray.length-1; i >= 0; i--) {
//appending string in reverse order
reversedString = reversedString + charArray[i];
}
System.out.println("Original String : " + originalString);
System.out.println("Reversed String : " + reversedString);
}
}
9. Program to find the reverse of a string using recursion.
public class P9_ReverseAStringUsingRecursion {
String input = "Recursion";
public static void main(String[] args) {
ReverseAStringUsingRecursion r = new ReverseAStringUsingRecursion();
System.out.println(r.reverse(r.input));
}
public String reverse(String inputString) {
if(inputString.isEmpty()) {
return inputString;
}
if(inputString.length() > 0) {
return inputString.charAt(inputString.length()-1) + reverse(inputString.substring(0, inputString.length()-1));
}
return null;
}
}
10. Program to find the reverse of a string using the default method.
public class P10_ReverseAStringUsingDefaultMethod {
public static void main(String[] args) {
StringBuffer input = new StringBuffer("Buffer");
System.out.println(input.reverse());
StringBuilder input2 = new StringBuilder("Builder");
System.out.println(input2.reverse());
}
}