Pattern program in java:

what is pattern program:

Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs.

what is the uses:

Pattern programs best means to study the working of looping structures in general purpose programming languages. Its helps beginners visualize how every iteration in a loop works. Also popularly asked in campus placements & job interviews.

program1:Ascending

public class Ascending  
{
	
public static void main(String[] args)
{
	int count;
	for( count=1; count<=5; count++)
	{
for(int i=1; i<=5; i++)
{
	System.out.print(i + "  " );
}
System.out.println();
}
}
}

output:

program2:Descending

public class Descending 
{
public static void main(String[] args) 
	{
	int count;
	for( count=1; count<=5; count++)
	{
for(int i=5; i>0; i--)
{
	System.out.print(i + "  " );
}
System.out.println();
}
}
}

output:

program3: vertical

public class Vertical 
{
public static void main(String[] args)
{
	for( int i=1; i<=5; i++)
	{
	for( int j=1; j<=5; j++)
		{
			System.out.print( i+" " );
		}
		System.out.println();
		}
	}
}

output:

program4: Rvertical

public class Rvertical
{
public static void main(String[] args)
{
	for( int i=10; i>=1; i--)
		{
		for( int j=10; j>=1; j--)
			{
				System.out.print( i+" " );
			}
			System.out.println();
			}
		}
	}

output:

program5: star

public class Star 
{
public static void main(String[] args)
{
	int i=1;
	int j=1;
for(i=1; i<=5; i++)
{
	for(j=1; j<=5; j++)
	{
		System.out.print( " * " );
	}
	System.out.println();
}
}
}

output:

Fibonacci series in java:

A Fibonacci Series in Java is a series of numbers in which the next number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1.

Example:

//0+1=1
//1+1=2
//1+2=3
//2+3=5
//3+5=8
//5+8=13
//8+13=21
//13+21=34
//21+34=55
//34+55=89

