Monday, September 16, 2013

Java Program 010

Write a program DisplayPattern2.java, which accepts a number as argument and print the following output:

Ex. Input : 5

Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

public class DisplayPattern2
{
    public static void main(String args[])
    {
        int end = Integer.parseInt(args[0]);
        for(int i = 1; i <= end; i++)
        {
            for(int j = 1; j <= i; j++)
                System.out.print(j + " ");
            System.out.println("");
        }
    }
}

Wednesday, September 11, 2013

Java Program 009

Create a file ArithmeticTest.java which accepts three arguments- two int values and one Operator (any one of +, –, *) and Print The Following Output:

Output: 2 + 3 = 5

 

// Code NOT Tested !!

public class AdditionTest{
	public static void main(String[] args){
		int n1 = Integer.parseInt(args[0]);
		int n2 = Integer.parseInt(args[1]);
		String operator = args[2];
		
		if (operator.equals("*"))
			System.out.println(n1 + " * " + n2 + " = " + (n1 * n2));
		else if (operator.equals("+"))
			System.out.println(n1 + " + " + n2 + " = " + (n1 + n2));
		else if (operator.equals("-"))
			System.out.println(n1 + " - " + n2 + " = " + (n1 - n2));
		else
			System.out.println(" Use a Correct Third Operator");
		
		System.out.println(" Bye,");
	}
}		

Blogger Labels: Java,Program,ArithmeticTest,arguments,Operator,Print

Java Program 008

Write a program TestDemo.java which accepts the number as argument and print ” You Have Entered Zero ” if the Value is equal to 0. It should print “You have entered Positive value ” if the value is greater than 0. It should print “You have entered negative value ” if the Value is less than Zero.

public class TestDemo
{
	public static void main(String[] args)
	{
		int n1 = Integer.parseInt(args[0]);
		if(n1 == 0)
			System.out.println("You have entered ZERO ");
		else if (n1 < 0)
			System.out.println("You have entered Negetive value ");
		else 
			System.out.println("You have entered positive value ");
	}
}
Blogger Labels: Simple codes,Java,Program,argument

Java Program 007

Write java Code For printing Smallest of two given floating point numbers.

public class SmallestOfTwo
{
    public static void main(String[] args)
    {
        Float n1=Float.parseFloat(args[0]);
        Float n2=Float.parseFloat(args[1]);
        
        if(n1<n2)
            System.out.println("Smallest no. is " + n1);
        else
            System.out.println("Smallest no. is " + n2);
            
        System.out.println("Bye,.");
    }
}

Java Program 006

Write a Java Program to print This Pattern.

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25

  1: public class DisplayPattern10
  2: {
  3: 	public static void main(String[] args)
  4: 	{
  5: 		int endValue = Integer.parseInt(args[0]);
  6: 		
  7: 		for(int i = 1; i <= endValue; i ++)
  8: 		{
  9: 			for(int j = i; j <= (i * i); j = j + i)
 10: 			System.out.print(j + " ");
 11: 
 12: 		System.out.println("");
 13: 		}
 14: 	}
 15: }