Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts

Java Programs for Practice



Number Based Logical Programs

1. Java program to find perfect numbers


2. Java program to print perfect numbers from 1 to 1000

3. Java program to split number into digits.

4. Java program to swap two array.

5. Java program to find NCR factor of given number.

6. Java program to find generic root.

7. Add two number without using addition operator.

8. Java program to sum digits of a number.

9. Java program to reverse a number.

Number System and Conversion 

1. Java program for multiplication of two binary numbers.

2. Java program for addition of two binary numbers.

3. Java program for binary to hexadecimal conversion.

4. Java program for binary to decimal conversion.

5. Java program for octal to binary conversion.

6. Java program for octal to decimal.

7. Java program to convert decimal to binary number.

8. Java program to convert decimal to octal number.

9. Java program to convert decimal to hexadecimal number.

Series Based Programs

1. Java program to find out the sum of A.P. series.

2. Java program to calculate the sum of GP series.

3. Java program to find out the sum of given H.P. Series.

4. Print Sum of series 1+ (1+2) + (1+2+3).... (1+2+3+....n)

5. Print Sum of Series (1/1^2) + (1/2^2) + (1/3^2)....1/n^2

6. Print sum of series (1/1^3) – (1/2^3) + (1/3^3)....1/n^3

7. Print Series 1, 11, 111, 1111....n terms in Java.

8. Print Series 1, 12, 123, 1234.....n in Java.

9. Print Series 1, -3, 5, -7.....n terms in Java.

10. Print Series 2, -4, 6, -8....n terms in Java

11. Java program to print Tribonacci Series.

12. Java program to print fibonacci series.

String Based Programs

1. Program to find the frequency of one string in another.

2. Java program to remove vowels from a string.

3. Program to remove common characters from two strings.

4. Program to finding Shortest and Longest Words in a String.

5. Java program to reverse a String without using direct method.

6. Java program to input name, middle name and surname of a person and print only the initials.

7. Program to remove duplicates characters from given String.

8. Java program to count the occurrence of any character in given String.

9. Java program to find palindromic substring in a string.

10. Java program to accept a string and print each character of the string along with its ASCII code.

Common Logical Programs

1. Java program to find whether given no. is Armstrong or not.

2. Generate prime numbers between 1 & given number

3. Calculate Factorial of a number using recursion

4. Java program to check Palindrome Number.

5. Java program to calculate the Simple Interest.

6. Java program to show all function of bank and generate account number dyanamically.

7. Java program to find out the HCF and LCF.

8. Java program to test the Prime number.

9. Java program to Demonstrate Type Casting.

10. Find the average, sum, min and max of the N numbers Using user Input in Java.

Servlet Based Programs

1. Write a servlet which accepts client request and display the client requested data on the browser?

2. Write a servlet which accepts product details from html form and stores the product details into database?

3. Develop a flexible servlet that should display the data of the database irrespective driver name, table name and dsn name?

4. Write a servlet which retrieves the data from database?

5. Write a servlet which illustrate the concept of ServletContext?

6. Write a servlet which displays current system date and time?

7. Write a servlet which displays a message “I LOVE MY MOM”?

8. Write a servlet which retrieves the data from database?

JDBC Based Programs

1. Write a java program to create a table through frontend application?

2. Write a java program which illustrates the concept of Batch processing?

3. Write a java program which illustrates the concept of updatable ResultSet?

4. Write a java program which illustrates the concept of scrollable ResultSet?

5. Write a java program which points the data of a table along with its column names?

6. Write a java program which illustrates the concept of resource bundle file or how to develop a flexible jdbc application along with its metadata?

7. Write a java program which illustrates the concept of DatabaseMetaData and ResultSetMetaData?

8. Write a jdbc program to retrieve the data from excel?

9. Write a java program which illustrates the concept of procedure?

10. Write a java program which illustrates the concept of function?

11. Write a java program to retrieve the records from a specified database by accepting input from keyboard?

12. Write a java program to insert a record in dept database by accepting the data from keyboard at runtime using dynamic queries?

13. Write a java program to retrieve the data from emp database?

14. Write a jdbc program which will insert a record in the Student database?

Reflection Based Programs

1. Write a java program which will de-Serializable from the specified file?

2. Write a java program which will save the Serializable sub class object into a file?

3. Write a java program to print fields or data members of a class?

