Example : A simple Java program to display message .
class Message 
{
  public static void main(String[] args)
  //public static void main(String []args)
  //public static void main(String args[])
  //public static void main(String...args)
  //static public void main(String[] args)
  //public static final void main(String[] args)
  //final public static void main(String[] args) 
  {
    System.out.println("Welcome U All in Codershelpline");
  }
}

Output :
Welcome U All in Codershelpline
Example : A simple Java addition program to display the output of given static or constant values .
class Simple
{
  public static void main(String[] args) 
  {
    int x,y,z,c;
    x=50;y=60;z=15;    
    c=x+y+z;
    System.out.println(c);      // System.out.println(x + y + z);
  }
}

//-------------  OR  ----------------


class Simple
{
  public static void main(String[] args) 
  {
    int x = 50, y = 60, z = 15,c;    
    c=x+y+z;
    System.out.println(c);    // System.out.println(x + y + z);
  }
}


//-------------  OR  ----------------

class Simple
{
  public static void main(String[] args) 
  {
    int x = 50;
    int y = 60;
    int z = 15;

    int c;    
    c=x+y+z;
    System.out.println(c);     // System.out.println(x + y + z);
  }
}

//-------------  OR  ----------------

class Message 
{
  public static void main(String[] args) 
  {    
	float f1=720.00000f;
	float f2=302.00000f;

	float sum=f1+f2;

	System.out.println("The addition result are ");
        System.out.println(sum);
	System.out.println(f1+f2);
	System.out.printf("%.3f", sum);
  }
}

Output : 
The addition result are
1022.0
1022.0
1022.000

//-------------  OR  ----------------

class Message 
{
  public static void main(String[] args) 
  {	
	float c = (1.1345 + 2.2765) ;    // Error generated.

        float c = (float) (1.1345 + 2.2765) ;

	System.out.println("The addition result are : ");	
	System.out.printf("%.3f", c);	
  }
}

Output : 
The addition result are :
3.411
Example : A simple Java program to calculate given constant float values.
class Message 
{
  public static void main(String[] args) 
  {    
	float f1=720.00000f;
	float f2=302.00000f;

	float sum=f1+f2;

	System.out.println("The addition result are ");
        System.out.println(sum);
	System.out.println(f1+f2);
	System.out.printf("%.3f", sum);
  }
}

Output : 
The addition result are
1022.0
1022.0
1022.000

//-------------  OR  ----------------

class Message 
{
  public static void main(String[] args) 
  {	
	//   float c = (1.1345 + 2.2765) ;    Incorrect format & hence Error will generated.

        float c = (float) (1.1345 + 2.2765) ;    //correct format
	System.out.println("The addition result are =");	
	System.out.printf("%.3f", c);	
  }
}

Output : 
The addition result are =
3.411
Example : A simple Java program to calculate given dynamic values.
import java.io.*;
import java.util.*;
class Datainput   
{
	public static void main(String[]args)
	{
		Scanner Sc=new Scanner(System.in);
		int a,b,c;
		float d;
		
		System.out.println("Enter First no.");
		a=Sc.nextInt();
		System.out.println("Enter Second no.");
		b=Sc.nextInt();
		System.out.println("Enter Third no.");
		c=Sc.nextInt();
		d=(a+b+c)/3;
		System.out.println(d);  //output format = 30.0
		
                System.out.println();
                d=(float)(a+b+c)/3;
		System.out.println(d);	 //output format = 30.1420		
	}
}
Example : A Java program to find ASCII value of a given character .
import java.io.*;
class Simple
{
   public static void main(String[] args) 
   {
        char ch1 = 'M';
	char ch2 = '*';
        System.out.println("ASCII value of "+ch1+" is: " + (int)ch1);
        System.out.println("ASCII value of "+ch2+" is: " + (int)ch2);
    }
}

Output :
ASCII value of M is: 77
ASCII value of * is: 42
Example : A Java program to find ASCII Character from a given ASCII Value .

import java.io.*;
class Simple
{
   public static void main(String[] args) 
   {
        int x = 65;
	int y = 90;

        System.out.println("ASCII character of "+x+" is: " + (char)x);
        System.out.println("ASCII character of "+y+" is: " + (char)y);
    }
}

Output:

	ASCII character of 65 is: A
	ASCII character of 90 is: Z
Example : A Java program to find Largest/Smallest value between two values along with the square root value.
import java.io.*;
class Example 
{
  public static void main(String[] args) 
  {
    System.out.println(Math.max(37,15));
    System.out.println(Math.min(37,15));
    System.out.println(Math.sqrt(625));	
  }
}

Output :
37
15
25

//-------------  OR  ----------------

import java.util.Scanner;
class Simple
{  
  public static void main(String[] args) 
  {
    int num1,num2;
	
	Scanner sc= new Scanner(System.in);	
	System.out.println("Enter two integer values");
	num1=sc.nextInt();
	num2=sc.nextInt();
	
	System.out.println("The output are ");
	System.out.println(Math.max(num1,num2));
        System.out.println(Math.min(num1,num2));
	System.out.println();

	System.out.println(Math.sqrt(num1));
        System.out.println(Math.sqrt(num2));	
  }
}

Output :
Enter two integer values
625
121
The output are 
625
121

