Sunday, 15 May 2016

Java code to check whether a number is power of two


import java.util.Scanner;

public class CheckPowerTwo {
   
     
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
   
    for( i=0;i<a;i++)
    {
    if(a==Math.pow(2,i))
    { System.out.println("The number is power of two");break;
    }
   
    }
    if(i==a)
        System.out.println("The number is not power of two");
   
   
    }
   
}

Reading a file using FileInputStream in java

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}