Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.
import java.util.Scanner;
public class Series  
{
 public static void main(String args[])
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                                          // s for terms of series, c for n terms
  for (c = 1; c <= n; c++)                          // To generate n terms
  {
   s = s * 10 + 1;
   System.out.print(s + " ");
  }                                                           //for  ends
 }
}


Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111...... 
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                       // s for terms of series, c for counter to generate n terms
  for (c = 1; c <= n; c++) {
   s = s * 10 + c;
   System.out.print(s + " ");
  }
 }
}


Output:
Enter the number of terms: 6
1,  12,  123,  1234,  12345,  123456....... 
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class OddSeries 
{
 public static void main(String args[]) 
  {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int i = 1, c, f = 1;                            // i for odd nos, c for counter, f for flag
  for (c = 1; c <= n; c++) {
   if (f % 2 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
   i += 2;
   f++;
  }                                                  //Loop ends
 }
}


Output:


Enter the number of terms: 6

1  -3  5  -7  9  -11...... 
BUILD SUCCESSFUL (total time: 6 seconds)

import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  int c, i = 2, n;                                          // c for counter, i for even nos.
  System.out.print("Enter the number of terms: ");
  n = sc.nextInt();
  System.out.print("\n");
  for (c = 1; c <= n; c++, i += 2)               //to generate n terms of the series
  {
   if (i % 4 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
  }
 }
}

Output:

Enter the number of terms: 10
2 -4 6 -8 10 -12 14 -16 18 -20 
BUILD SUCCESSFUL (total time: 6 seconds)

A tribonacci series is one in which the sum next term is the sum of previous three terms
Example 0 1 2 3 6 11 20………….


import java.util.Scanner;
public class Tribonacci 
 {
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 2, d = 0, i;
  System.out.print(a + " , " + b + " , " + c);
  for (i = 4; i <= n; i++) {
   d = a + b + c;
   System.out.print(", " + d);
   a = b;
   b = c;
   c = d;
  }
 }
}

Output:

0 , 1 , 2, 3, 6, 11, 20, 37, 68, 125
BUILD SUCCESSFUL (total time: 1 second)
A fibonacci series is one in which the next term is the sum of previous two terms
Example. 0 1 1 2 3 5 8……



import java.util.Scanner;
public class Fibonacci 
{
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 0, i;
  System.out.print(a + " , " + b);
  for (i = 3; i <= n; i++) {
   c = a + b;
   System.out.print(", " + c);
   a = b;
   b = c;
  }
 }
}

Output:

0 , 1, 1, 2, 3, 5, 8, 13, 21, 34
BUILD SUCCESSFUL (total time: 0 seconds)
import java.util.Scanner;
public class WordFrequencyCounter {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter main string");
        String s = scan.nextLine();
        System.out.println("enter string to be searched");
        String f = scan.nextLine();
        s = s + " ";
        int l = s.length();
        char a;
        int c = 0;
        String s1 = "";
        for (int i = 0; i < l; i++) {
            a = s.charAt(i);
            if (a != ' ') {
                s1 = s1 + a;
            } else {
                if (s1.equalsIgnoreCase(f) == true) {
                    c++;
                }
                s1 = "";
            }
        }
        System.out.println("Frequency of the word " + f + " is " + c);
    }                                         
}

Output:
Enter main string
Umesh Kumar Kushwaha Kumar
enter string to be searched
Kumar
Frequency of the word Kumar is 2

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveVowels
{
 public static void main(String args[]) throws IOException {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   String str1, str2 = "";
   char ch, ch1;
   int i, len;
   System.out.println("Enter the Sentence:");
   str1 = br.readLine();
   len = str1.length();
   for (i = 0; i < len; i++) {
    ch = str1.charAt(i);
    ch1 = Character.toLowerCase(ch);
    switch (ch1) {
     case 'a':
     case 'e':
     case 'i':
     case 'o':
     case 'u':
      break;
     default:
      str2 = str2 + ch;
    }                           //switch ends
   }                            //for ends
   System.out.println("Modified string without vowels:" + str2);
  }                            //method ends
}

