Showing posts with label command line argument. Show all posts
Showing posts with label command line argument. Show all posts

Tuesday, November 4, 2014

, , , ,

Program to find H.C.F. of two numbers using Command Line Argument.



Code Box
---------------------------------------

/*Program to find H.C.F. of two numbers */

class hcf
{
public static void main(String x[])
{

int num1,num2,remainder,dividend,divisor;

num1=Integer.parseInt(x[0]);
num2=Integer.parseInt(x[1]);

System.out.print("\n---------------H.C.F. Calculator --------------------");
System.out.print("\nEntered numbers are: ");
System.out.print("\nFirst number: "+num1);
System.out.print("\nSecond number: "+num2);

if(num1>num2)  //Checker for greater number
{
remainder=num1%num2;
dividend=num2;
}
else{
remainder=num2%num1;
dividend=num1;
}
//If remainder is zero
if(remainder==0)
{
System.out.print("\nH.C.F. of "+num1+" & "+num2+" is : "+dividend);
}
else
{

divisor=remainder;
while(remainder !=0)
{
remainder=dividend%divisor;
dividend=divisor;
divisor=remainder;
}

System.out.print("\nH.C.F. of "+num1+" & "+num2+" is : "+dividend);

}

System.out.print("\n---------------H.C.F. Calculator --------------------");


}
}
Publisher: Brijmohan Lal Sahu - 5:15 AM
, , , ,

Program to find L.C.M. of any two numbers using Command Line argument.

lcm calculator


Code Box

-------------------------------------------
/*Program to find L.C.M. of any two numbers command line argument*/

class lcmc
{
public static void main(String x[]) 
{
int num1,num2,a,b,count,ans,i;
ans=1;

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

System.out.print("\n----------------- L.C.M. Calculator ---------------");
System.out.print("\nEntered first number:"+a);
System.out.print("\nEntered second number:"+b);
num1=a;
num2=b;

//prime factors are 2,3,5,7

System.out.print("\nL.C.M. Factors => ");
for(i=7;i>=2;)
{
count=0;

if(num1%i==0)
{
num1=num1/i;
count=1;
}

if(num2%i==0)
{
num2=num2/i;
count=1;
}

if(count==0)
{
i--;
}
else
{
System.out.print("  "+i);
ans=ans*i;
}
}

//in case of prime number
System.out.print(" "+num1+" "+num2);
ans=ans*num1*num2;

System.out.print("\nL.C.M. of "+a+" & "+b+" is : "+ans);
System.out.print("\n\n-------------------------------------------");
}
}
Publisher: Brijmohan Lal Sahu - 4:58 AM

Sunday, November 2, 2014

, , , ,

Program to find factor of any number using command line argument.





Code Box
------------------------------------

/* Program to find factor of any number using command line argument */

class factor
{
public static void main(String x[])
{
int num,i;
System.out.print("\n---------Factor Finder--------");
num=Integer.parseInt(x[0]);
System.out.print("\nYou have enter :"+num);

System.out.print("\nFactors of "+num+" are : ");
for(i=1;i<num;i++)
{
if(num%i==0)
{
System.out.print("  "+i);
}

}

}
}



Publisher: Brijmohan Lal Sahu - 10:38 PM
, , , ,

Program to print given number is even or odd numbers using conditional operator in command line argument .



Code Box
------------------------------------------

/* Program to print given number is even or odd numbers using 
conditional operator in command line argument */


class evenc
{
public static void main(String x[])
{
int a;
String s;

a=Integer.parseInt(x[0]);

s=(a%2==0)?"a is even":"a is odd";

System.out.print("\n"+s);

}

}
Publisher: Brijmohan Lal Sahu - 3:41 AM
, , , ,

Program to print given number is even or odd numbers using if else in command line argument.




Code Box 
-----------------------------------

/* Program to print given number is even or odd numbers using 
if else in command line argument */


class even
{
public static void main(String x[])
{
int a;

a=Integer.parseInt(x[0]);

if(a%2==0)
{
System.out.print("\n a is Even ");
}
else
{
System.out.print("\n a is Odd ");
}

}

}
Publisher: Brijmohan Lal Sahu - 3:38 AM
, , , ,

Program to print greater number in 3 numbers using if else in command line argument.




Code Box
-----------------------------------------

/* Program to print greater number in 3 numbers using 
if else in command line argument */


class greaterif3
{
public static void main(String x[])
{
int a,b,c;

a=Integer.parseInt(x[0]);
b=Integer.parseInt(x[1]);
c=Integer.parseInt(x[2]);

if(a>b && b>c)
{
System.out.print("\n a is greater a="+a);
}
else
if(b>c)
{
System.out.print("\n b is greater b="+b);
}
else
{
System.out.print("\n c is greater c="+c);
}

}

}
Publisher: Brijmohan Lal Sahu - 3:35 AM
, ,

Program to find area of circle using power function in command line argument.

area of circle

Code Box
--------------------------------------

/*Program to find area of circle using power function in command line argument */

class areacf
{
public static void main(String x[])
{

float a,r;

r=Float.parseFloat(x[0]);

System.out.print("\nRadius of circle is = "+r);

a=3.14f*(float)(Math.pow(r,2));

/* you can also write it as

a=3.14f*(Math.pow(r,2));
But in this case Math.pow(r,2) returns a double value not a
float that cause an error.
*/

System.out.print("\nAread of circle is = "+a);

}
}

Publisher: Brijmohan Lal Sahu - 3:20 AM
, , ,

Program to find area of circle using command line argument.



Code Box
-------------------------------------

