Posts

Java program for printing the below output

import java.util.Scanner; public class PracticeDemo { public static void getNumber(int number){ int middle=1; String start="1",end="1",output; System.out.println(start); for(int i=1;i<=number;i++){ middle=middle*2; output=start+" "+middle+" "+end; System.out.println(output); start=start+" "+middle; end=middle+" "+end; } } public static void main(String [] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int x=sc.nextInt(); getNumber(x); sc.close(); } } Ouput: Enter the number 10 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 16 8 4 2 1 1 2 4 8 16 32 64 32 16 8 4 2 1 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1 1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1 1 2 4 8 16 3...

Collections(Java)-Map Interface example program

import java.util.HashMap; import java.util.TreeMap; import java.util.Map; import java.util.Iterator; import java.util.Map.Entry; import java.util.Hashtable; public class CollectionsMapDemo { public static void main(String [] args){ HashMap<Object,Object> map=new HashMap<Object,Object>(); map.put(1, "filter"); map.put("a", "claim"); map.put("3", "smart"); map.put(null,"filter"); map.put(null,"filter"); map.put(4, "sentinal"); map.put(4, "sentinal"); if(!map.isEmpty()){ Iterator<Entry<Object, Object>> it=map.entrySet().iterator(); System.out.println("HashMap can have one null key with multiple values \n"); while(it.hasNext()){ Map.Entry<Object,...

Collections(Java)-Comparator example program

Employees.java import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class Employees { String name; int id; //setters public void setName(String name){ this.name=name; } public void setId(int id){ this.id=id; } //getters public String getName(){ return name; } public int getId(){ return id; } //constructor public Employees(String name,int id){ this.name=name; this.id=id; } public static void main(String [] args){ Employees e1=new Employees("xyz",37); Employees e2=new Employees("abc",38); Employees e3=new Employees("sst",39); ArrayList<Employees> arrayList=new ArrayList<Employees>(); arrayList.add(e1); arrayList.a...

Collections(Java)-Comparable example program

import java.lang.Comparable; public class ComparableDemo { public static void main(String[] args) { String str1="Comparable"; String Str2="Comparator"; int value=str1.compareTo(Str2); if(value==0){ System.out.println("Strings are equal"); } else{ System.out.println("Strings are not equal"); } Integer obj1=new Integer("37"); Integer obj2=new Integer("38"); int retValue=obj1.compareTo(obj2); if(retValue>0){ System.out.println("Obj1 is greater than obj2"); }else if(retValue<0){ System.out.println("Obj1 is less than obj2"); } else{ System.out.println("Obj1 and Obj2 are equal"); } } } Ou...

Array List example programs

  How to Iterate ArrayList using Java ListIterator Example    Arraylist add element at specific index   Sort ArrayList in descending order Java program to insert an element to ArrayList using ListIterator Example    How to sort arraylist of strings alphabetically java   Java Program to Sort elements of Java ArrayList Example Basic Java example program to replace an element at specified index arraylist Search an element of Java ArrayList Example Java Example Program to Remove element from specified index ArrayList import java.util.ArrayList; import java.util.Collections; import java.util.ListIterator; import java.util.Comparator; import java.util.Scanner; public class CollectionLists { public static void main(String [] args){ ArrayList<String> arrayList=new ArrayList<String>(); //adding elements to list arrayList.add("A"); arrayList.add("B"); ...

Jail Broke out program.

Here I have written an assumption scenario, where a prisoner broke out the jail, This program show an approximate assumption how he crossed the two slippery walls of jail, one is of 9 meters and other as 10 meters. public class JailBrokeOut { public static void getJumpCount(int input, int input2){ int attempt=0,jumped=5,slide=1,result=0; System.out.println("Wall length :"+input+" meters \n"); if(input==9){do{ attempt++; System.out.println(+attempt+ " Attempt \n"); result+=jumped-slide; System.out.println("Jumped:"+jumped+ " meters"); System.out.println("Slided: "+slide+" meters"); slide--; System.out.println("Climbed :"+result+" meters \n"); if(input==result){ ...

Factorial program in Java

import java.util.Scanner; public class Factorial{ public static void findFactorial(int number){ int factorial=1; do{ factorial*=number; number--; }while(number>=2); System.out.println(factorial); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int number=sc.nextInt(); findFactorial(number); sc.close(); } } Output: Enter the number : 4 24