Showing posts with label java basic. Show all posts
Showing posts with label java basic. 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 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

Sunday, November 2, 2014

, , , ,

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 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 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 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 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
, , , , , ,

Number Pattern 3 by while loop,


Code Box by while loop
-------------------------------
/*program to print number pattern  by while loop

  1
  22
  333
  4444
  55555

*/

class npat3w
{
public static void main(String a[])
{
int n,i,j;
i=1;
n=Integer.parseInt(a[0]);

while(i<=n)
{j=1;
        while(j<=i)
        {
        System.out.print(i);
        j++;
}

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

Publisher: Brijmohan Lal Sahu - 1:25 AM
, , , ,

Number Pattern 3 by do while loop


Code Box by do while loop
------------------------------
/*program to print number pattern  by do while loop

  1
  22
  333
  4444
  55555

*/

class npat3do
{
public static void main(String a[])
{
int n,i,j;
i=1;
n=Integer.parseInt(a[0]);

do
{j=1;
        do
        {
        System.out.print(i);
        j++;
}
while(j<=i);
System.out.print("\n");
i++;
}
while(i<=n);
}
}


Publisher: Brijmohan Lal Sahu - 1:22 AM
, , , ,

Number Pattern 4 by while loop


Code Box by while loop
-----------------------------
/*program to print star pattern square box by while loop

  11111
  22222
  33333
  44444
  55555

*/

class npat4w
{
public static void main(String a[])
{
int n,i,j;
i=1;
n=Integer.parseInt(a[0]);

while(i<=n)
{j=1;
        while(j<=n)
        {
        System.out.print(i);
        j++;
}

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

}
}

Publisher: Brijmohan Lal Sahu - 1:13 AM

Sunday, September 22, 2013

, , , , , ,

Number Pattern 6 by for loop



Code Box by for loop
---------------------------
/*program to print star pattern square box by for loop

  55555
  44444
  33333
  22222
  11111
  

*/

class npat6
{
public static void main(String a[])
{
int n,i,j;

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

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

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

}
}
}


Publisher: Brijmohan Lal Sahu - 5:18 PM
, , , , , ,

Number Pattern 4 by for loop



Code Box by for loop
-------------------------
/*program to print number pattern square box by for loop

  11111
  22222
  33333
  44444
  55555

*/

class npat4
{
public static void main(String a[])
{
int n,i,j;

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

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

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

}
}
}


Publisher: Brijmohan Lal Sahu - 5:13 PM
, , , , ,

Number Pattern 3 by for loop



Code Box by for loop
-----------------------------
/*program to print number pattern by for loop

  1
  22
  333
  4444
  55555

*/

class npat3
{
public static void main(String a[])
{
int n,i,j;

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

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

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

}
}
}



Publisher: Brijmohan Lal Sahu - 5:08 PM
, , , , , ,

Number Pattern 2 by for loop



Code Box by for loop
--------------------------

/*program to print star pattern by for loop

  1
  12
  123
  1234
  12345

*/

class npat2
{
public static void main(String a[])
{
int n,i,j;

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

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

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

}
}
}


Publisher: Brijmohan Lal Sahu - 4:40 PM
, , , , , ,

Number Pattern 1 by for loop


Number Pattern
codeboxjava
Code Box by for loop
-------------------------
/*program to print number pattern square box by for loop

  12345
  12345
  12345
  12345
  12345
  
*/

class npat1
{
public static void main(String a[])
{
int n,i,j;

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

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

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

}
}
}


Publisher: Brijmohan Lal Sahu - 4:08 PM
, , , ,

Program to illustrate Function with return type and arguments in java


Addition of two numbers.

Code Box
-------------------
/*Program for  function with return type and with arguments*/

class funwithr
{
public static void main(String r[])
{
int total,a,b;
a=Integer.parseInt(r[0]);
b=Integer.parseInt(r[1]);

System.out.print("\nSum of two number:");
total=sum(a,b);
System.out.print(total);    //Function call
}
public static int sum(int a,int b)  //Function Definition
{
int c;
c=a+b;
return c;
}


}

Publisher: Brijmohan Lal Sahu - 3:54 PM
, , , , , , ,

Simple Function in java without return type and without arguments


Code Box

Download Link:
Publisher: Brijmohan Lal Sahu - 3:51 PM
, , , , , ,

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
, , , , , , ,

Perfect Number Checker Using while loop


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

/*Program to check input number is 
perfect or not using while loop*/

class per_while
{
public static void main(String a[])
{
int i,j,k,sum,mul;
sum=0;
mul=1;
i=1;
k=Integer.parseInt(a[0]);

while(i<k)
{
if(k%i==0)
{
mul=mul*i;
sum=sum+i;
}
i++;
}

if(sum==mul && k==mul)
{
System.out.println("perfect");
}
else
{
System.out.println("Not perfect");
}
}
}


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

Program to show solution of FizzBuzz Problem


Code Box
------------------------
/* FizzBuzz Program */
/*It prints number from 1 to 50 
=>In place of multiple of 3 it print Fizz
=>In place of multiple of 5 it print Buzz
*/

class fizzbuzz
{

public static void main(String a[])
{
int i;

for(i=1;i<=50;i++)
{
if(i%(3*5)==0)
{
System.out.print("BuzzFizz");
}
else if(i%5==0)
{
System.out.print("Buzz");
}
else if(i%3==0)
{
System.out.print("Fizz");
}
else
{
System.out.print(i);
}

if(i%10==0)
{
System.out.println("\n");
}
else
{
System.out.print("\t");
}
}
}
}



Publisher: Brijmohan Lal Sahu - 3:33 PM