Posts

Showing posts from May, 2024

leap year logic with && and ||

Image
  import java . util .* ; /**  * leapyear  */ public class leapyear {     public static void main ( String [] args ) {         int y ;         Scanner s = new Scanner ( System . in );         System . out . println ( "Enter The year" );         y = s . nextInt ();         if (( y % 4 == 0 && y % 100 != 0 ) || ( y % 4 == 0 && y % 100 == 0 && y % 400 == 0 )){             System . out . println ( "This is leap year" );         }         else {             System . out . println ( "not a leap year" );         }     } } Output :=

Leap year logic with if and else statement

  import java . util .* ; /**  * leapyear  */ public class leapyear {     public static void main ( String [] args ) {         int y ;         Scanner s = new Scanner ( System . in );         System . out . println ( "Enter the year" );         y = s . nextInt ();         if ( y % 4 == 0 ){             if ( y % 100 == 0 ){                 if ( y % 400 == 0 ){                     System . out . println ( "leap year" );                 }                 else {                     System . out . println ( "not a leap year" );                 }             }             else {                 System . out . println ( "leap year" );             }                 }         else {             System . out . println ( "Not a leap year" );         }     } }

Type Promotion

  /**  * typepromotion  */ import java . util .* ; public class typepromotion {     public static void main ( String [] args ) { //         Type Promotion in Expressions // 1 . Java automatically promotes each byte, short, or char operand to int // when evaluating an expression. // 2. If one operand is long, float or double the whole expression is // promoted to long, float, or double respectively.         byte b = 4 ;         char c = 'a' ;         short s = 512 ;         int i = 1000 ;         float f = 3.14f ;         double d = 99.999 ;         double result = ( f * b ) + ( i % c ) - ( d * s );     }     }

Type Casting

Image
  import java . util .* ; public class typecasting {     public static void main ( String [] args ) {         // int marks = (int)99.99f;         // char c= 'a';         // int  n= c+1;         // short s=(short)c;         // System.out.println(s);         // System.out.println(n);         long l = 10454332333333337L ;         int n = ( int ) l ;         System . out . println ( n );                     } } Output :-

Diamond Pattern

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void diamondpattern ( int n ){         for ( int row = 1 ; row <= n ; row ++){             for ( int space = n - row ; space >= 1 ; space --){                 System . out . print ( " " );             }             for ( int col = 1 ; col <= 2 * row - 1 ; col ++){                 System . out . print ( "*" );             }             System . out . print ( " \n " );         }         for ( int row = n ; row >= 1 ; row --){             for ( int space = n - row ; space >= 1 ; space --){                 System . out . print ( " " );             }             for ( int col = 1 ; col <= 2 * row - 1 ; col ++){                 System . out . print ( "*" );             }             System . out . print ( " \n " );         }     }     public static void main ( String [] args ) {         Scanner s = n

Hollow Rhombus Pattern

Image
  import java . util .* ; public class practice {     public static void rhombus ( int n ){         for ( int row = 1 ; row <= n ; row ++){             for ( int space = 1 ; space <= n - row ; space ++){                 System . out . print ( " " );             }             for ( int col = 1 ; col <= n ; col ++){                 if ( row == 1 || row == n || col == 1 || col == n ){                     System . out . print ( "*" );                 }                 else {                     System . out . print ( " " );                 }                             }             System . out . print ( " \n " );         }             }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the no of stars on rhombus" );         n = s . nextInt ();         rhombus ( n );                 } } Output :=

Solid Rhombus Pattern

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void rhombus ( int n ){         for ( int row = 1 ; row <= n ; row ++){             for ( int space = 1 ; space <= n - row ; space ++){                 System . out . print ( " " );             }             for ( int star = 1 ; star <= n ; star ++){                 System . out . print ( "*" );             }             System . out . print ( " \n " );         }             }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the no of stars on rhombus" );         n = s . nextInt ();         rhombus ( n );                 } } Output :-

Butterfly Pattern for any number

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void butterflypattern ( int n ){         int space1 , space2 ;         for ( int row = 1 ; row <= n ; row ++){               for ( int col = 1 ; col <= row ; col ++){                 System . out . print ( "*" );                              }               space1 = ( n * 2 ) - ( row * 2 );               for ( int col = 1 ; col <= space1 ; col ++){                 System . out . print ( " " );              }               for ( int col = 1 ; col <= row ; col ++){                 System . out . print ( "*" );                              }               System . out . print ( " \n " );         }         for ( int row = 1 ; row <= n ; row ++){             for ( int col = row ; col <= n ; col ++){                 System . out . print ( "*" );             }             space2 = 2 * ( row ) - 2 ;             for (

Butterfly Pattern for even numbers

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void butterflypattern ( int n ){         int space ;         for ( int row = 1 ; row <= n / 2 ; row ++){               for ( int col = 1 ; col <= row ; col ++){                 System . out . print ( "*" );                              }               space = n - row * 2 ;               for ( int col = 1 ; col <= space ; col ++){                 System . out . print ( " " );              }               for ( int col = 1 ; col <= row ; col ++){                 System . out . print ( "*" );                              }               System . out . print ( " \n " );         }         for ( int row = 1 ; row <= n / 2 ; row ++){             for ( int col = row ; col <= n / 2 ; col ++){                 System . out . print ( "*" );             }             space = 2 * ( row ) - 2 ;             for ( int col =

0-1 Traingle Pattern

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void invertedhalfpryaminnumberpattern ( int n ){         int k = 0 ;         for ( int row = 1 ; row <= n ; row ++){             for ( int col = 1 ; col <= row ; col ++){                 if ( col % 2 == 0 ){                     System . out . print ( 0 + " " );                 }                 else {                     System . out . print ( 1 + " " );                 }                                                             }                                     System . out . print ( " \n " );                     }     }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the size of square matrix" );         n = s . nextInt ();         invertedhalfpryaminnumberpattern ( n );                 } } Output:-

Floid's Trangle

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void invertedhalfpryaminnumberpattern ( int n ){         int k = 1 ;         for ( int row = 1 ; row <= n ; row ++){             for ( int col = 1 ; col <= row ; col ++){                 System . out . print ( k + " " );                 k ++;                             }                                     System . out . print ( " \n " );                     }     }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the size of square matrix" );         n = s . nextInt ();         invertedhalfpryaminnumberpattern ( n );                 } } Output:-

Inverted Half piramid with numbers

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void invertedhalfpryaminnumberpattern ( int n ){         int k = 0 ;         for ( int row = 1 ; row <= n ; row ++){             for ( int col = 1 ; col <= n - k ; col ++){                 System . out . print ( col );                             }             k ++;             System . out . print ( " \n " );                     }     }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the size of square matrix" );         n = s . nextInt ();         invertedhalfpryaminnumberpattern ( n );                 } } Output -

Inverted rotated piramid

Image
  import java . util .* ; // import java.util.Scanner; public class practice {     public static void invertedrotated ( int n ){         for ( int row = 1 ; row < n ; row ++){             for ( int col = 1 ; col <= n - row ; col ++){                 System . out . print ( " " );             }             for ( int col2 = 1 ; col2 <= row ; col2 ++){                 System . out . print ( "*" );             }             System . out . print ( " \n " );         }     }     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n ;         System . out . println ( "Enter the size of square matrix" );         n = s . nextInt ();         invertedrotated ( n );                 } } Output -

Decimal To Binary

Image
  import java . util .* ; public class practice {     public static void main ( String [] args ) {         Scanner s = new Scanner ( System . in );         int n , b = 0 , k , i = 0 ;         System . out . println ( "Enter the Decimal Number to be converted to Binary" );         n = s . nextInt ();         while ( n > 0 ){             k = n % 2 ;             b = b + k * ( int ) Math . pow ( 10 , i );             n = n / 2 ;             i ++;         }         System . out . println ( "Decimal vlaue of Binary Number is " + b );     } } Output:-