Posts

Showing posts from May, 2017

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

Fibonacci series in java

www.java67.com In mathematics, the  Fibonacci numbers  or Fibonacci series  or  Fibonacci sequence  are the numbers  in the following integer  sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144... By definition, the first two  numbers  in the  Fibonacci sequence  are 0 and 1, and each subsequent number is the sum of the previous two. import java.util.Scanner; public class PracticeDemo { public static void getFibonacciSeries(int number){ int count=2,first=0,second=1,third=0; System.out.print(first+" "+second); if(number>0) do{ third=first+second; first=second; second=third; count++; System.out.print(" "+third); }while(count<number); } public static void main(String [] args){ Scanner sc=new Scanner(Sy...

Write Java program to check if a number is Armstrong number or not?

An Armstrong number of 3 digit is a number for which sum of cube of its  digits  are equal to number e.g. 371 is an Armstrong number because  3*3*3 + 7*7*7 + 1*1*1 = 371 ).  import java.util.Scanner; public class ArmstrongNumber{ public static void checkArmstrongNumber(int value){ int armstrongNumber=getArmstrongNumber(value); if(value==armstrongNumber){ System.out.println("The given number is armstrong number"); }else{ System.out.println("The given number is not armstrong number"); } } public static int getArmstrongNumber(int number){ int remainder=0,reverse=0; do{ remainder=number%10; number=number/10; reverse+=remainder*remainder*remainder; }while(number>10); int armstrongValue=reverse+(number*number*number); ...

Java program to check number is palindrome or not.

For those who are not familiar with palindrome numbers, palindrome number is a number which is equal to reverse of itself. For example 121 is a palindrome because reverse of 121 is 121, while 123 is not a palindrome in Java because reverse of 123 is 321 and  121!=321 . import java.util.Scanner; public class Palindrome{ public static void checkPalindrome(int number){ int reverseNumber=reverseNumber(number); if(number==reverseNumber){ System.out.println("The given number is palindrome"); }else{ System.out.println("The given number is not palindrome"); } } public static int reverseNumber(int number){ int remainder=0,reverse=0; do{ remainder=number%10; number=number/10; reverse=remainder+(reverse*10); }while(number>0); return reverse; } ...

Get the list of Curious numbers in java.

What is Curious number? Seriously, I just had come up with this name Curious because it got me curious while work on some project. The below method may have different name, but not aware of that name, so I myself name it as curious. So, let's jump into this method. let us take number 9. 9x1=9        0+9=9 9x2=18      1+8=9 9x3=27      2+7=9 9x4=36      3+6=9 9x5=45      4+5=9 9x6=54      5+4=9 9x7=63      6+3=9 9x8=72      7+2=9 9x9=81      8+1=9 .... .. ... import java.util.Scanner; public class CuriousNumber{ public static void getCuriousNumberList(int value){ for(int j=1;j<=value;j++){ checkCuriousNumber(j); } } //method to check entered number is cyclic or not public static void checkCuriousNumber(int number){ int multiplier=...

Write a Java method to count all vowels in a string.

import java.util.Scanner; public class FindVowels{ public static int getVowels(String word){ String [] vowels={"a","A","e","E","i","I","o","O","u","U"}; int count=0; for(int i=0;i<vowels.length;i++){ String a=vowels[i]; for(int j=0;j<word.length();j++){ String b=String.valueOf(word.charAt(j)); if(a.equals(b)){ count++; } } } return count; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String word=sc.next(); System.out.println("There are "+getVowels(word)+" vowels in String: "+word ); ...

Write a Java method to display the middle character of a string.

import java.util.Scanner; public class MiddleLetter{ public static void getMiddleCharacter(String value){ int length=value.length(); int remainder=0; if(length%2==0){ remainder=length/2; int priorLetter=remainder-1; System.out.println(String.valueOf(value.charAt(priorLetter))+String.valueOf(value.charAt(remainder))); }else{ remainder=length/2; System.out.println(value.charAt(remainder)); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String word=sc.next(); getMiddleCharacter(word); sc.close(); } } Output: Enter the string umapathi pa

Find the greatest number based on the user input in Java

import java.util.Scanner; public class FindHighestNumber{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the First Number : "); int firstNumber=sc.nextInt(); System.out.println("Enter the Second Number : "); int secondNumber=sc.nextInt(); System.out.println("Enter the Third Number : "); int thirdNumber=sc.nextInt(); int [] array={firstNumber,secondNumber,thirdNumber}; int arrayValue=0; int arrayMatch=0; int count=0; for(int i=0;i<array.length;i++){ arrayValue=array[i]; for(int j=0;j<array.length;j++){ arrayMatch=array[j]; if(arrayValue>arrayMatch){ count++; } } ...

How to find largest element in an array with index and value in Java

public class FindHighestElement{ public static void main(String[] args) { int [] array={1,9,11,4,3}; int arrayValue=0; int arrayMatch=0; int count=0; for(int i=0;i<array.length;i++){ arrayValue=array[i]; for(int j=0;j<array.length;j++){ arrayMatch=array[j]; if(arrayValue>arrayMatch){ count++; } } if(count==(array.length-1)){ System.out.println("The highest Value : "+arrayValue); System.out.println("The index is : " +i); } count=0; } } } Output: The highest Value : 11 The index is : 2

Reverse number in Java

import java.util.Scanner; public class ReverseNumber{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the number: "); int reverse=0; int remainder=0; int number=sc.nextInt(); do{ remainder=number%10; reverse=remainder+(reverse*10); number=number/10; }while(number>0); System.out.println(reverse); sc.close(); } } Output: Enter the number: 1234 4321.

Print 1 to 10 without using loop in java?

public class PrinWithoutLoop{ public static void print(int n){ if(n<=10){ System.out.println(n); print(n+1); } } public static void main(String [] args){ print(1); } } Output: 1 2 3 4 5 6 7 8 9 10

Check the number as Even or Odd without using % or / operator in Java.

import java.util.Scanner; public class EvenOdd{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the number: "); int number=sc.nextInt(); if((number &1)==0){ System.out.println("Even number"); } else{ System.out.println("Odd number"); } } } Output: Enter the number: 6 Even number.