Showing posts with label Simple Codes. Show all posts
Showing posts with label Simple Codes. Show all posts

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("");
        }
    }
}

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);
    }
}