/*Program to find area of circle using command line argument */

class areac
{
public static void main(String x[])
{

float a,r;

r=Float.parseFloat(x[0]);

System.out.print("\nRadius of circle is = "+r);

a=3.14f*r*r;

System.out.print("\nAread of circle is = "+a);



}
}

Publisher: Brijmohan Lal Sahu - 3:15 AM
, , ,

Program to add two numbers using command line argument 2

add



Code Box 
---------------------------------

/*Program to add two numbers using command line argument 2 */

class add
{
public static void main(String x[])
{
int a,b,c;

System.out.print("\nzeroth argument :"+x[0]);
a=Integer.parseInt(x[0]);
//zeroth argument in a
System.out.print("\nvalue of a= "+x[0]);


System.out.print("\nfirst argument :"+x[1]);
b=Integer.parseInt(x[1]);
//first argument in b
System.out.print("\nvalue of b= "+x[1]);

c=a+b;

System.out.print("\nSum of a+b= "+c);

}
}

Publisher: Brijmohan Lal Sahu - 3:10 AM
, ,

Program to add two numbers using command line argument.



Code Box
-----------------------------

/*Program to add two numbers using command line argument */

class add
{
public static void main(String x[])
{
int a,b,c,d;

System.out.print("\nzeroth argument :"+x[0]);
a=Integer.parseInt(x[0]);
//zeroth argument in a
System.out.print("\nvalue of a= "+x[0]);


System.out.print("\nfirst argument :"+x[1]);
b=Integer.parseInt(x[1]);
//first argument in b
System.out.print("\nvalue of b= "+x[1]);

c=a+b;

System.out.print("\nSum of a+b= "+c);

d=Integer.parseInt(x[0])+Integer.parseInt(x[1]);
System.out.print("\nSum of arguments is ="+d);

}
}

Publisher: Brijmohan Lal Sahu - 3:05 AM

Saturday, November 1, 2014

, , ,

Diamond Pattern 10

Diamond Pattern 10



Code Box
------------------------------

/* Program to print diamond pattern 10 */


class diam10
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = 1; k <=i; k++)
System.out.print("*");

System.out.println();
}


for (i = n; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print("*");

for (k = 1; k < i; k++)
System.out.print(" ");

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 12:04 PM
, , , ,

Diamond Pattern 9



Code Box
----------------------------------

/* Program to print diamond pattern 9 */


class diam9
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");



for (i = n-1; i >= 0; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = 1; k < i; k++)
System.out.print(k);

System.out.println("");

}



for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(j);

for (k = j; k >=1; k--)
System.out.print(" ");

System.out.println();
}


System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 12:01 PM
, , , , , ,

Diamond Pattern 8

Diamond Pattern 8

Code Box
--------------------------------

/* Program to print diamond pattern 8 */


class diam8
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(j);

for (k = j; k >=1; k--)
System.out.print(" ");

System.out.println();
}


for (i = n-1; i >= 0; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = j; k >= 1; k--)
System.out.print(k);

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:57 AM
, , , ,

Diamond Pattern 7



Code Box
---------------------------------

/* Program to print diamond pattern 7 */


class diam7
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 0; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = j; k >=1; k--)
System.out.print(k);

System.out.println();
}


for (i = n-1; i >= 0; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = j; k >=1; k--)
System.out.print(k);

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:54 AM
, , , , ,

Diamond Pattern 6



Code Box
--------------------------------------------

/* Program to print diamond pattern 6 */


class diam6
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(j);

for (k = j; k >=1; k--)
System.out.print(" ");

System.out.println();
}


for (i = n-1; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print("j");

for (k = j; k >=1; k--)
System.out.print(" ");

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:51 AM
, , , , ,

Diamond Pattern 5



Code Box
-----------------------------

/* Program to print diamond pattern 5 */


class diam5
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 0; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(j);

for (k = j; k>=1; k--)
System.out.print(k);

System.out.println();
}


for (i = n-1; i >= 0; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(j);

for (k = j; k >=1; k--)
System.out.print(k);

System.out.println();

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:48 AM
, , , ,

Diamond Pattern 4


Code Box
--------------------------------------

/* Program to print diamond pattern 4 */


class diam4
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(i);

for (k = 1; k <i; k++)
System.out.print(" ");

System.out.println();
}


for (i = n; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = 1; k < i; k++)
System.out.print(i);

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:45 AM
, , ,

Diamond Pattern 3


Code box
-------------------------------

/* Program to print diamond pattern 3 */


class diam3
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(" ");

for (k = 1; k <=i; k++)
System.out.print(i);

System.out.println();
}


for (i = n; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(i);

for (k = 1; k < i; k++)
System.out.print(" ");

System.out.println("");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}
Publisher: Brijmohan Lal Sahu - 11:42 AM
, , , , ,

Diamond Pattern 2


Code box
-------------------------

/* Program to print diamond pattern 2 */


class diam2
{
public static void main(String x[])
{

int i, j, k,n;

n=Integer.parseInt(x[0]);

System.out.println("---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");
for (j = 1; j <= i; j++)
System.out.print(i);

for (k = 1; k <i; k++)
System.out.print(i);

System.out.println();
}


for (i = n-1; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
System.out.print(" ");

for (j = 1; j <= i; j++)
System.out.print(i);

for (k = 1; k < i; k++)
System.out.print(i);

System.out.println(" ");

}
System.out.println(" ");
System.out.print("\n----------------------------------\n");

}

}


Note:Use Command line argument method to run the program.
Publisher: Brijmohan Lal Sahu - 11:39 AM