Skip to main content

Posts

Program to find the power of a number without using inbuilt function.

import java.io.*; class PowerOfANumber {     public static void main(String [] args){         int x=5,y=5;         System.out.println(power(x,y));     }     public static int power(int x , int y){         if(y==0)             return 1;         else if(y%2==0)             return power(x,y/2)*power(x,y/2);         else             return x*power(x,y/2)*power(x,y/2);     } }
Recent posts

Open Text File and Read Words Print and store without repetition

import java.io.*; import java.util.*; class CountNumbersofEachWordInaFile {   private ArrayList<String> readFile(String filename) {   ArrayList<String> records = new ArrayList<String>();   try   {     BufferedReader reader = new BufferedReader(new FileReader(filename));     String line;    while ((line = reader.readLine()) != null)     { for (String retval: line.split("\\s+")){ if(records.contains(retval.toLowerCase())){        continue;    }    else                 records.add(retval.toLowerCase()); }     }     reader.close();     return records;   }   catch (Exception e)   {     System.err.format("Exception occurred trying to read '%s'.", filename);     e.printStackTrace();     return null;   } } public static void main(String a[]) ...

Open Text File and Read Lines and Print Lines On Console

import java.io.*; import java.util.*; class CountNumbersofEachWordInaFile {     private ArrayList<String> readFile(String filename) {   ArrayList<String> records = new ArrayList<String>();   try   {     BufferedReader reader = new BufferedReader(new FileReader(filename));     String line;     while ((line = reader.readLine()) != null)     {       records.add(line);     }     reader.close();     return records;   }   catch (Exception e)   {     System.err.format("Exception occurred trying to read '%s'.", filename);     e.printStackTrace();     return null;   } } public static void main(String a[]) throws Exception { CountNumbersofEachWordInaFile obj = new CountNumbersofEachWordInaFile(); ArrayList<String> list = new ArrayList<String>(); list=obj.readFile("txt.txt"); Iterator<String>...

Write a program to demonstrate the use of Generics ?

import java.util.*; public class UseofGenerics { public static void main(String a[]) { System.out.println("Welcome to the UseofGenerics project!"); ArrayList<String> list = new ArrayList<String>(); list.add("Sarvajeet"); list.add("Gupta"); String s = list.get(1); System.out.println("Hello "+s); //Access whole list using iterator Iterator<String> itr = list.iterator(); while(itr.hasNext()){    System.out.println(itr.next()); } } } http://www.programmr.com/node/163227

Write a program to demonstrate the use of interface ?

import java.io.*; interface Printable{      void print(); } interface Showable(){     void show(); } public class interfaceexample implements Printable,Showable {     public void print(){         System.out.println("Hello");     }         public void show(){         System.out.println("Welcome!!");     } public static void main(String a[]) throws Exception { System.out.println("Welcome to the interfaceexample project!"); interfaceexample obj = new interfaceexample(); obj.print(); obj.show(); } } http://www.programmr.com/Java/interface_example

write a program to find the biggest element in the Array ?

import java.io.*; public class BiggestElementinArray { public static void main(String a[]) throws Exception { System.out.println("Welcome to the BiggestElementinArray project!"); int [] array = {10,2,30,15,5,41}; int largest=array[0]; for(int i=1;i<array.length;i++){    if(array[i]>largest){        largest=array[i];    } } System.out.println(largest); } } http://www.programmr.com/Java/Biggest_Element_in_Array

write a program to print all the prime numbers up to a given number ?

import java.io.*; class primenumber { public static void main(String a[]) throws Exception { System.out.println("Welcome to the primenumber project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Upper Bound Number"); int N = Integer.parseInt(br.readLine()); int i=0,j=0; int flag=1; System.out.print("2\t3\t"); for(i=4;i<N;i++){    for(j=2;j<=Math.sqrt(i);j++){        if(i%j==0){            flag=0;            break;        }    }    if(flag==1){        System.out.print(i+"\t");    }    flag=1; } System.out.println(); } } http://www.programmr.com/Java/prime_number-5