Social Icons

Friday, 23 August 2013

Generating Random No. In java

import java.util.Random;
class Run
{

public static void main(String args[])
{
 int i;
 Random d = new Random(3);
 Random d1 = new Random(0);
 for(i=0;i<10;i++)
 {
 System.out.println("Print Random value having seed 3  " + d.nextInt(1000));
System.out.println("Print Random value having seed 0 : "+d1.nextInt(1000));
 }
 }

}

output:

Print Random value having seed 3  734
Print Random value having seed 0 : 360
Print Random value having seed 3  660
Print Random value having seed 0 : 948
Print Random value having seed 3  210
Print Random value having seed 0 : 29
Print Random value having seed 3  581
Print Random value having seed 0 : 447
Print Random value having seed 3  128
Print Random value having seed 0 : 515
Print Random value having seed 3  202
Print Random value having seed 0 : 53
Print Random value having seed 3  549
Print Random value having seed 0 : 491
Print Random value having seed 3  564
Print Random value having seed 0 : 761
Print Random value having seed 3  459
Print Random value having seed 0 : 719
Print Random value having seed 3  961
Print Random value having seed 0 : 854

Printing Date in java Program

import java.util.Date;
class New
{

public static void main(String args[])
{

 Date d = new Date();

 System.out.println("Time in millisecond is from 1 jun 1970 " + d.getTime());
System.out.println("Current Date and Time is : "+d.toString());
 }


}

output:

   e:\java_programs>java New
Time in millisecond is  1377282070857
Current Date and Time is : Fri Aug 23 23:51:10 IST 2013

Command line Input Java Program


class New
{
public static void main(String args[])
{

 int a = Integer.parseInt(args[0]);
 int b= Integer.parseInt(args[1]);

 int c=a+b;
 System.out.println("Addition of two no . is  "+c);
}


}


output:

               

e:\java_programs>java New 5 6
Addition of two no . is  11


Java Program Using Scanner class to take Input from user

import java.util.Scanner;
class New
{
public static void main(String args[])
{
 Scanner s= new Scanner(System.in);
 System.out.println("Enter an integer value ");

 int a = s.nextInt();
 System.out.println("Enter 2nd integer");
 int b= s.nextInt();
 int c=a+b;
 System.out.println("Addition of two no. is "+c);
}


}


output:


      e:\java_programs>java New
Enter an integer value
25
Enter 2nd integer
36
Addition of two no. is 61
 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management