Posts

Showing posts from August, 2021

Program to print the diffrence between largest number index and smallest number index in an Array

 Program to print the diffrence between largest number index and smallest number index in an Array I/P: 5 O/P: 1 6 4 0 3 PROGRAM: import   java . util .*; public   class   DiffrenceOfIndicesoflargestandSmallest  {      public   static   void   Diff ( int   arr [], int   iNum )     {          int   a2 []= new   int [ iNum ];          for ( int   i = 0 ; i < iNum ; i ++)         {              a2 [ i ]= arr [ i ];         }          Arrays . sort ( arr );          int   l = arr [ 0 ];          int   h = arr [ iNum - 1 ];    ...

Sort the Array in Alternate Manner (Ascending and Descending)

 Sort the Array in Alternate Manner (Ascending and Descending) I/P: 5  1 7 11 16 19 O/P: 19 1 16 7 11 PROGRAM: import   java . util .*; public   class   AlternateSortArray  {      public   static   void    Alternate  ( ArrayList < Integer >  al )     {          ArrayList < Integer >  al2 = new   ArrayList <>( al );                   Collections . sort ( al );          ArrayList < Integer >  al3 = new   ArrayList <>( al2 );          Collections . sort ( al3 , Collections . reverseOrder ());          int   j = 0 , k = 0 ;          for ( int   i = 0 ; i <...

Product of another elements in the Array

 Product of another elements in the Array i/p: 5 1 2 3 4 5  o/p: [120, 60, 40 ,30 ,24] PROGRAM: import   java . util .*; public   class   ProductOfArray  {      public   static   void   main ( String   args []) {          Scanner   sc  =  new   Scanner ( System . in );          int   a  =  sc . nextInt ();          int   arr [] =  new   int [ a ];          int   arr2 [] =  new   int [ a ];          for  ( int   i  =  0 ;  i  <  a ;  i ++) {              arr [ i ] =  sc . nextInt ();        ...

PROGRAM TO CHECK THE GIVEN NUM IS PRIME .IF ITS PRIME CHECK THE SUM OF ITS DIGITS ALSO A PRIME OR NOT

 PROGRAM TO CHECK THE GIVEN NUM IS PRIME .IF ITS PRIME CHECK THE SUM OF ITS DIGITS ALSO A PRIME OR NOT I/P: 13 O/P: Not a prime num I/P: 43 O/P: 43 PROGRAM: import   java . util .*; public   class   primeCheck  {      public   static    boolean   isPrime ( int   iNum )     {          int   flag = 0 ;        for ( int   i = 2 ; i <= iNum / 2 ; i ++)       {            if ( iNum % i == 0 )           {                flag = 1 ;                break ;           }       }  ...

Merge Two Arrays and Sort them in Ascending order

 Merge Two Arrays and Sort them in Ascending order I/p: [4,89,12,56,2,8] [23,5,7,9,1] O/p: [1, 2, 4, 5, 7, 8, 9, 12, 23, 56, 89] PROGRAM: import   java . util .*; public   class   MergeSortarray  {      public   static   void    main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . next ();          String   s2 = sc . next ();          String   s3 = "" ;          String   s4 = "" ;                   ArrayList < Integer >  al3 = new   ArrayList <>();          for ( int  ...

Find Frequency of a particular character in a String

 Find Frequency of a particular character in a String I/p: hi hello how are you h O/p: 3 PROGRAM: import   java . util .*; class   FrequencyforparticularCharacter {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . nextLine ();          char   c = sc . next (). charAt ( 0 );          String   arr []= s1 . split ( " " );          ArrayList < Character >  al = new   ArrayList <>();          for ( int   i = 0 ; i < arr . length ; i ++)         {        ...

Program to remove punctuation and special character from a string

Program to remove punctuation and special character from a string I/P: $$$helo5lo)))hi O/P: helo5lohi PROGRAM: import   java . util .*; public   class   RemoveSpecialCharacters  {      public   static   void   main ( String []  args ) {          // TODO Auto-generated method stub          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . next ();          for ( int   i = 0 ; i < s1 . length (); i ++) {              if ( Character . isDigit ( s1 . charAt ( i )) ||  Character . isAlphabetic ( s1 . charAt ( i )))             {        ...

PROGRAM TO COUNT THE NUMBER OF UPPERCASE ,LOWERCASE AND SPECIAL CHARACTER IN A STRING

  PROGRAM TO COUNT THE NUMBER OF UPPERCASE ,LOWERCASE AND SPECIAL CHARACTER IN A STRING I/P: hi hello everyone O/P: Number of uppercase letter : 0 Number of lowercase letter : 15 Number of Special characters : 2 Number of  vowels : 7 PROGRAM: import   java . util .*; public   class   CountUppercaseLowercase  {      public   static   void   main ( String []  args ) {          // TODO Auto-generated method stub          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . nextLine ();          int   c1 = 0 , c2 = 0 , c3 = 0 , c4 = 0 ;          for ( int   i = 0 ; i < s1 . length (); i ++)         {   ...

DECODING STRINGS

 DECODING STRINGS I/P: A4B2C2A2 O/P: AAAABBCCAA PROGRAM: public   class   DecodingStrings  {      public   static   void   main ( String []  args ) {          // TODO Auto-generated method stub             Scanner   sc = new   Scanner ( System . in );             String   s1 = sc . next ();             sc . close ();             int   iCount = 0 ;             for ( int   i = 0 ;i< s1 . length ();i++)            {                 if ( Chara...

CHECK THE GIVEN NUMBER IS AJS NUMBER OR NOT

 CHECK THE GIVEN NUMBER IS AJS NUMBER OR NOT I/P: 9817 O/P: 1 PROGRAM: import   java . util .*; public   class   Ajsnumornot  {      public   static   void   AjsCheck ( int   iNum )     {          String   s1 = Integer . toString ( iNum );          int    a = Integer . parseInt ( String . valueOf ( s1 . charAt ( 0 )))+ Integer . parseInt ( String . valueOf ( s1 . charAt ( 1 )));          String   s2 = Integer . toString ( a );          String   s3 = s1 . substring ( 2 , s1 . length ());             if ( s2 . contains ( s3 ))         {              System . out . println ( "1...

ENCODING STRINGS

 ENCODING STRINGS I/P: AAAABBBCCDAA O/P: 4A3B2C1D2A PROGRAM: import   java . util .*; public   class   Encoding  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . next ();          char   c = s1 . charAt ( 0 );          int   count = 1 ;          for ( int   i = 1 ; i < s1 . length (); i ++)         {              if ( c == s1 . charAt ( i ))             {            ...

HOW TO FIND DUPLICATE CHARACTER IN A STRING

 HOW TO FIND DUPLICATE CHARACTER IN A STRING I/P: abcdababefcd O/P: a b c d PROGRAM: import   java . util .*; class   DuplicateCharacterInAString {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . next ();          char   arr []= new   char [ s1 . length ()];          for ( int   i = 0 ; i < s1 . length (); i ++){              arr [ i ]= s1 . charAt ( i );         }          Arrays . sort ( arr );          int   count = 0 ;     ...

REVERSE A SENTENCE

 REVERSE A SENTENCE  I/P: India is my country O/P: country my is India PROGRAM: import   java . util .*; public   class   StringArrayWordReverse  {           public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . nextLine ();          String   sarr []= s1 . split ( " " );          for ( int   i = sarr . length - 1 ; i >= 0 ; i --)         {              System . out . print ( sarr [ i ]+ " " );         }     } }

SUM OF ADJACENT PAIRS OF NUMBERS IN AN ARRAY

  SUM OF ADJACENT PAIRS OF NUMBERS IN ARRAY I/P: 6 1 2 3 4 5 6 O/P: 3 7 11 PROGRAM: import   java . util .*; class   AdjacentPairSum {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   iNum = sc . nextInt ();          int   arr []= new   int [ iNum ];          for ( int   i = 0 ; i < iNum ; i ++)         {              arr [ i ]= sc . nextInt ();         }          if ( iNum % 2 == 0 )         {      ...