Wednesday, December 10, 2014

Java useful program



Swap Numbers without Using Third Variable Java

public class swap elements without third variable 
{
public static void main(string[] args)
{                
int num1 = 10;
int num2 = 20;                 
system.out.println("Before Swapping");
system.out.println("Value of num1 is :" + num1);
system.out.println("Value of num2 is :" +num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;                
system.out.println("Before Swapping");
system.out.println("Value of num1 is :" + num1);
system.out.println("Value of num2 is :" +num2);
  }
}

Swap Numbers Java

public class swap elements
{
public static void main(string[] args)
{            
int num1 = 10;
int num2 = 20;
system.out.println("Before Swapping");
system.out.println("Value of num1 is :" + num1);
system.out.println("Value of num2 is :" +num2);
swap(num1, num2);
}
private static void swap(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
           system.out.println("After Swapping");
system.out.println("Value of num1 is :" + num1);
system.out.println("Value of num2 is :" +num2);              
    }
}

Reverse Number using Java

public class reverse number
{
public static void main(string[] args)
{
int number = 1234;
int reversednumber = 0;
int temp = 0;            
while(number > 0)
{
temp = number%10;
reversedNumber = reversedNumber * 10 + temp;
number = number/10;                      
}             
system.out.println("Reversed Number is: " + reversedNumber);
   }
}

Java Interface

interface int
{
public void sayhello();
}
}
public class Javainterfaceexample implements intexample
{
public void sayhello()
{
system.out.println("Hello Visitor !");
}
public static void main(string args[])
{
javainterfaceexample javainterfaceexample = new javainterfaceexample();
javainterfaceexample.sayhello();
   }
}

Java Factorial Using Recursion

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
public class javafactorialusingrecursion
{       
public static void main(string args[]) throws numberformatexception, ioexception
{          
system.out.println("Enter the number: ");
bufferedreader br=new bufferedreader(new inputstreamreader(system.in));
int a = integer.parseint(br.readline());
int result= fact(a);
system.out.println("Factorial of the number is: " + result);
}
static int fact(int b)
{
if(b <= 1)
return 1;
else
return b * fact(b-1);
   }
}

Java Factorial

public class numberfactorial
{
public static void main(string[] args)
{             
int number = 5;           
int factorial = number;          
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}       
system.out.println("Factorial of a number is " + factorial);
  }
}
  

Java Class

public class javaclassexample
{
private string name;
{
}
public void setname(string n)
{
name = n;
}
public string getname()
{
return name;
}
public static void main(string args[])
{
object-name = new ;
javaclassexample javaclassexample = new javaclassexample();
javaclassexample.setname("visitor");
system.out.println("hello " + javaclassexample.getname());      
  }
}

No comments:

Post a Comment