Posts

Showing posts from December, 2023

Type casting

  package code ; public class typecasting {     public static void main ( String [] args ) {                 float n1 = 224.3434f ;     int n2 = ( int ) n1 ;     System . out . println ( n2 ); } }

VarArgs

  public class varArgs {     static int sum ( int ... arr ) {         int sum = 0 ;         for ( int a : arr ) {             sum += a ;         }         return sum ;     }     public static void main ( String [] args ) {         System . out . println ( "Sum:" + sum ());         System . out . println ( "Sum:" + sum ( 1 ));         System . out . println ( "Sum:" + sum ( 5 , 9 ));         System . out . println ( "Sum:" + sum ( 10 , 13 , 15 ));         System . out . println ( "sum:" + sum ( 51 , 17 , 19 , 95 ));         System . out . println ( "sum:" + sum ( 121 , 34 , 56 , 67 , 78 , 90 ));     } }                         ...

L.C.M using methods

import java . util .* ; public class p10 {     static void lcm ( int n1 , int n2 ){         int lcm ;         if ( n1 > n2 ){ lcm = n1 ;         }         else {             lcm = n2 ;         }         while ( true ) {             if ( lcm % n1 == 0 && lcm % n2 == 0 ){                 System . out . println ( "LCM of two numbers is:" + lcm );                 break ;             }             lcm ++ ;         }     }     public static void main ( String [] args ) {     Scanner sc = new Scanner ( System . in );     System . out . println ( "Enter n1:" );     int n1...

Fibonacci series using methods

  import java . util .* ; public class p9 {     static void fibonacci ( int a , int b , int n ){         int temp ;         System . out . print ( a + " " );         for ( int i = 0 ; i <= n ; i ++ ){             temp = b ;             b = a + b ;             a = temp ;             System . out . print ( b + " " );         }     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter n:" );       int n = sc . nextInt ();       int a = 0 ;       int b = 1 ;     fibonacci ( a , b , n );     } }

GCD of two numbers using methods(WHILE LOOP)

  import java . util .* ; public class p8_1 {     static int gcd ( int n1 , int n2 ) {         while ( n1 != n2 ) {             if ( n1 > n2 ) {                 n1 -= n2 ;             } else {                 n2 -= n1 ;             }         }         return n1 ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter n1:" );         int n1 = sc . nextInt ();         System . out . println ( "Enter n2:" );         int n2 = sc . nextInt ();         System . out . println ( "GCD of two nu...

G.C.D of two numbers using methods

  import java . util .* ; public class p8 {     // GCD     static int gcd ( int n1 , int n2 ) {         int gcd = 1 ;         for ( int i = 1 ; i <= n1 && i <= n2 ; i ++ ) {             if ( n1 % i == 0 && n2 % i == 0 ) {                 gcd = i ;             }         }         return gcd ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter n1:" );         int n1 = sc . nextInt ();         System . out . println ( "Enter n2:" );         int n2 = sc . nextInt ();         System . out . p...

x to the power of n using methods

  import java . util .* ; public class p7 {     static double power ( double x , double n ) {         double power = Math . pow ( x , n );         return power ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter x:" );         double x = sc . nextDouble ();         System . out . println ( "Enter n:" );         double n = sc . nextDouble ();         System . out . println ( " x to the power of  n:" + power ( x , n ));     } }

******Finding the no.of positive,negative,zero's entered*******

  import java . util .* ; // FINDING THE NO.OF POSITIVE,NEGATIVE,ZERO'S ENTERED public class p6_a {     public static void main ( String [] args ) {       Scanner sc = new Scanner ( System . in );       int zero = 0 , positive = 0 , negative = 0 ;       System . out . println ( "Enter 1 to continue and 0 to stop" );       int input = sc . nextInt ();       while ( input == 1 ) {           System . out . println ( "enter number" );           int number = sc . nextInt ();           if ( number > 0 ) {             positive ++ ;          } else if ( number < 0 ) {             negative ++ ;          } else {             zero ++ ;       ...

eligible for voting or not using methods

  import java . util .* ; public class p5 {     static void eligible ( int age ) {         if ( age < 18 ) {             System . out . println ( "Not eligible for voting" );         } else if ( age >= 18 ) {             System . out . println ( "Eligible for voting" );         }     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter Age:" );         int age = sc . nextInt ();         eligible ( age );     } }

Circumference of a circle using methods

  import java . util .* ; // CIRCUMFERENCE OF A CIRCLE public class p4 {     static float circumference ( int r ) {         float circumference = 2 * 3.14f * r ;         return circumference ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter radius:" );         int r = sc . nextInt ();         System . out . println ( "Circumference of the circle is:" + circumference ( r ));     } }

Finding greater among given two numbers using methods

  import java . util .* ; // FINDING THE GREATEST NUMBER AMONG THE GIVEN 2 NUMBERS public class p3 {     static int greater ( int a , int b ) {         if ( a > b ) {             return a ;         } else  {             return b ;         }     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter a:" );         int a = sc . nextInt ();         System . out . println ( "Enter b:" );         int b = sc . nextInt ();         System . out . println ( "Greatest number among the given numbers is:" + greater ( a , b ));     } }

sum of odd numbers from 1 to n using methods

  import java . util .* ; public class p2 {     static int sum ( int n ) {         int sum = 0 ;         for ( int i = 1 ; i <= n ; i ++ ) {             if ( i % 2 != 0 ) {                 sum += i ;             }         }         return sum ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter n:" );         int n = sc . nextInt ();         System . out . println ( "sum of odd numbers is:" + sum ( n ));     } }

Average using methods

  import java . util .* ; class p1 {     static float avg ( int a , int b , int c ) {         float avg ;         avg = ( a + b + c ) / 3f ;         return avg ;     }     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter a:" );         int a = sc . nextInt ();         System . out . println ( "Enter b:" );         int b = sc . nextInt ();         System . out . println ( "Enter c:" );         int c = sc . nextInt ();         System . out . println ( "Average of the numbers:" + avg ( a , b , c ));     } }

Method Overloading

  public class methodoverloading {     static void display () {         System . out . println ( "Hellooo" );     }     static void display ( int a ) {         System . out . println ( "Good Morning" + a );     }     static void display ( int a , int b ) {         System . out . println ( "Helloo" + a );         System . out . println ( "Good Morning" + b );     }     public static void main ( String [] args ) {         display ();         display ( 5 );         display ( 5 , 7 );     } }

Changing value of an array

  public class changingarr {     static void change ( int [] arr ) { // void         arr [ 0 ] = 45 ;     }     public static void main ( String [] args ) {         int [] marks = { 34 , 55 , 56 , 88 , 35 };         change ( marks );         System . out . println ( "value after changing:" + marks [ 0 ]);     } }

Product of two numbers using methods

  import java . util .* ; public class product {     public static int product ( int a , int b ) {         return a * b ;     }     public static void main ( String [] args ) {         System . out . println ( "Enter a and b:" );         Scanner sc = new Scanner ( System . in );         int a = sc . nextInt ();         int b = sc . nextInt ();         System . out . println ( "Product of two numbers is:" + product ( a , b ));     } }

METHODS

  public class methods {     static int logic ( int x , int y ) { // Method or function declaration         int z ;         if ( x > y ) {             z = x + y ;         } else {             z = ( x + y ) * 5 ;         }         return z ;     }     public static void main ( String [] args ) {         int a = 5 ;         int b = 7 ;         int c = logic ( a , b ); // method call or function call         int d = 2 ;         int e = 1 ;         int f = logic ( d , e );         System . out . println ( c );         System . out . println ( f );     } }

finding element in a 2d array

  //FINDING ELEMENT IN A 2D ARRAY public class practice5 {     public static void main ( String [] args ) {         int [][] a = { { 14 , 5 , 7 }, { 19 , 31 , 69 }, { 7 , 90 , 11 } };         int n = 69 ;         for ( int i = 0 ; i < 3 ; i ++ ) {             for ( int j = 0 ; j < 3 ; j ++ ) {                 if ( a [ i ][ j ] == n ) {                     System . out . println ( "element found at index:(" + i + "," + j + ")" );                 }             }         }     } }

Finding if an array is sorted or not

  public class practice4 {     public static void main ( String [] args ) {         int [] a = { 112 , 34 , 56 , 71 };         boolean sorted = true ;         for ( int i = 0 ; i < a . length - 1 ; i ++ ) {             if ( a [ i ] > a [ i + 1 ]) {                 sorted = false ;                 break ;             }         }         if ( sorted ) {             System . out . println ( "array is sorted" );         } else {             System . out . println ( "array is not sorted" );         }     } }

Finding minimum

  public class findingmin {     public static void main ( String [] args ) {         int [] a = { 112 , 45 , 667 , 54 };         int min = Integer . MAX_VALUE ;         for ( int i = 0 ; i < a . length ; i ++ ) {             if ( a [ i ] < min ) {                 min = a [ i ];             }         }         System . out . println ( "minimum value:" + min );     } }

Finding max

public class findingmax {     public static void main ( String [] args ) {         int [] a = { 112 , 45 , 667 , 54 };         int max = 0 ;         for ( int element : a ) {             if ( element > max ) {                 max = element ;             }         }         System . out . println ( "maximum element is:" + max );     } }                                                                   OR public class findingmax {     public static void main ( String [] args ) {         int [] a = { 112 , 45 , 667 , 54 };       ...