Java Coding Drill – One

1. Factorial finding

public class Fact {
        public static int factorial(int n) {
            if (n==0)
                  return 1;
           else
                 return n * factorial(n-1);
        }
        public static void main(String argv[]) {
             int x;
             x=9;
             System.out.println(“Factorial of “+x+” is “+factorial(x));
        }
}

2. Divisible by number

public class Divisor{
          public static void main(String[] args){
               int a = 10;
               int b = 2;
               if ( a % b == 0 ){
                      System.out.println(a + ” is divisible by “+ b);
              }else{
                      System.out.println(a + ” is not divisible by ” + b);
              }
         }
}

3. Compound Interest

public class CompoundInterest {
            public static void main(String[] args) {
               double principal;
               double rate;
               double interest;
               principal = 17000;
               rate = 0.07;
               interest = principal * rate; 
               principal = principal + interest;
               System.out.print(“The interest earned is $”);
               System.out.println(interest);
               System.out.print(“The value of the investment after one year is $”);
               System.out.println(principal);
          }
}

4. Printing Square of number

public class PrintSquare {
        public static void main(String[] args) throws Exception {
             int square = 0;
             int userInput = 0;
             userInput = Integer.parseInt(args[0]);
             square = userInput * userInput ;
             System.out.print(“The square of “+userInput+” is “+square);
        }
}

5. Sum of Numbers

     import java.util.*;
      public class SumNo{
            public static void main(String args[]){
                  Scanner console = new Scanner(System.in);
                  System.out.print(“Type a number: “);
                  int num1 = console.nextInt();
                  System.out.print(“Type a number: “);
                  int num2 = console.nextInt();
                  System.out.print(“Type a number: “);
                  int num3 = console.nextInt();
                  int sum = num1 + num2 + num3;
                  System.out.println(“The sum is ” + sum);
             }
      }

6. Finding even/odd numbers

public class EvenOdd{
       public static void main(String[] args) {
             for (int i = 1; i <= 20; i++) {
                  if (i % 2 == 0) {
                     System.out.println(i + ” is even”);
                  } else {
                     System.out.println(i + ” is odd”);
                  }
             }
       }
}

7. Finding Prime Number

public class PrimeNo {
         public static void main(String[] args) {
             for(int i=0;i<100;i++){
                  if(countFactor(i)==2){
                    System.out.println(i);
                  }
              }
          }
         public static int countFactor(int num){
              int cnt = 0;
              for(int j = 1; j <= num; j++) {
                 if (num % j == 0) {
                     cnt++;
                 }
               }
                return cnt;
           }
 }

8.Counting the charectors

public class CountChar {
      public static void main(String[] args) {
           String str = “mississippi”
           int tot = cntCh(str);
           System.out.println(“Total count :”+tot);
      }
      public static int cntCh(String str) {
          int count = 0;
          for (int i = 0; i < str.length(); i++) {
             if (str.charAt(i) == ‘s’) {
                  count++;
              }
         }
        return count;
      }
}

9. Printing Time and Date format

import java.util.*;
     public class TimeDateFormat {
         public static void main(String args[]) {
             Formatter fmt = new Formatter();
             Calendar cal = Calendar.getInstance();
             // Display standard 12-hour time format.
             fmt.format(“%tr”, cal);
             System.out.println(fmt); 
             // Display complete time and date information. 
             fmt = new Formatter();
             fmt.format(“%tc”, cal);
             System.out.println(fmt);
             // Display just hour and minute.
             fmt = new Formatter(); 
             fmt.format(“%tl:%tM”, cal, cal);
             System.out.println(fmt);
             // Display month by name and number.
             fmt = new Formatter();
             fmt.format(“%tB %tb %tm”, cal, cal, cal);
            System.out.println(fmt);
      }
}

10. Find In Line

import java.util.*;
        public class FindInLine{
            public static void main(String args[]) {
            String instr = “Name: Tom Age: 28 ID: 77”
            Scanner conin = new Scanner(instr); 
            // Find and display age.
            conin.findInLine(“Age:”); // find Age
            if(conin.hasNext())
                 System.out.println(conin.next());
            else
                  System.out.println(“Error!”);
         }
 }

11.File Read / Write

import java.util.*;
import java.io.*;
public class ScanMixed {
public static void main(String args[])
throws IOException {
int i;
double d;
boolean b;
String str;
// Write output to a file.
FileWriter fout = new FileWriter(“test.txt”);
fout.write(“Testing Scanner 10 12.2 one true two false”);
fout.close();
FileReader fin = new FileReader(“Test.txt”);
Scanner src = new Scanner(fin);
while(src.hasNext()) {
if(src.hasNextInt()) {
i = src.nextInt();
System.out.println(“int: ” + i);
}
else if(src.hasNextDouble()) {
d = src.nextDouble();
System.out.println(“double: ” + d);
}
else if(src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println(“boolean: ” + b);
}
else {
str = src.next();
System.out.println(“String: ” + str);
}
}
fin.close();
}
}