Example:

  1. class FibonacciExample2{
  2. static int n1=0,n2=1,n3=0;
  3. static void printFibonacci(int count){
  4. if(count>0){
  5. n3 = n1 + n2;
  6. n1 = n2;
  7. n2 = n3;
  8. System.out.print(” “+n3);

Example program:

public class Finociseries 
{
	public static void main(String[] args)
	{
	int f =0, s = 1; 
	int t = f + s; 
	while(t<100)
	{
	System.out.print( t+ "  " ); //1
	f = s; 	//f = 1
	s = t;	//s = 1
	t = f + s; 
	} 
	
}
}

output:

Perfect number in java:

A number whose sum of factors (excluding the number itself) is equal to the number is called a perfect number. In other words, if the sum of positive divisors (excluding the number itself) of a number equals the number itself is called a perfect number.

Example program:

public class Perfect_number
	{

		public static void main(String[] args)
		{
			int num=28;
	        int div=1;
	        int total = 0;
	        while(div<num)
	        {
	        	if(num%div==0)
	        	{
	        		System.out.println(div);
	        		total = total+div;
	        	}
	        	div++;
	        	
	        }
	        System.out.println(total);
			if(num==total)
			{
				System.out.println("Perfect");
			}

		}

	}

output:

LCM program in java:

Why LCM use in java:

In arithmetic, the Least Common Multiple (LCM) of two or more numbers is the least positive number that can be divided by both the numbers, without leaving the remainder. It is also known as Lowest Common Multiple (LCM), Least Common Denominator, and Smallest Common Multiple.

Example program :

public class Lcm 
{
public static void main(String[] args)
{
	
int no1 = 4,no2 = 15;//4 8 12 ...... 40 44 48 52 56 60 
	//64 68 72 76 80 84 88 92 96 100 104 108 112 116 120  int big = no1>no2?no1:no2;  //4>15  4:15  //15 30 45 60 75 90    
                            105 120 135 150
while(true)            //lcm - 60 120
{
if(big%no1 ==0 && big%no2==0)
{	
System.out.println("LCM "+ big); 
break;
}
big++; 
} 
}
}

output:

sum of digit java program:

Step 1: Get the modulus/remainder of the number.

Step 2: sum the remainder of the number.

Step 3: Divide the number by 10.

Step 4: Repeat the step 2 while number is greater than 0.

Example program:

public class Sum of digit
{
public static void main(String[] args)
{
int box =1234;
int total= 0;
while(box>0)
{
int rem = box%10;
total = total+rem;
box = box/10;
}
System.out.println(total);

}

}

output:

Armstrong number in java:

an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example: 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 =371.

example :

public class Armstrong 
{
  public static void main(String[] args) 
	{
		 int num = 370,  temp, total = 0 ;
                 int number =num;
	         while (num > 0)
	       {
	            temp = num % 10;    //3 5 1
	            total = total + temp*temp*temp;
	            //(0+3*3*3=27)  (27= 27+5*5*5=152) (152=152+*1*1*1=153)
	            num= num/10;  //(153/10= 15)   //(15/10=1)  (1=1/10=0)
	        }
	        System.out.println(total);
	        if(number==total)
	        {
	           System.out.println("is an Armstrong number");
	        }
	        else 
	        {
                   System.out.println(" is not an Armstrong number");	        	
	        }
	}
	
} 

output:

For-loop in java:

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.

Syntax:

for(statement 1;statement 2;statement 3)
{
//
//
//
}

Statement 1 :is executed (one time) before the execution of the code block.

Statement 2 :defines the condition for executing the code block.

Statement 3: is executed (every time) after the code block has been executed.

Example:

for( int i=0; i<5; i++)
{
System.out.println(i);
}

Example program:

public class For
{

	public static void main(String[] args) 
	{
            int box = 6012374; 
	    int total = 0; 
	    int count = 0; 
	    //while(box>0)
	    for (box=6012374; box>0; count++) //for loop
	{
	 System.out.println("Hi"); 
	 int rem = box%10; 
	 total = total + rem; //4	3	2	1
	 box=box/10;
	 //count++; 
	}
	System.out.println("Count of Digits " + count); 
	System.out.println("Sum of Digits " + total); 

 }
}

Output:

Do-while in java:

What is do-while:

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax in do-while:

    do{    
    //code to be executed / loop body  
    //update statement   
    }while (condition);    

Flowchart of do-while loop:

flowchart of do while loop in java

Example program:

public class Do
{
public static void main(String[] args)
{
int no = 20;
do
{
System.out.println(no);
no++;
}while(no<=25); //2<=5
}
}

Output:

Palindrome Program in Java

Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. It can also be a string like LOL, MADAM, MALAYALAM etc.

public class Palindrome 
{
public static void main(String[] args)
{
			int box = 121; int box2 = box; //box2 = 121
			int total = 0; 
			int reverse = 0; //1  12
			int count = 0; 
			while(box>0)		
			{
			System.out.println("Hi");  
			int rem = box%10; 
			total = total + rem; //1+2+1=4
			reverse = (reverse*10) + rem; //0*10+1=1 1*10+2=12 12*10+1=121
			box=box/10;  //121/10
			count++; 
			} 	
			if(box2==reverse)	System.out.println("Palindrome"); 
			System.out.println("Reversed Number is " + reverse); //palindrome
			System.out.println("Count of Digits " + count); //loop 3 times
			System.out.println("Sum of Digits " + total); //1+2+1=4

		}
}

OUTPUT:

count of digit program in java:

In this program, you’ll learn to count the number of digits using a while loop and for loop in Java.

To understand this example, you should have the knowledge of the following Java programming topics:

public class Countofdigit
{
	public static void main(String[] args)
	{
	int box = 6012374; 
	int total = 0; 
	int count = 0;
	while(box>0)    //6012374>0    601237>0   60123>0                        
	{
	System.out.println("Hi"); // hii  hii hii
	int rem = box%10;      // 4 7      6 
	total = total + rem; // 0=0+4=4  4=4+7=11  23
	box=box/10;              //6012374/10=601237
//601237/10=60123  0
	count++;// 1 2 7
	}
	System.out.println("Count of Digits " + count);// 7
	System.out.println("Sum of Digits " + total);
	
	}
		
}

output:1

output:2