Skip to main content

Posts

Showing posts from November, 2023

remove consicutive duplicate

public class removeDuplicate { public static String removeDuplicate ( String str ){ if ( str .length() <= 1 ){ return str ; } String out = removeDuplicate ( str .substring( 1 )) ; if ( str .charAt( 0 ) == str .charAt( 1 )){ return out ; } else { return str .charAt( 0 ) + out ; } } public static void main ( String [] args ) { System . out .println( removeDuplicate ( "abacd" )) ; } }  

remove x from string using recursion in java

import java.sql.SQLOutput ; public class removeX { //write a java program to remove x from string using Recursion public static String removeX ( String str ){ if ( str .length() == 0 ){ return str ; } String ans = "" ; if ( str .charAt( 0 ) != 'x' ) { ans = ans + str .charAt( 0 ) ; } String smallans = removeX ( str .substring( 1 )) ; return ans + smallans ; } public static void main ( String [] args ) { System . out .println( removeX ( "absxsnd" )) ; } }  

Multiplication Recursive

Question: Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction and addition for your calculation. No other operators are allowed. Input format: Line 1: Integer M Line 2 : Integer N Ans:  Yaha ham kya kar rhe n ==  1 ho jata hai to return kar de rhe m  example n =1                   m = 4 1 X 4 = ? Answer will be 4 so return m m+ recursive call m aur n-1  isme m hai 4 aur +4 and n-1 ka mtlb hai kitne bar recursive call karna hai public class practiceRecursion { public static int multiply ( int n, int m){ if (n == 1 ){ return m; } return m+ multiply (m,n- 1 ); } public static void main (String [] agrs){ System. out .println( multiply ( 3 , 4 )); } }

Recursion Power

public   class   Solution   {      public   static   int   power ( int   x ,   int   n )   {          /* Your class should be named Solution          * Don't write main().          * Don't read input, it is passed as function argument.          * Return output and don't print it.          * Taking input and printing output is handled automatically.          */           if ( x == 0 &&   n == 0 )      {          return   1 ;      }      if ( x == 0 )      {          return   0 ;      }      if ( n == 0 )      {       return   1 ;         }      return   x *( power ( x ,   n - 1 ));      } }                isme ham x ko multiple kar rhe pow function ke saath aur parameter call kiya ha jo function me parameter dala tha lekin n -1 kar rhe kyo ki hamen 0 tak chlna hai loop chalana hai basically

Recursion In JAVA .

What is Recursion ? Ans: A function in Java can call itself is such calling of function by itself is called recursion. Ek aisa bnana jo khud ko hi call kare aur tab tk call kare jb tk ki base line touch na kar jaye wo recursion hota hai. Stack Me FIFO model chlta hai  What is base case ? where we want to stop our recursion. public class Recursion2 { public static int factorialN ( int n){ if (n == 0 || n == 1 ){ // base case return 1 ; } return n * factorialN (n- 1 ); } public static void main (String[] args) { int n = 4 ; System. out .println( "Factorial Number of n is:" + factorialN (n)); factorialN (n); } } 2nd: Examples: // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, // then press Enter. You can now see whitespace characters in your code. public class Recursion1 { public static void printN ( int n) { if (n == 0 ){ //base case

Logic Gate

Not Gate:   Not gate kisi bhi value ko change kr deta hai Question: Explain the operation of a NOT gate in digital logic. Provide a truth table for a NOT gate and explain how it inverts the input signal. Ans: "NOT gate digital logic mein ek aisa gate hai jo ek input signal ko ulta (inverse) karta hai. Iska kaam ek single input signal ko lekar use opposite output signal generate karna hota hai. NOT gate ka truth table niche diya gaya hai: Input (A)                Output (Y) 0                                    1 1                                    0 Is truth table se dikhaya gaya hai ki NOT gate input signal ko ulta karke output signal deta hai. Agar input 0 hota hai, to output 1 hota hai, aur agar input 1 hota hai, to output 0 hota hai. Yani, NOT gate ek signal ko reverse karta hai." 2.     Or Gate A+b = y Ka Expression Or gate hota hai. 3. AND Gate  ----------- Ans: Agar zero hoti hai dono me se koi v value to return 0 hoga aur agr dono 1 and  1 hota hai to output 1 d