4. Write a java program to obtain constructors of a class?

5. Write a java program to obtain information about methods which are present in a class?

6. Write a java program to print super class hierarchy at a current class which is passed from command prompt?

7. Write a java program to find name of the class and its super class name by passing the class name at runtime?

8. Write a java program to print name of the current class and its super class name?

Logical Programs

1. How can you determine if String has all Unique Characters.?

2. How to reverse a String without using any direct method.?

3 How to convert String to integer without using any direct method in Java.?

4. How to find the missing values from an sorted array.?

5. How to reverse an array without using any built in method and any other array.?

6. How to remove specific character from an String in Java.?

7. How to find the caller of a method in Java.?

8. How to call private method from another class in java.?

Matrix Based Program

1. Addition , Substraction and trace of two Matrix in Java.

2. Find the Transpose of given Matrix in Java.

3. Program for Matrix multiplication in Java.

4. Calculate the Sum of the Elements of each Row & Column of given Matrix in Java.

5. Check if a given Matrix is an Identity (Unit) Matrix or not in Java

6. Find the Frequency of Odd & Even Numbers in the given Matrix in Java

7. Determine if a given Matrix is a Sparse Matrix in Java.

Pyramid Based Programs

1. Write a program to generate a following #'s triangle:



2. Write a program to generate a following @'s triangle:



3. Write a Java program to print the following triangle:




4. Write a Java program to display the following rhombus symbol structure:



5. Write a Java program to display the following number rhombus structure:



6. Write a Java program to print the following number pyramid:





7. Write a Java program to display the following character rhombus structure:



Character pattern pyramid program in Java





package demo;
import java.util.Scanner;
public class CharacterPattern
{
 public static void main(String[] args) 
 {
  char ch, r, c;
  System.out.println("Enter last pyramid character  : ");
  Scanner sc = new Scanner(System.in);
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  for (r = 'A'; r <= ch; r++) {
   for (c = 'A'; c <= r; c++) {
    System.out.print(r + "" + (char)(r + 32) + " ");
   }
   System.out.println();
  }
 }
}

Output:

Enter last pyramid character  : 
e

Aa 
Bb Bb 
Cc Cc Cc 
Dd Dd Dd Dd 
Ee Ee Ee Ee Ee

BUILD SUCCESSFUL (total time: 7 seconds)


Java Program for Reverse Floyd Triangle - Half Floyd number diamond






package demo;
public class FloydTriangle
{
 public static void main(String[] args) 
 {
  int num = 4, y;
  y = num + 1;
  for (int r = 1; r <= num; r++, y--) {
   for (int c = 1, n = num; c <= r; c++, n--) {
    System.out.print(" " + n);
   }
   for (int c = 1, p = y; c < r; c++, p++) {
    System.out.print(" " + p);
   }
   System.out.println();
  }
  for (int r = num - 1, x = 1; r >= 1; r--, x--) {
   for (int c = r, n = num; c >= 1; c--, n--) {
    System.out.print(" " + n);
   }
   for (int c = r, z = num - x; c > 1; c--, z++) {
    System.out.print(" " + z);
   }
   System.out.println();
  }
 }
}

Output:

 4
 4 3 4
 4 3 2 3 4
 4 3 2 1 2 3 4
 4 3 2 3 4
 4 3 4
 4

BUILD SUCCESSFUL (total time: 1 second)


Java Program for Number Pattern of Half Diamond.





package demo;
import java.util.Scanner;
public class HalfDaimond 
{
 public static void main(String[] args) 
 {
  int num, q;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter Seeding Number : ");
  num = sc.nextInt();
  for (int r = 1, n = num; r <= num; r++, n--) {
   for (int c = r, p = n; c >= 1; c--, p++) {
    System.out.print(p);
   }
   for (int c = r, p = num - 1; c > 1; c--, p--) {
    System.out.print(p);
   }
   System.out.println();
  }
  for (int r = 1; r < num; r++) {
   for (int c = r, p = r + 1; c < num; c++, p++) {
    System.out.print(p);
   }
   for (int c = num - r, p = num - 1; c > 1; c--, p--) {
    System.out.print(p);
   }
   System.out.println();
  }
 }
}


Output:

Enter Seeding Number : 4

4
343
23432
1234321
23432
343
4


BUILD SUCCESSFUL (total time: 3 seconds)


Java program to split number into digits.



