Monday, May 30, 2016

Difference between println() and print() in java


This is a very useful thing when you are doing some fancy programming in java and want to decorate your output.


println() and print() have same functionality of printing strings inside () with a few difference.

Both are from same parents(System.out):

for println=>System.out.println("Hello I am Print");

for print =>System.out.print("Hello I am Print");


Now the difference between println() and print() is

PRINTLN:


  • println() means print with a new line. Or get a new line then print the out put.
  • automatic new line before printing.
PRINT
  • print() means just start printing no matter where you are. No new line generated.
  • you need to insert new line manually.

Best thing about them is Java happily support both of them.

Publisher: Brijmohan Lal Sahu - 10:42 PM

Saturday, November 15, 2014

, , , , , ,

Program to swap two values without third variable in java.

Swapping without third variable


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

/*program to swap two values without using third variable */

import java.io.*;

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

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nEnter two numbers :");
System.out.print("\nEnter first number a= ");
a=Integer.parseInt(obj.readLine());


System.out.print("\nEnter second number b= ");
b=Integer.parseInt(obj.readLine());


System.out.print("\nValues before swapping a= "+a+" & b= "+b);

a=a+b;   //swapping
b=a-b;
a=a-b;

System.out.print("\nValues after swapping a= "+a+" & b= "+b);

}
}


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

Program to swap two values using third variable in java.

Swapping using third variable


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

/*program to swap two values using third variable */

import java.io.*;

class swap
{
public static void main(String x[]) throws IOException
{
int a,b,temp;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nEnter two numbers :");
System.out.print("\nEnter first number a= ");
a=Integer.parseInt(obj.readLine());

System.out.print("\nEnter second number b= ");
b=Integer.parseInt(obj.readLine());

System.out.print("\nValues before swapping a= "+a+" & b= "+b);

temp=a;   //swapping
a=b;
b=temp;

System.out.print("\nValues after swapping a= "+a+" & b= "+b);

}
}



Publisher: Brijmohan Lal Sahu - 10:14 AM

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
 

 

Our Leading Clients

Awesome people who trust us