Posts

Pool Puzzel

Image
  public class PoolPuzzleOne {     public static void main ( String [] args ) {         int x = 0 ;         while ( x < 4 ) {             if ( x < 1 ) {                 System . out . print ( "a " );             }             if ( x < 1 ) {                 System . out . print ( "noise" );             }                         if ( x == 1 ){                 System . out . print ( "annoys" );             }             if ( x > 1 ) {                 System . out . print ( "an" );           ...

All pyramid triangle

Image
  public class alltriangle {     public static void main ( String [] args ) {         // bottom Left Corner Star Pattern         int n = 4 ;         for ( int i = 1 ; i <= n ; i ++){             for ( int j = 1 ; j <= n ; j ++){                 if ( i - j >= 0 ){                     System . out . print ( "*" );                 }             }             System . out . println ();         }         System . out . println ();         // Top left corner star pattern             for ( int i = 1 ; i <= n ; i ++){             for ( int j = 1 ; j <= n ; j +...

Convert IST to GMT

  import java . util .* ; public class TimezoneConvertor {     public static void main ( String [] args ) {                 Scanner sc = new Scanner ( System . in );         // Taking user input for minute, hour, day, month, and year         int min = sc . nextInt ();         int hour = sc . nextInt ();         int day = sc . nextInt ();         int month = sc . nextInt ();         int year = sc . nextInt ();                 // Close scanner to avoid resource leaks         sc . close ();         // Adjust hours and minutes for IST to GMT conversion         hour = hour + 5 ;         min = min + 30 ;         if ( min >= 60 ) {   ...

FizzBuzz

  import java . util .* ; public class fizzbuzz {     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter the number" );         int n = sc . nextInt ();         String result = ( n % 3 == 0 && n % 5 == 0 ) ? "fizzbuzz" : ( n % 5 == 0 ) ? "buzz" : ( n % 3 == 0 ) ? "fizz15" : Integer . toString ( n );         System . out . println ( result );     } }

Switch case advance usage

  import java . util .* ; public class weekday {     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter the number" );         int n = sc . nextInt ();         switch ( n ) {             case 1 , 2 , 3 , 4 , 5 : { System . out . println ( "week day" ); break ;}             case 6 , 7 : { System . out . println ( "week end" ); break ;}             default: System . out . println ( "Invalid Day" );                   }     }     }

Reverse of number

  import java . util .* ; class reverseofnumber {     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter the number" );         int k = sc . nextInt ();         int rev = 0 ;         while ( k != 0 ){             int n = k % 10 ;             rev = rev * 10 + n ;             k = k / 10 ;         }                 System . out . println ( "Reverse of number is " + rev );     } }

String compression TC = O(n)

Image
  class stringcompression {     public static String printstring ( String str ){         StringBuilder result = new StringBuilder ();         for ( int i = 0 ; i < str . length (); i ++){             Integer count = 1 ;             while ( i < str . length () - 1 && str . charAt ( i ) == str . charAt ( i + 1 )){                 count ++;                 i ++;             }             result . append ( str . charAt ( i ));             if ( count > 1 ){                 result . append ( count );             }         }         return result . toString ();     } ...