Showing posts with label Flow control in java. Show all posts
Showing posts with label Flow control in java. Show all posts

Tuesday, November 4, 2014

, , , ,

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 print greater number in 2 numbers using conditional operator in command line argument.



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

/* Program to print greater number in 2 numbers using 
conditional operator in command line argument */


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

String s;

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


s=(a>b)?"a is greater a="+a:"b is grater b="+b;

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

}

}
Publisher: Brijmohan Lal Sahu - 3:26 AM

Sunday, September 22, 2013

, , , , , ,

Program to iluustrate use of Break & Continue in java


Code Box
--------------------
/*Program to print table of all odd numbers less than 10. Useing flowcontrol
terms  Break & Continue. */

class flowbc
{
public static void main(String r[])
{
int a,b;

for(a=1;;a++)
{

if(a>=10)
{ break;
}
else
if(a%2==0)
{continue;
}
else
{
for(b=1;b<=10;b++)
{
System.out.print("  "+b*a);
}

}

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


Publisher: Brijmohan Lal Sahu - 3:41 PM