Sunday, March 29, 2020

Print binary tree pattern in Java

To print a binary pattern in java is simple thing. its same like printing stars the only logic changes is here we have to print 1 or 0 instead of star. and thats also very easy. if we observe when (i+j%2==0) that time we have to print 1 else 0.
public class pattern2 {
 public static void main(String[] args) {



  for(int i=0;i<=4;i++)
  {
   for(int j=0;j<=i;j++)
   {
    if((i+j)%2==0)
    {
     System.out.print(1);
    }
    else
    {
     System.out.print(0);
    }
   }
   System.out.println();
  }
 }
}
Output:-
1
01
101
0101
10101

Reverse Pyramid star patterns in java

To print  reverse pyramid pattern  in java is same like we do print pyramid in increment size. the only think change here is , here we run i ( row) loop in reverse manner.
public class Pattern1 {

 public static void main(String[] args) {
  
  for(int i=4;i>=0;i--)
  {
   for(int j=0;j<=i;j++)
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}
OUTPUT:-
 *****
 ****
 ***
 **
 *


To check weather given number is prime or not in Java.

To check weather number is prime or not is simple. we need to take a number as a input and than we need run a for loop till the number-1 time and module each of the for loop variable with the number given. so by that if we get module = 0 , which means number can be devide and hence number is ot prime. 
import java.util.Scanner;

public class Primenumbers {

 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.println("enter the number->");
  int num=sc.nextInt();
  int flag=0;


  if(num==0||num==1)
  {
   System.out.println("please enter the valid number");
  }
  else
   for(int i=2;i<num/2;i++)
   {
    if(num%1==0)
    {
     System.out.println("not prime");
     flag=1;
     break;
    }
   }

  if(flag==0)
  {
   System.out.println("prime");
  }


 }

}

To Print occurence of a character in given String in java.

To print Occurrence of  a given character is a simple thing. We need compare each character of a given string with the character we need to compare. and if both matches than we have to increase the counter by 1.
import java.util.Scanner;

public class Accurence_char {
 
 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.print("enter the string:--");
  String str=sc.next();
  char check='o';
  int count=0;
  
 for(int i=0;i<str.length();i++)
 {

  if(str.charAt(i)==check)
  {
   count++;
  }
  
  
 }
 
 System.out.println("occurence of "+check+" in given string is:"+count);
 
 }
 

}

Find a factorial of a given number | Basic java programs for interview for Test automation engineer

To find a factorial of a given number is very simple. For exp if we have to find factorial of 5 than we have to (5*4*3*2*1) here we are doing multipication of a number and print sum of it.

import java.util.Scanner;

public class factorial {

 public static void main(String[] args) {


  Scanner sc= new Scanner(System.in);
  System.out.println("enter the number: ");

  int num=sc.nextInt();

  int sum=1;

  for(int i=num;i>=1;i--)

  {
   sum=sum*i;
   if(i==1)
   {
    System.out.println("factorial of"+" "+num+" "+ "is-->" +" "+sum);
   }

  }
 }


}

Print Fibonacci series of a number in java | Basic java program for Test automation engineer interview

Fibonacci series is 1,1,2,3,5,8,13,21,34,55,etc..

Here simple logic is Take the first number a=1,b=1, and do sum of that c=a+b.
and after that replace the value of a and b.

Lets look at the code.
import java.util.Scanner;

public class Fibonaci {
 /* 1 1 2 3 5 8 13 21 34
  * 
  * 
  * 
  */

 public static void main(String[] args) {

  int a=1;
  int b=1;
  int c = 0;
  Scanner sc=new Scanner(System.in);
  System.out.println("enter the number till fibonaci series you want to print: ");
  int num=sc.nextInt();
  System.out.print(a+" "+b);

  while(c<num)
  {
   c=a+b;

   System.out.print(" "+c+" ");

   a=b;
   b=c;


  }

 }
}

To check weather give string is palindrom or not | Basic java programs for Interview for Test automation engineer

1) To check weather a given string is palindrome or not in java.

Palindrome string a string which is same while reading from forward to backword or backword to forword (Ex. madam,lo,pop,rubber)

-To do that that there is very simple logic in java. 
1)We need to take a length of the string
2)Reverse a string
3)compare both the string



import java.util.Scanner;

public class Reversestring {

 public static void main(String[] args) {

  Scanner sc= new Scanner(System.in);
  System.out.println("enter the string: "); 
  String str=sc.next(); 
  int length=str.length();

  String s2="";

  for(int i=length-1;i>=0;i--)

   s2=s2+str.charAt(i);

  if(s2.equalsIgnoreCase(str))
  {
   System.out.println("palindrome");

  }
  else {
   System.out.println("not palindrom");
  }

 }

}