package demo;
import java.util.Scanner;
public class SplitNumber
{
 public static void main(String[] args)
 {
  int num, temp, factor = 1;
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number: ");
  num = sc.nextInt();
  temp = num;
  while (temp != 0) {
   temp = temp / 10;
   factor = factor * 10;
  }
  System.out.print("Each digits of given number are: ");
  while (factor > 1) {
   factor = factor / 10;
   System.out.print((num / factor) + " ");
   num = num % factor;
  }
 }
}

Output:
Enter a number: 4732
Each digits of given number are: 4 7 3 2
BUILD SUCCESSFUL (total time: 21 seconds)



Java program for octal to decimal.



package demo;
import java.util.Scanner;
public class OctalToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long octal, decimal = 0;
  int i = 0;
  System.out.print("Enter any octal number: ");
  octal = sc.nextLong();
  while (octal != 0) 
  {
   decimal = (long)(decimal + (octal % 10) * Math.pow(8, i++));
   octal = octal / 10;
  }
  System.out.print("Equivalent decimal value: " + decimal);
 }
}

Output:
Enter any octal number: 1000
Equivalent decimal value: 512
BUILD SUCCESSFUL (total time: 3 seconds)


Java program to reverse a String without using direct method.



import java.io.BufferedReader;
public class ReverseOfString {
 public static String reverseString(String s)
 {
  int l = s.length();
  String backward = "";
  for (int i = l - 1; i >= 0; i--) 
 {
   backward = backward + s.charAt(i);
  }
  return backward;
 }
 public static void main(String args[]) {
  String backwards = reverseString("Umesh Kushwaha");
  System.out.println("Reverse String is " + backwards);
 }
}


Output:
Reverse String is ahawhsuK hsemU



Java program to accept a string and print each character of the string along with its ASCII code.



class CalculateAscii {
 public static void main(String args[]) {
  word("Umesh Kushwaha");
 }
 static void word(String s) {
  int len = s.length();
  int a;
  char a1;
  for (int i = 0; i < len; i++) {
   a1 = s.charAt(i);
   a = a1;
   System.out.println(a1 + "–>" + a);
  }
 }
}


Output:

U–>85
m–>109
e–>101
s–>115
h–>104
 –>32
K–>75
u–>117
s–>115
h–>104
w–>119
a–>97
h–>104
a–>97


Java program to calculate Factorial of a number using recursion



/*
This program shows how to calculate
Factorial of a number using recursion function.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaFactorialUsingRecursion {
 public static void main(String args[]) throws NumberFormatException,
  IOException {
   System.out.println("Enter the number: ");    //get input from the user
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   int a = Integer.parseInt(br.readLine());
   int result = fact(a);            //call the recursive function to generate factorial
   System.out.println("Factorial of the number is: " + result);
  }
 static int fact(int b) {
  if (b <= 1)                           //if the number is 1 then return 1
   return 1;
  else                                    //else call the same function with the value - 1
   return b * fact(b - 1);
 }
}


/*
Output of this Java example would be
Enter the number:
5
Factorial of the number is: 120
*/


Java program to Generate prime numbers between 1 & given number



/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to generate prime numbers
between 1 and given number using for loop.
*/
public class GeneratePrimeNumbersExample {
 public static void main(String[] args) {
  int limit = 100;                       //define limit
  System.out.println("Prime numbers between 1 and " + limit);
  for (int i = 1; i < 100; i++) {    //loop through the numbers one by one
   boolean isPrime = true;       
   for (int j = 2; j < i; j++) {      //check to see if the number is prime
    if (i % j == 0) {
     isPrime = false;
     break;
    }
   }                                         // print the number
   if (isPrime)
    System.out.print(i + " ");
  }
 }
}

/*
Output of Prime Numbers example would be
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/




Java program to check Palindrome Number.



/*
This program shows how to check for in the given list of numbers
whether each number is palindrome or not
*/
public class JavaPalindromeNumberExample {
 public static void main(String[] args) {
  int numbers[] = new int[] {121,13,34,11,22,54};
  for (int i = 0; i < numbers.length; i++) {
   int number = numbers[i];
   int reversedNumber = 0;
   int temp = 0;
   /*
    * If the number is equal to it's reversed number, then
    * the given number is a palindrome number.
    *
    * For ex,121 is a palindrome number while 12 is not.
    */
   //reverse the number
   while (number > 0) {
    temp = number % 10;
    number = number / 10;
    reversedNumber = reversedNumber * 10 + temp;
   }
   if (numbers[i] == reversedNumber)
    System.out.println(numbers[i] + " is a palindrome");
   else
    System.out.println(numbers[i] + " not a palindrome ");
  }
 }
}


