All Posts By

mathanlalsait@gmail.com

Java Program how to find second largest number in array

April 12, 2023

Program:

public class SecondLarge 
{
	public static void main(String[] args) 
	{
		int[] a = { 25, 122, 33, 45, 56 };
		int largest = a[0];
		int secondLargest = a[0];
		System.out.println("The Given Array is:");

		for (int i = 0; i < a.length; i++) 
		{
			System.out.println(a[i] + "\t");
		}

		for (int i = 0; i < a.length; i++) 
		{
			if (a[i] > largest) 
			{
				secondLargest = largest;
				largest = a[i];
			} 
			else if (a[i] > secondLargest) 
			{
				secondLargest = a[i];
			}
		}
		System.out.println("The Second Largest Number is:" + secondLargest);
	}
}

Compile:

javac SecondLarge.java
 java SecondLarge

Output:

The Given Array is:
25	
122	
33	
45	
56	
The Second Largest Number is:56

Java Code To Check Print Array Elements (Using For Loop)

April 7, 2023

Program:

public class Array 
{
	public static void main(String[] args) 
	{
		int a[] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
		System.out.println("The Array Elements:");
		for (int i = 0; i < a.length; i++) 
		{
			System.out.println(a[i]);
		}

	}
}

Compile:

javac  Array.java
 java  Array

Output:

The Array Elements:
1
2
3
4
5
6
7

Java Code To Check Fibonnaci Series

April 6, 2023

Explanation:

         Fibonacci series means the next number is the sum of the previous two number.

         The First two number is always 0 and 1.
        
         Step 1: In this program,we use class name as Fibonacci.

         Step 2: In that main method,we declare the variable n1=0;n2=1,n3,i,count=10.

         Step 3: And then use for loop.
    
         Step 4: In that loop,the initalization starts from 2 because 0 and 1 are already 
                 printed.

         Step 5: To find n3 value,add n1 and n2.Now the n3 value will be printed.

         Step 6: Now we assign n2 value to n1 and n3 value to n2 in that statement 

                                      n1=n2;
                                      n2=n3;

         Step 7: And then put the value in the previous step n3=n1+n2.

         Step 8: This process will continue until the loop exixts.

Program:

public class Fibonnaci 
{
	public static void main(String args[]) 
	{
		int n1 = 0, n2 = 1, n3, i, num = 10;
		System.out.println("The fibonacci series are:");
		System.out.print(n1 + " " + n2);
		for (i = 2; i < num; ++i) 
		{
			n3 = n1 + n2;
			System.out.print(" " + n3);
			n1 = n2;
			n2 = n3;
		}
	}
}

Compile:

javac Fibonnaci.java
 java Fibonnaci

Output:

The fibonacci series are:
0 1 1 2 3 5 8 13 21 34

Java Code To Check Print the Reverse Of a Given Number

April 1, 2023
 EXPLANATION:-

   Here we see how to reverse a program using while loop.

STEP1: First write the class name and declare the main method.

STEP2: Initialize the variable int,num,remainder,reverse for the program.

STEP3: Use while loop and find the remainder value by 10 using the modulo of the given program.

STEP4: Then multiply the reverse number by 10 and add the modulo value what we get in the remainder.

STEP5: Next divide the number by 10.

STEP6: Keep on repeat the process until the zero value will appear.

STEP7: Finally we get the reverse value (EG:654321-123456).

Program:

public class Reverse 
{
	public static void main(String[] args) 
	{
		int num = 654321, reverse = 0;
		while (num != 0) 
		{
			int remainder = num % 10;
			reverse = reverse * 10 + remainder;
			num = num / 10;
		}
		System.out.println(" The reverse of the given number is : " + reverse);
	}
}

Compile:

javac Reverse.java
 java Reverse

Output:

 The reverse of the given number is : 123456

Java Code To Check Swap with Variable

March 31, 2023

EXPLANATION:

    step1: Write the class name and declare the main method.

    step2: Initalize the variable int a,b,temp.

    step3: Give the value for the variable a=10,b=5.

    step4: Assign the third variable as temp.

    step5: Declare temp=a; a=b; b=temp;
                    
    step6: Assign a to a temp variable temp=a;

    step7: Assign b to a variable a=b;

    step8: Assign temp to variable b=temp;

    step9: Now, the value will be swapped. 

Program:

public class Swapvar 
{
	public static void main(String args[]) 
	{
		int a = 10;
		int b = 5;
		b = a + b;
		a = b - a;
		b = b - a;
		System.out.println(a);
		System.out.println(b);
	}

}

Compile:

javac Swapvar.java
 java Swapvar

Output:

5
10

Java Code To Check Print Square Pattern Program

March 23, 2023

Program:

public class squarePattern 
{
       public static void main(String[] args) 
       {
             int size=9;
             for(int i=0;i<size;i=i+2) 
	     {
                      for(int j=0;j<size;j++) 
	              {
			  System.out.print("*");
	              }
		          System.out.println();
             }
       }
}

Compile:

javac squarePattern.java
 java squarePattern

Output:

*********
*********
*********
*********
*********

Java Code To Print Check Number Pattern Program

March 22, 2023

Program:

public class Pattern2
{
	public static void main(String args[])
	{
		int a=1;
		for(int i=1;i<=9;i=i+2)
		{
			for(int j=1;j<=i;j++)
			{
				System.out.print(a);

			}
		  System.out.println();
		  a++;
		}
	}
}

Compile:

javac Pattern2.java
 java Pattern2

Output:

1
222
33333
4444444
555555555

Java Code To Check Print Right Triangle Star Pattern

March 16, 2023

Program:

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

			}
			System.out.println();
		}
	}
}

Compile:

javac Pattern1.java
 java Pattern1

Output:

*
**
***
****
*****

Java Code To Check Average of First 100 Prime Number?

March 11, 2023

Program:

import java.util.Scanner;

public class Avg 
{
	private static double a;

	public static void main(String args[]) 
	{
		int number = 2;
		int count = 0;
		double sum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("The number n is  ");
		int n = sc.nextInt();
		
		while (count < n) 
		{
			if (isPrimenumber(number)) 
			{
				System.out.print(number + " ");
				sum += number;
				a++;
				count++;
			}
			number++;
		}
		System.out.println("\n Sum=" + sum);
		double average = (sum / a);
		System.out.println("\n Average=" + average);
	}

	private static boolean isPrimenumber(int number) 
	{
		for (int i = 2; i <= number / 2; i++) 
		{
			if (number % i == 0) 
			{
				return false;
			}
		}
		return true;
	}
}

Compile:

javac Avg.java
 java Avg

Output:

The number n is: 100

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 

Sum=24133.0

Average=241.33