25.0
11.0
Example : A Java program to read/accept values from the user at run time through keyboard of different types using Scanner class and display all the data.
import java.io.*;
import java.util.*;     //import java.util.Scanner;
class Datatype
{
	public static void main(String args[])
	{
		int num;
                long val;		
		float amt;                
		double total;
                char ch;
		String str;
 
		Scanner sc= new Scanner(System.in);

		System.out.println("Enter an Integer Value");
		num=sc.nextInt();

                System.out.println("Enter a large integer Value");
		val=sc.nextLong();
                
                System.out.println("Enter a Float Value");
		amt=sc.nextFloat();                

                System.out.println("Enter a Double Value");                
		total=sc.nextDouble();

                System.out.println("Enter a Single Character Value");
                ch=sc.next().charAt(0);

                System.out.println("Enter a String Value");
		str=sc.next();
                //str=sc.nextLine();

                System.out.println();
                System.out.println("The output of various datatypes are");

		//System.out.println(num);
		//System.out.println(val);
		//System.out.println(amt);
		//System.out.println(total);
                //System.out.println(ch);
		//System.out.println(str);

                System.out.println("Integer value is : " + num + "\n" + "Long value is  : " + 
                val + "\n" + "Float value is  : " + amt + "\n" +"Double value is : "+total 
                +"\n"+"Single Character is : "+ ch +"\n"+"String value is : "+str);
       }
}
Example : A Java program to read/accept values from the user at run time through keyboard of different types using DataInputStream class and display all the data.
import java.io.*;
class Datainput 
{    
    public static void main(String[] args)throws Exception
    {
        int num;
        long lg;
        float val;
	double dbl;
        char ch;
        String str;
        
        DataInputStream dis=new DataInputStream(System.in);
        
		System.out.println("Enter Integer value:");
		num=Integer.parseInt(dis.readLine());
		System.out.println(num);
		
		System.out.println("Enter Long value:");
		lg=Long.valueOf(dis.readLine()).longValue();
		System.out.println(lg);
		
		System.out.println("Enter Float value:");
		val=Float.valueOf(dis.readLine()).floatValue();
		System.out.println(val);
		
		System.out.println("Enter Double value:");
		dbl=Double.valueOf(dis.readLine()).doubleValue();
		System.out.println(dbl);
		
		System.out.println("Enter CharacterString value:");
		ch=(char)dis.read();
		//ch=dis.readLine().charAt(0);
		System.out.println(ch);
		
		System.out.println("Enter String value:");
		str=dis.readLine();
		System.out.println(str);		
    }    
}	  
Example : A Java program to read/accept values from the user at run time through keyboard of different types using Buffered Reader class and display all the data.
import java.io.*;
class Buffer
{    
    public static void main(String[] args)throws Exception
    {
        int num;
        long lg;
        float val;
	double dbl;
        char ch;
        String str;
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
		System.out.println("Enter Integer value:");
		num=Integer.parseInt(br.readLine());
		System.out.println(num);
		
		System.out.println("Enter Long value:");
		lg=Long.valueOf(br.readLine()).longValue();
		System.out.println(lg);
		
		System.out.println("Enter Float value:");
		val=Float.valueOf(br.readLine()).floatValue();
		System.out.println(val);
		
		System.out.println("Enter Double value:");
		dbl=Double.valueOf(br.readLine()).doubleValue();
		System.out.println(dbl);
		
		System.out.println("Enter Character value:");
		ch=(char)br.read();
		//ch=br.readLine().charAt(0);
		System.out.println(ch);
		
		System.out.println("Enter String value:");
		str=br.readLine();
		System.out.println(str);		
    }    
}
Example : A Java program to read/accept values from the user at run time through keyboard using command line arguments and display all the data.
import java.io.*;
class Simple
{
   public static void main(String args[])
    {
      String g1=args[0];
      String g2=args[1];
  
      //int num1=Integer.parseInt(g1);
      //int num2=Integer.parseInt(g2);
      
      //Long num1=Long.parseLong(args[0]);
      //Long num2=Long.parseLong(args[1]);
	  
      //Float num1=Float.parseFloat(args[0]);
      //Float num2=Float.parseFloat(args[1]);
	  
      //Double num1=Double.parseDouble(args[0]);
      //Double num2=Double.parseDouble(args[1]);      
  
      System.out.println(num1+num2);
    }
 }

Output :

E:\>javac Simple.java
E:\>java Simple 20 25
45
E:\>

NB :
Here input is given exactly at interpreted time after compilation with space i.e.java Cmdinput 20 25 and then press enter that gives the result 70.
Example : A Java program to read/accept values from the user at run time through keyboard using console method and display all the data.
import java.io.*;
class Console
{
	public static void main(String args[])
	{
	  String name=System.console().readLine();
	  System.out.println(name);  
	}
}

/* --------------------  OR  ---------------------*/
import java.io.*;
class Console
{
	public static void main(String args[])
	{
		Console con=System.console();    
		System.out.println("Enter your string: ");    
		String str=con.readLine();    
		System.out.println(str);    
	}
}
Example : A Java program to calculate simple interest of given values.
import java.io.*;
import java.util.Scanner;

class Simple
{
   public static void main(String args[]) 
    {
        float p, r, t, si;

        Scanner Sc = new Scanner(System.in);
        System.out.print("Enter Principal value : ");
        p = Sc.nextFloat();

        System.out.print("Enter rate value : ");
        r = Sc.nextFloat();

        System.out.print("Enter time value : ");
        t = Sc.nextFloat();

        Sc.close();
        si = (p * r * t) / 100;

        System.out.print("Simple Interest is: " +si);
    }
}

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.