Friday, August 16, 2013

Java Program 005

Write a program DisplayPattern1.java, which accepts int value as arguments and print the Following Output:
Ex: Input: 12
Output: 
1   2   3   4
5   6   7   8
9  10  11 12


// Author : J. Srivastav Reddy
// Date : 30-7-2013

public class DisplayPattern1
{ public static void main(String[] args)
{ int endValue = Integer.parseInt(args[0]);
int j = 1;
int temp = 1;

for(int i = 1;j <= endValue;i++)
{
for(;j <= temp + 3;j++)
System.out.print(j + " ");
temp = j;

System.out.println("");
}
}
}

Java Program 004

write a Program DisplayNumbersDemo1.java, which accepts Three int values as command line arguments (Start value End value and Increment value) and print the number between the given Numbers.

Ex:    Input: 10, 30, 2

Output: 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30

// Author		: J. Srivastav Reddy
// Date : 30-7-2013

public class DisplayNumbersDemo1
{
public static void main(String[] args)
{
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);

for(;start <= stop;)
{
System.out.print(start + " ");
start += Integer.parseInt(args[2]);
}
}
}

Friday, August 9, 2013

Java Program 003

Write a program to print sum of all even numbers between given numbers.


// Author : J. Srivastav Reddy
// Date : 30-7-2013

public class SumOfEvenNumbersInBetween
{
public static void main(String[] args)
{
int start = Integer.parseInt(args[0]) + 1;

int
stop = Integer.parseInt(args[1]);
                int sum = 0;
for (; start < stop; start ++)
{
if(start %2 == 0)
sum = sum + start;
}

System.out.println(" Sum of Even No in given Range is " + sum);

}
}

Java Program 002

Write a program DisplayNumbers.java, which accepts two int values as command line arguments (Start value and End value) and print the numbers between the given Numbers.


// Author J. Srivastav Reddy
// Date 30-7-2013

public class DisplayNumbers
{
public static void main(String[] args)
{
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);

for (; start < stop; start ++)
System.out.print(start + " ");
}
}

Java Program 001

Write solution for java code for counting the number of numbers, reading one at a time. If the given no is negative, count can be stopped and program exit.

// Author   J. Srivastav Reddy
// Date     30-7-2013

public class CountingNumbers
{
    public static void main(String args[])
    {
        int count = 0;
               
        for(int i=0; i<args.length; i++ )
        {
            int num=Integer.parseInt(args[i]);
                if(num > 0)
                    count++;
                else
                    break;
        }
        System.out.println(" Numbers are " + count);
    }
}