Showing posts with label Java. Show all posts
Showing posts with label Java. 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:



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 find palindromic substring in a string.



public class PalidrumSubString 
{
 public static void main(String args[]) 
 {
  String s = "my name rar kumar poop";
  String p[] = s.split(" ");
  String s2 = "";
  for (int j = 0; j < p.length; j++) 
  {
   for (int i = p[j].length() - 1; i >= 0; i--) 
   {
    char z = p[j].charAt(i);
    s2 = s2 + z;
   }

   if (p[j].equals(s2)) 
   {
    System.out.println(s2);
   }
   else 
   {
    s2 = "";
   }
  }
 }
}

Output:
rar
poop


Java program to swap two array.



package demo;
import java.util.Scanner;
public class SwappingArray
{
 public static void main(String[] args) 
 {
  int[] a = new int[10], b = new int[10], c = new int[10];
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter First array: ");
  for (int i = 0; i < 10; i++) {
   a[i] = sc.nextInt();
  }
  System.out.println("Enter Second array: ");
  for (int i = 0; i < 10; i++) {
   b[i] = sc.nextInt();
  }
  System.out.println("Arrays before swapping");
  System.out.println("First array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(a[i]);
  }
  System.out.println("Second array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(b[i]);
  }
  for (int i = 0; i < 10; i++) {            //write any swapping technique
   c[i] = a[i];
   a[i] = b[i];
   b[i] = c[i];
  }
  System.out.println("Arrays after swapping");
  System.out.println("First array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(a[i]);
  }
  System.out.println("Second array: ");
  for (int i = 0; i < 10; i++) {
   System.out.print(b[i]);
  }
 }
}

Output:

Enter First array: 
1
2
3
4
5
6
7
8
9
10

Enter Second array: 

11
22
3
44
55
66
77
55
22
33

Arrays before swapping


First array: 12345678910

Second array: 1122344556677552233

Arrays after swapping

First array: 1122344556677552233
Second array:  12345678910

BUILD SUCCESSFUL (total time: 27 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 to find NCR factor of given number.



Calculate the combinations for C(n,r) = n! / ( r!(n - r)! ). For 0 <= r <= n.

package demo;
import java.util.Scanner;
public class Ncr
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  int n, r, ncr;
  System.out.print("Enter any two numbers-");
  n = sc.nextInt();
  r = sc.nextInt();
  ncr = fact(n) / (fact(r) * fact(n - r));
  System.out.print("The NCR factor of " + n + " and " + r + " is " + ncr);
 }

 public static int fact(int n) 
 {
  int i = 1;
  while (n != 0) {
   i = i * n;
   n--;
  }
  return i;
 }
}

Output:
Enter any two numbers- 5
3
The NCR factor of 5 and 3 is 10
BUILD SUCCESSFUL (total time: 5 seconds)



Java program to find generic root.



Generic RootIt sum of digits of a number unit we don't get a single digit.
Example:
Generic root of 4563: 4+5+6+3 = 18 since 18 is two digit numbers so 1 + 8 = 9
So, generic root of 4563 = 9


package demo;
import java.util.Scanner;
public class GenericRoot
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  long num, sum = 0, r;
  System.out.print("Enter a number:-");
  num = sc.nextLong();
  while (num > 10) 
  {
   sum = 0;
   while (num != 0) 
   {
    r = num % 10;
    num = num / 10;
    sum += r;
   }
   if (sum > 10) 
   {
    num = sum;
   } else
   {
    break;
   }
  }
  System.out.println("Sum of the digits in single digit is: " + sum);
 }
}

Output:
Enter a number:-5555
Sum of the digits in single digit is: 2
BUILD SUCCESSFUL (total time: 6 seconds)


Java program to add two number without using addition operator.



package demo;
import java.util.Scanner;
public class Addition
{
 public static void main(String[] args) 
 {
  int a, b;
  int sum;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter any two integers: ");
  a = sc.nextInt();
  b = sc.nextInt();
  sum = a - ~b - 1;
  System.out.print("Sum of two integers: " + sum);
 }
}