/*
Output of Java Palindrome Number Example would be
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/



Java program to design a class account using the inheritance and static that show all function of bank and generate account number dyanamically.



import java.util.*;
class Bank {
 static int acc_no = 10001;
 float amt;
 public void display() {
  System.out.println("Account no :" + acc_no);
  System.out.println("Current Amount :" + amt);
 }
 public bank() {
  amt = 1000;
  System.out.println("Ur account no is " + acc_no);
  acc_no++;
 }
 public void getamt() {
  System.out.println("Current balance :" + amt);
 }
 public void withdraw(float x) {
  if (amt == 1000 || amt <= x) {
   System.out.println("Sorry u can't withdraw");
  } else {
   amt = amt - x;
   System.out.println("amount withdrawn :" + x);
   System.out.println("After withdrawl");
   getamt();
  }
 }
 public void deposit(float x) {
  if (x == 0.0)
   System.out.println("OOPS 0 can't be deposited");
  else {
   amt += x;
   System.out.println("After deposition");
   getamt();
  }
 }
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  bank b1 = new bank();
  b1.deposit(0);
  b1.withdraw(120.5 f);
  b1.display();
  System.out.println("\n");
  bank b2 = new bank();
  b2.deposit(1000.0 f);
  b2.withdraw(150.5 f);
 }
}



Java program to calculate the Simple Interest and Input by the user.



import java.util.*;
class SimpleInterest {
 int p, t;
 float si, r;
 public si() {
  r = 0;
  p = 0;
 }
 public void getdata() {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter principle : ");
  p = sc.nextInt();
  System.out.println("Enter rate : ");
  r = sc.nextFloat();
  System.out.println("Enter time period : ");
  t = sc.nextInt();
 }
 public void cal() {
  si = (p * r * t) / 100;
 }
 public void display() {
  System.out.println("Principle : Rs" + p);
  System.out.println("Rate : " + r);
  System.out.println("Time period : " + t);
  System.out.println("Simple Interest : Rs" + si);
 }
 public static void main(String args[]) {
  si s = new si();
  s.getdata();
  s.cal();
  s.display();
 }
}


Java program to test the Prime number.



import java.util.*;
class Prime {
 public static void main(String args[]) {
  int flag, x, i;
  flag = 0;
  int a[] = new int[7];
  for (x = 0; x < args.length; x++) {
   a[x] = Integer.parseInt(args[x]);
   for (i = 2; i < (a[x] / 2); i++) {
    if ((a[x] % i) == 0) {
     break;
    } else flag = 1;
   }
   if (flag == 1)
    System.out.println(a[x] + " is a prime no ");
   else
    System.out.println(a[x] + " is not a prime no ");
   flag = 0;
  }
 }
}


Java program to Demonstrate Type Casting.



class Typecast {
 public static void main(String args[]) {
  byte h = 127;
  int a = 300;
  float a1 = 12.222 f;
  float g;
  short b = 200;
  long c = 999999;
  float e = 345.89 F;
  double f = 45645.78222222222222;
  g = (float) f;
  System.out.println("short b =" + g);
  System.out.println("short b =" + b);
  System.out.println("long c =" + c);
  System.out.println("float e=" + e);
  System.out.println("double f=" + f);
  System.out.println("short b=" + b);
  System.out.println("short to byte " + (byte) b);
  System.out.println("int to byte " + (byte) a);
  System.out.println("int to float" + (float) a);
  System.out.println("long to byte " + (byte) c);
  System.out.println("double to long " + (long) f);
  System.out.println("double to int " + (int) f);
  System.out.println("double to byte " + (byte) f);
  System.out.println("double to short " + (short) f);
  System.out.println("double to float " + (float) f);
  System.out.println("float to int " + (int) e);
  System.out.println("float to byte " + (byte) e);
  System.out.println("float to short " + (short) e);
  System.out.println("float to long " + (long) e);
  System.out.println("float to double =" + (double) e);
  System.out.println("long to int" + (int) c);
  System.out.println("byte to int =" + (int) h);
 }
}