Data Structures and Algorithms : Basic Programs : ================== if-else Functions & How to call functions loops Standard programs Patterns Arrays Strings ====================== Write a program to print wheather a number is Even or Odd Even numbers : 0, 2 , 4, 6, 8 Odd numbers : 1,3, 5, 7,9... if a number is divisible by 2 then the number said to be Even else odd % Modulus opearator it returns remainder 21%2==1 (odd if remainder and even if remainder 0) 22%2==0 23%2==1 =============================== Instead hard coding I want to take input from the end user and map them to our local variables =============================== package test; import java.util.*; public class Checker { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); int n=scan.nextInt(); if(n%2==0) { System.out.println("n is an even number"); }else { System.out.println("n is an Odd number"); } } } ======================================= 2. Write a program to print n natural number for(initialization ; conditioncheck;incr/decr){ } 1.initialization 2.condition check 3.statements 4.increment/decrement 2,3,4 steps will repet till your condition got failed ================================================= import java.util.*; public class Checker { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); System.out.println("Please enter the value of N"); int n=scan.nextInt(); System.out.println("The Values are"); for(int i=1;i<=n;i++) { System.out.println(i); } //console : 1,2,3 //i=1 ; 1<=50 //i=2 2<=50 //i=3 3<=50 } } ====================================================== 3. Wap to print all the even numbers till n we need to apply filter if(i%2==0) sop("print even number") ======================================= import java.util.*; public class Checker { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); System.out.println("Please enter the value of N"); int n=scan.nextInt(); System.out.println("The Values are"); for(int i=1;i<=n;i++) { if(i%2==0) { System.out.println(i); } } //console : 1,2,3 //i=1 ; 1<=50 //i=2 2<=50 //i=3 3<=50 } } =============================================== 3 Programs ================================================ Prime number I will explain in 3 ways by optimizing the iterations 4th Approach based on binary search The number having 2 factors is called prime number Those 2 factors are 1 and itself will run a loop from 1 to <=n will calculate count of all factors if x is factor of n what is the basic requirement : n%x==0 code : for(int i=1;i<=n;i++) { if(n%i==0) { count=count+1; } } if(count==2) { System.out.println("It is a prime number"); }else { System.out.println("It is not a prime number"); } ============================ n=5 i= 1 to 5 5%1==0 (crct) count=1 5%2!=0 count=1 5%3!=0 count=1 5%4!=0 count=1 5%5==0 count=2 ================================= n=1 lakh how many iterations loop is going 1 lakh 50k iterations response time of Amazon has down by few milli seconds number 2 to n/2 23/2 = 2 to 11 if you find a factor between 2 to n/2 count+=1 ====================================== if(n<=1) { System.out.println("It is not a prime number"); }else { for(int i=2;i<=n/2;i++) { if(n%i==0) { count=count+1; break; } } if(count==0) { System.out.println("It is a prime number"); }else { System.out.println("It is not a prime number"); } } ============================= if n=23 we will check from 2 to 23/2 = 11 if(count==0) sop("It is a prime number") ================================ 1 lakh to 50k 1 crore to 50 lakhs 2 to sqrt(100) 1 to 100 2 to 50 2 to 10 =========================== 17 ==> 4 2 to 4 17%2!=0 17%3!=0 17%4!=0 if(count==0) ======================= 10000000 10000=10^7 Square root(some big number)= 1 lakh ========================= Binary Search class mid=1 + some big/2 mid*mid==number ========================== Arrays , Strings , Map ,Stack , Queue , LL , Trees , GrapSorting algos , searching , Greedy , Dynamic programming , Recursion 2 pointer algos , sliding window algos ===============================