Showing posts with label if else. Show all posts
Showing posts with label if else. Show all posts

Tuesday, November 4, 2014

, , , ,

Program to find H.C.F. of two numbers using BufferedReader.


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

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

import java.io.*;

class hcfb
{

public static void main(String x[]) throws IOException
{
int num1,num2,remainder,dividend,divisor;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n---------------H.C.F. Calculator --------------------");
System.out.print("\nEnter two number: ");


System.out.print("\nEnter First number: ");
num1=Integer.parseInt(obj.readLine());

System.out.print("\nEnter Second number: ");
num2=Integer.parseInt(obj.readLine());

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:17 AM
, , , ,

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 BufferedReader.



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

/*Program to find L.C.M. of any two numbers using BufferedReader */

import java.io.*;
class lcm
{
public static void main(String x[]) throws IOException
{
int num1,num2,a,b,count,ans,i;
ans=1;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n----------------- L.C.M. Calculator ---------------");
System.out.print("\nEnter first number:");
a=Integer.parseInt(obj.readLine());
System.out.print("\nEnter second number:");
b=Integer.parseInt(obj.readLine());
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 - 5:05 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 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 print greater number in 2 numbers using if else in command line argument.



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

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


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

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

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

}

}
Publisher: Brijmohan Lal Sahu - 3:32 AM

Sunday, September 22, 2013

, , , , ,

Program for Character case converter.


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


/* program to Check in put character is Lower or upper case and convert them into other */
class uporlo
{
public static void main(String a[])
{

int i;
char c;

c=a[0].charAt(0);
i=c;

if(i>=97 && i<=122)
{
System.out.println("Enterd Character is Lower Case :"+c);
i=i-32;
c=(char)i;
System.out.println(" Upper case  Character is:"+c);

}
else if(i>=65 && i<=90)
{
System.out.println("Enterd Character is Upper Case :"+c);
i=i+32;
c=(char)i;
System.out.println(" Lower Case  Character is:"+c);

}
else
{
System.out.println("pleas enter a correct case Character");

}
}
}


Publisher: Brijmohan Lal Sahu - 3:16 PM