Showing posts with label for loop. Show all posts
Showing posts with label for loop. 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

Saturday, November 1, 2014

, , , , , ,

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 1

Diamond Pattern 1

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

/* Program to print diamond pattern 1 */


class diam1
{
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-1; 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 - 11:35 AM
, , , , ,

Program to find all prime numbers in between a range and also sum all it's digits.


Code Box
----------------------------
/*program to find a number is prime or not and sum its all digits between a range */

class rprimesum
{
public static void main(String a[])
{
int i,j,c,l,k,sum,n,b,m,z;
j=0;
z=0;
n=Integer.parseInt(a[0]);
m=Integer.parseInt(a[1]);

for(l=n;l<=m;l++)
{ //----------outer loop for number
c=0;
b=l;
sum=0;

for(i=2;i<l;i++)
{
if(l%i==0)
{c++;
}
}
//------------------------if it is prime
if(c==0)
{
for(j=0;j<=b;j++)
{
k=b%10;
b=b/10;
sum=sum+k;
}
System.out.println("Number "+l+" is prime and sum of digits are : "+sum);

z=1;//---------------------for checking any prime number found or not
}
//-------------------------------if it is prime end
} //----------------------------------------outer loop for number

if(z==0)   //-----------to check prime number exist or not
{
System.out.println("Not prime in between range");
}
}
}
Publisher: Brijmohan Lal Sahu - 2:30 AM

Sunday, September 22, 2013

, ,

Number Pattern 10 by For Loop


Number patterns Some of most interesting topics always in programming...

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

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

        1
       21
      321
     4321
    54321

*/

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

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

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

        for(k=i;k>=1;k--)
        {

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

}
}
}

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

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

Program to print number table from 2 to 10


Number Table printing requires a little knowledge of for loop for repeating the chain..

This program print tables for numbers from 2 to 10.

you can also change the range at first outer loop to increase of decrease the range..

Code Box
--------------------
/* A simple program to print Number Table from 2 to 10  */

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

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

for(i=2;i&lt;=10;i++)
{
        System.out.print("Table of "+i+" =&gt;"); 
        for(j=1;j&lt;=10;j++)
        {
         num=i*j;
        System.out.print(" | "+num);
        }
System.out.println("\n-------------------------------------------------------\n");

}
}
}


Publisher: Brijmohan Lal Sahu - 3:02 PM