Skip to main content

Posts

Showing posts with the label TCS

Placement Problem 1

 Don't jump directly to the solution.  Read the problem carefully and try to solve it on your own. Solution : Java Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.Scanner; public class PlacementProblem1 { public static void main (String[] args) { Scanner sc = new Scanner(System. in ); int t = sc. nextInt (); while (t--> 0 ) { int size = sc. nextInt (); int target = sc. nextInt (); int arr[] = new int [size]; for ( int i= 0 ; i<arr. length ; i++) arr[i] = sc. nextInt (); for ( int i= 0 ; i<arr. length ; i++) { boolean flag = false ; for ( int j=i+ 1 ; j<arr. length ; j++) { if (arr[i]+arr[j]==target) { System. out . println (i+ " " +j); ...

Find the nth term in the mixture of two series

 Consider the series:- 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64 ...........and so on. Find the nth term in the given series ?  ex:-   9 th term       print 16            6 th term       print  9 Note:- We have to print only nth term not the whole series up to nth term. Explanation:- The given series is a combination of two geometric progression series. First series ( odd positions ) :-  1, 2, 4, 8, 16, 32, 64, ........and so on.                                                               2 0  ,   2 1  ,   2 2  ,   2 3  ,   2 4  ,   2 5  ,……. Second series (  even positions ) :- 1, 3, 9, ...

Separate digits, characters and special characters from a mixed string

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.Scanner; public class Separate{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a mixed string:" ); String mixed = sc. next (); String chars= "" , digits= "" , spec_chars= "" ; for ( int i= 0 ; i<mixed. length (); i++){ char ch = mixed. charAt (i); if (ch>= 'a' && ch<= 'z' || ch>= 'A' && ch<= 'Z' ) chars += ch; else if (ch>= '0' && ch<= '9' ) digits += ch; else spec_chars += ch; } System. out . println ( "Characters: " +char...

Check whether a number is Armstrong or not

 Java Python

Print half pyramid with numbers

 Java Python

Half pyramid star pattern

 Java Python

Convert decimal value to octal value

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

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)

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 nu...

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 ch...

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" retur...

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...