Output:
Enter any two integers: 20
50
Sum of two integers: 70
BUILD SUCCESSFUL (total time: 10 seconds)



Algorithm:

In c ~ is 1's complement operator. This is equivalent to:  
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b


Java program to sum digits of a number.



package demo;
import java.util.Scanner;
public class SumOfDigits
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int num, sum = 0, r;
  System.out.println("Enter a number: ");
  num = sc.nextInt();
  while (num != 0) {
   r = num % 10;
   num = num / 10;
   sum = sum + r;
  }
  System.out.print("Sum of digits of number:  " + sum);
 }
}

Output:
Enter a number: 32165
Sum of digits of number:  17
BUILD SUCCESSFUL (total time: 8 seconds)



Java program to reverse a number.



package demo;
import java.util.Scanner;
public class ReverseNumber
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int num, r, reverse = 0;
  System.out.print("Enter any number: ");
  num = sc.nextInt();
  while (num != 0) 
  {
   r = num % 10;
   reverse = reverse * 10 + r;
   num = num / 10;
  }
  System.out.println("Reversed of number: " + reverse);
 }
}

Output:
Enter any number: 123789
Reversed of number: 987321
BUILD SUCCESSFUL (total time: 4 seconds)


Java program for multiplication of two binary numbers.



package demo;
import java.util.Scanner;
public class BinaryMultiplication
{
 public static void main(String[] args)
 {
  long binary1, binary2, multiply = 0;
  int digit, factor = 1;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the first binary number: ");
  binary1 = sc.nextLong();
  System.out.print("Enter the second binary number: ");
  binary2 = sc.nextLong();
  while (binary2 != 0)
  {
   digit = (int)(binary2 % 10);
   if (digit == 1) 
   {
    binary1 = binary1 * factor;
    multiply = binaryproduct((int) binary1, (int) multiply);
   } 
   else
   {
    binary1 = binary1 * factor;
   }
   binary2 = binary2 / 10;
   factor = 10;
  }
  System.out.print("Product of two binary numbers: " + multiply);
 }

 static int binaryproduct(int binary1, int binary2) 
 {
  int i = 0, remainder = 0;
  int[] sum = new int[20];
  int binaryprod = 0;

  while (binary1 != 0 || binary2 != 0) 
  {
   sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
   remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
   binary1 = binary1 / 10;
   binary2 = binary2 / 10;
  }
  if (remainder != 0)
  {
   sum[i++] = remainder;
  }
  --i;
  while (i >= 0) 
  {
   binaryprod = binaryprod * 10 + sum[i--];
  }
  return binaryprod;
 }
}

Output:
Enter the first binary number: 11100
Enter the second binary number: 11100
Product of two binary numbers: 1100010000
BUILD SUCCESSFUL (total time: 16 seconds)



Java program for binary to hexadecimal conversion.



package demo;
import java.util.Scanner;
public class BinaryToHexadecimal
{
 public static void main(String[] args)
 {
  int[] hex = new int[1000];
  int i = 1, j = 0, rem, dec = 0, bin;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter a Binary Number: ");
  bin = sc.nextInt();
  while (bin > 0) {
   rem = bin % 2;
   dec = dec + rem * i;
   i = i * 2;
   bin = bin / 10;
  }
  /* At this state our input number is converted into Decimal Number */
  i = 0;
  while (dec != 0) {
   hex[i] = dec % 16;
   dec = dec / 16;
   i++;
  }
  System.out.print("Equivalent HexaDecimal value: ");
  for (j = i - 1; j >= 0; j--)
  {
   if (hex[j] > 9) 
   {
    System.out.print((char)(hex[j] + 55));
   } else
   {
    System.out.print(hex[j]);
   }
  }
 }
}

Output:
Enter a Binary Number: 11111100
Equivalent HexaDecimal value: FC
BUILD SUCCESSFUL (total time: 6 seconds)