Skip to main content

Posts

Showing posts from August, 2020

Convert decimal value into binary value

For 4 binary is 100 For 21 binary is 10101  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner; public class DecToBin{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a decimal value:" ); int decimal = sc. nextInt (); int binary = 0 , i = 1 ; while (decimal!= 0 ){ binary += (decimal % 2 ) * i; i = i * 10 ; decimal = decimal / 2 ; } System. out . println ( "Binary value:" +binary); } } Python decimal = int ( input ( "Enter a decimal value:" )) binary, i = 0 , 1 while decimal != 0 : binary += (decimal % 2 ) * i i = i * 10 decimal = decimal // 2 print ( "binary value :" ,binary)

Write a function to calculate distance between three points

 Distance between two points (x1, y1) and (x2, y2) is calculated as:                       Java In java, to round double values to 2 decimal places, we use round()      double val = 3.1624537484; Math.round ( val * 100.0 / 100.0 )       ====> It produces 3.16 Python In python, to round values, we use round().

Convert binary value into decimal value

  For binary 100 decimal is 4.   For binary 10101 decimal is 21 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner; public class BinToDec{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a binary value:" ); int binary = sc. nextInt (); int decimal = 0 , i = 0 ; while (binary!= 0 ){ decimal += (binary % 10 ) * ( int )Math. pow ( 2 ,i); i++; binary = binary / 10 ; } System. out . println ( "Decimal value:" +decimal); } } Python binary = int ( input ( "Enter a binary value:" )) decimal = i = 0 while binary != 0 : decimal += (binary % 10 ) * ( 2 ** i) i += 1 binary = binary // 10 print ( "decimal value :" ,decimal)

Program to check if a given number is a Strong number or not

What is a strong number ?  A number is said to be strong if the sum of the factorial of its digit is equal to that number. eg.   23 => 2! + 3! = 2 + 6 = 8 ( Not a strong number).          145 => 1! + 4! + 5! = 1 + 24 + 120 = 145 ( Strong number). Java Python

Print Palindromic number pattern with stars

Q.  if rows = 5 * 1 * * 1 2 1 * * 1 2 3 2 1 * * 1 2 3 4 3 2 1 * * 1 2 3 4 5 4 3 2 1 * Java Python

Write a program to reverse a number

 Java Python

Write a program to reverse a string

 Ex:-  "code"  ==> "edoc"    ,    "heist" ==> "tsieh"  Java Python

Print Fibonacci series upto n terms

Fibonacci Series :- Series of numbers in which each number is the sum of two preceding numbers.    The series is   1, 1, 2, 3, 5, 8, 13, 21 etc.  Java Python

Check if a number is Palindrome or not

A number or string is said to be palindrome if it remains the same after reversing it.  Java Python

Print all the prime numbers in the given range

 What is a prime number?  check here Java Python

Find the GCD or HCF of two numbers

   GCD means Greatest Common Divisor  and HCF means Highest Common Factor       For 26 and 52  GCD is 26        For 26 and 39 GCD is 13      For 13 and 53 GCD is 1 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class GCDHCF{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int num1 = 39 , num2 = 65 ; System. out . print (gcd(num1, num2)); } public static int gcd ( int num1, int num2){ if (num1== 0 ) return num2; if (num2== 0 ) return num1; if (num1==num2) return num1; if (num1>num2) return gcd (num1-num2, num2); return gcd (num1, num2-num1); } } Python def gcd(num1, num2): if num1 == 0 : return num2 if num2 == 0 : return num1 if num1 == num2: return num1 if num1 > num2:

Check if a given year is a leap year

 A given year is said to be a leap year if it is divisible by 4 and at the same time not by 100. Or   It is divisible by 400. Ex:- 2100  ( divisible by 4 and 100 both )  ✖ Not a leap year            2020 ( divisible by 4 and not divisible by 100 ) ✔ Leap year           Java 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.Scanner; public class Prime{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int year = sc. nextInt (); System. out . print (checkLeapYr(year)); } public static boolean checkLeapYr ( int year){ if ( ( (year% 4 == 0 )&&(year% 100 != 0 ) ) || (year% 400 == 0 )) return true ; return false ; } } Python def checkLeapYr(year): if ( (year % 4 == 0 ) and (year % 100 != 0 ) ) or (year

Check a number is prime or not

        A number is prime if it is divisible by only two numbers 1 and the number itself. Ex:-   11 is prime ( bcz it is divisible by 1 and 11 only) ✔                12 is not prime ( bcz it is divisible by 1, 2, 3, 4, 6 and 12 ) ✖ Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner; public class Prime{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int num = sc. nextInt (); System. out . print (checkPrime(num)); } public static boolean checkPrime ( int num){ for ( int i= 2 ; i<=num/ 2 ; i++){ if (num%i== 0 ) return false ; } return true ; } } Python def checkPrime(num): for i in range ( 2 , num // 2 + 1 ): if num % i == 0 : return "Not prime" return "prime" num = int ( input ( "Enter a numbe

Find the second smallest element from an array or list

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Array2ndSmallest{ public static void main (String []args){ int [] arr = { 6 , 2 , 3 , 4 , 5 , 1 }; int first = Integer. MAX_VALUE ; int second = Integer. MIN_VALUE ; for ( int i= 0 ; i<arr. length ; i++){ if (arr[i]<first){ second = first; first = arr[i]; } else if (arr[i]<second && arr[i]!=first) second = arr[i]; } System. out . println ( "second smallest:" +second); } } Python import sys arr = [ 6 , 2 , 3 , 4 , 5 , 1 ]; first = sys . maxsize second = sys . maxsize for i in range ( 0 , len (arr)): if arr[i] < first: second = first first = arr[i] elif arr[i] < second and arr[i] != first: second = arr[i] print ( "second smal

Reverse an array in place in Java or list in Python

Ex:-      [ 1, 2, 3, 4, 5]  after reversing  [ 5, 4, 3, 2, 1]   Here, " in place "   means you can't take another array to reverse. Java 1 2 3 4 5 6 7 8 9 10 11 12 public class ArrayReverse{ public static void main (String []args){ int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 }; for ( int i= 0 ; i<arr. length / 2 ; i++){ int temp = arr[i]; arr[i] = arr[arr. length -i- 1 ]; arr[arr. length -i- 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python given_list = [ 1 , 2 , 3 , 4 , 5 , 6 ] given_list . reverse() print (given_list)

Find all pairs of an integer array or list whose sum is equal to a given number

Ex:-   [ 2, 4, 3, 5, 1, 6, 0 ]     and  5                      (2, 3)  ==> 2 + 3 = 5                      (4, 1) ==> 4 + 1 = 5                       (5, 0) ==> 5 + 0 = 5 Java 1 2 3 4 5 6 7 8 9 10 public class ArraySum { public static void main (String []args){ int [] arr = { 2 , 4 , 3 , 5 , 1 , 6 , 0 }; int given_sum = 5 ; for ( int i= 0 ; i<arr. length ; i++) for ( int j=i+ 1 ; j<arr. length ; j++) if (arr[i]+arr[j]==given_sum) System. out . println (arr[i]+ " " +arr[j]); } } Python given_list = [ 2 , 4 , 3 , 5 , 1 , 6 , 0 ] given_sum = 5 for i in range ( 0 , len (given_list)): if given_sum - given_list[i] in given_list[i + 1 :]: print (given_list[i], given_sum - given_list[i])

Find the longest common substring between two Strings

 Ex:-  Longest common substring or subsequence between "concatenation" and "decatenation" is "catenation". ex:-  Between "ABABC" and "BABA"  ==> "BAB" Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.Scanner; public class LCS{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter first string:" ); String str1 = sc. next (); System. out . println ( "Enter second string:" ); String str2 = sc. next (); System. out . print (longestCommonSubstring(str1, str2)); } public static String longestCommonSubstring (String str1, String str2){ for ( int i= 0 ; i<str1. length (); i++){ String temp = str1. substring (i); if (str2. contains (temp)) return temp;

Find the largest and smallest number in an unsorted integer array or list

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class LargestSmallest{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 , 3 , 4 , 1 }; int large = Integer. MIN_VALUE ; int small = Integer. MAX_VALUE ; for ( int i= 0 ; i<arr. length ; i++){ if (arr[i]<small) small = arr[i]; else if (arr[i]>large) large = arr[i]; } System. out . println ( "largest:" +large); System. out . println ( "smallest:" +small); } }  Java 2nd Approach 1 2 3 4 5 6 7 8 9 import java.util.Arrays; public class LargestSmallest2{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 ,

Remove the duplicate number from a given integer array or list.

Removing the duplicate elements from an array is the same as printing the unique elements of an array. Notes:- If we write              boolean[ ]  flag = new boolean[ size ] ;               in java, it creates  an array of booleans where false present in all positions by default.               We don't have to assign value to flag array elements exclusively.  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class RemoveDuplicate{ public static void main (String []args){ int [] arr = { 1 , 2 , 3 , 6 , 3 , 3 , 3 , 4 , 2 }; boolean [] flag = new boolean [arr. length ]; for ( int i= 0 ; i<arr. length ; i++) if (!flag[i]) for ( int j=i+ 1 ; j<arr. length ; j++) if (arr[i]==arr[j]) flag[j] = true ; for ( int i= 0 ; i<arr. length ; i++) if

Find all the missing number in a given integer array(or list) of 1 to 10.

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Array1to10{ public static void main (String []args){ int [] arr = { 4 , 5 , 1 , 3 , 6 , 7 , 9 }; int j; for ( int i= 1 ; i<= 10 ; i++){ for (j= 0 ; j<arr. length ; j++){ if (i==arr[j]){ break ; } } if (j==arr. length ) System. out . print (i+ " " ); } } } Python arr = [ 4 , 5 , 1 , 3 , 6 , 7 , 9 ] for i in range ( 1 , 11 ): if i not in arr: print (i, end = " " )

Find all permutations of a string

 Ex:- Permutations of string "abc" are           "abc"  "acb"   "bac"  "bca"  "cab"  "cba"  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class StrPermutation{ public static void main (String []args){ String str = "abc" ; permute(str, "" ); } public static void permute (String str, String ans){ if (str. length ()== 0 ){ System. out . print (ans+ " " ); return ; } for ( int i= 0 ; i<str. length (); i++){ char ch = str. charAt (i); String rem = str. substring ( 0 ,i) + str. substring (i+ 1 ); permute(rem, ans+ch); } } } Python def permute( str , ans): if len ( str ) == 0 : print (ans, end = " " ) return for i in range ( 0 , len ( s