Output:
Enter the Sentence:
umesh kumar kushwaha
Modified string without vowels:msh kmr kshwh
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RemoveCommonDuplicateChar 
{
 public static void main(String args[]) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String s1, s2, s3 = "", s4 = "";
  int a[], b[], x = 0, y = 0;
  int len1, len2;
  char ch;
  int i, j;
  System.out.println("Enter the first sentence:");
  s1 = br.readLine().trim();
  System.out.println("Enter the second sentence:");
  s2 = br.readLine().trim();
  len1 = s1.length();
  len2 = s2.length();
  a = new int[len1];
  b = new int[len2];
  for (i = 0; i < len1; i++) {
   ch = s1.charAt(i);
   for (j = 0; j < len2; j++) {
    if (ch == s2.charAt(j)) {
     break;
    }
   }
   if (j != len2) {
    a[x++] = i;
    b[y++] = j;
   }
  }
  for (i = 0; i < len1; i++) {
   for (j = 0; j < x; j++) {
    if (i == a[j]) {
     break;
    }
   }
   if (j == x) {
    s3 = s3 + s1.charAt(i);
   }
  }
  for (i = 0; i < len2; i++) {
   for (j = 0; j < y; j++) {
    if (i == b[j]) {
     break;
    }
   }
   if (j == y) {
    s4 = s4 + s2.charAt(i);
   }
  }
  System.out.println("Original string1=" + s1 + " Modified string1=" + s3);
  System.out.println("Original string2=" + s2 + " Modified string2=" + s4);
 }
}

Output:

Enter the first sentence:
Umesh Kumar Kushwaha
Enter the second sentence:
Rahul Kumar Singh
Original string1=Umesh Kumar Kushwaha Modified string1=Uessw
Original string2=Rahul Kumar Singh Modified string2=Rlua Singh
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

import java.io.BufferedReader;
class FindMinMaxString {
 public static void main(String args[]) {
  findMethod("My name is Umesh Kushwaha");
 }
 static public void findMethod(String s) {
  String str = s + " ";
  char ch = ' ';
  int len = str.length(), l = 0;
  int min = len, max = 0;
  String shortest_word = "", longest_word = "", word = "";
  for (int i = 0; i < len; i++) {
   ch = str.charAt(i);
   if (ch != ' ') {
    word += ch;
   }                                     //if ends
   else {
    l = word.length();
    if (l < min) {
     min = l;
     shortest_word = word;
    }                                     //if ends
    if (l > max) {
     max = l;
     longest_word = word;
    }
    word = "";
   }
  }
  System.out.println("Shortest word = " + shortest_word + " with length " + min);
  System.out.println("Longest word = " + longest_word + " with length " + max);
 }
}

Output:
Shortest word = My with length 2
Longest word = Kushwaha with length 8

Example.

Enter your name
Umesh Kumar Kushwaha
Initials are   U.K.K.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class InitialName {
 public static void main(String args[]) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter your name");
  String s = br.readLine(), s1 = "";
  s = " " + s;
  int len = s.length();
  char a;
  for (int i = 0; i < len; i++) {
   a = s.charAt(i);
   if (a == ' ') {
    s1 = s1 + s.charAt(i + 1) + ".";
   }
  }
  System.out.println("Initials are   " + s1);
 }
}

Output:
Enter your name
Umesh Kumar Kushwaha
Initials are   U.K.K.

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

class CountFrequencyOfWord {
 public static void main(String args[]) {
  try {
   input();
  } catch (IOException ex) {
   Logger.getLogger(CountFrequencyOfWord .class.getName()).log(Level.SEVERE, null, ex);
  }
 }
 static void input() throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a sentence");
  String s = br.readLine();
  System.out.println("Enter the letter ");
  char a = (char) br.read();
  int len = s.length();
  int c = 0;
  char x;
  for (int i = 0; i < len; i++) {
   x = Character.toLowerCase(s.charAt(i));
   if (x == a)
    c = c + 1;
  }
  System.out.println("Frequency of the letter  " + a + " in the sentence " + s + " is " + c);
 }
}



Output:

Enter a sentence
Hi my name is umesh
Enter the letter 
m
Frequency of the letter  m in the sentence "Hi my name is umesh" is 3
class RemoveDuplicate {
 public static void main(String args[]) {
  remove("123aa bbbcc  c2589  99oppq rtyyy");
 }
 static void remove(String s) {
  int l = s.length();
  int c;
  String org = s, s1 = "";
  for (int i = 0; i < (l - 1); i++) {
   s1 = s.substring(0, i + 1);
   c = 0;
   for (int j = i + 1; j < l; j++) {
    if (s.charAt(i) == s.charAt(j)) {
     c++;
     continue;
    } else
     s1 = s1 + s.charAt(j);
   }
   s = s1;
   s1 = "";
   if (c > 0)
    l -= c;
  }
  System.out.println("Original String:" + org);
  System.out.println("String after removing duplicates:  " + s);
 }
}


Output:

Original String:123aa bbbcc  c2589  99oppq rtyyy

String after removing duplicates:  123a bc589opqrty