For Loop Syntax

for(Initialization; Condition; Updation)
{
  Loop Work
}

Print Odd numbers in For loop

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();
        int count = 1;
        
        for(int i=1;i<number;i=i+2){
          System.out.print(i + " ");                          
        }
        
    }
}

Input

10

Output

1 3 5 7 9

Factors
An Integer X is a factor of N if X divides N with no reminder

  1. 2 is factor of 4
  2. 4 is factor of 24

Factors of 10 are 1,2,5,10 leaving reminder 0
Factors of 24 are 1,2,3,4,6,8,12,24 leaving reminder 0

Minimum factor is the least factor which would be always 1
Maximum factor would be always N

All Factors lie between [1 to N]

Print Factors of a number

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();
        int count = 1;
        
        for(int i=1;i<=number;i++){
          if(number%i == 0){
             System.out.println(i);   
          }
        }
        
    }
}

Input

24

Output

1 2 3 4 6 8 12 24 

Check the number is prime number

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();
        int factors = 0;
        
        for(int i=1;i<=number;i++){
          if(number%i == 0){
             factors++;   
          }
        }

     if(factors == 2){
       System.out.println("Prime No");
     }
        
    }
}

Input

13

Output

Prime No

Now in the above code I am checking the factors is equals to 2 and deciding whether the input is
prime no or not

Imagine you are giving 12 as input for above code

Input :- 12


         i               factor
	 1                  1
	 2                  2
	 3                  3-----> You can break here as we already know factor is greater than 2
	 4                  4
	 5                  4
	 6                  5
	 7                  5
	 8                  5
	 9                  5
	 10                 5
	 11                 5
	 12                 6

Refactoring the above for loop by adding break conditon

.
.
 for(int i=1;i<=number;i++){
     if(number%i == 0){
         factors++;   
      }
  }

  if(factors > 2){ //Break Factor 
     break;
  }
.
.

Using break factor in for loop

public class CheckPrimen {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();
        int factors = 0;

        for (int i = 1; i <= number; i++) {
            if (number % i == 0) {
                factors++;
            }

            if(factors > 2){ //Break Factor 
                break;
            }
        }

        if(factors == 2){
            System.out.println("Prime No");
        }else{
            System.out.println("Not a Prime No");
        }
    }
}

Input


Output


Print Odd numbers in For loop


Input


Output


	

While Statement

  1. Initialization
  2. Condition
  3. Task
  4. Updation
.
.
int cnt = 1; //Initialization

while(cnt < 5) //Condition
{
 SOP("Hello"); //Task
 cnt++; //Updation
}

.
.

While loop becomes infinite loop if the updating has no effect on while loop condition

Print the numbers divisible by 4 till N
Approach 1

import java.util.Scanner;

public class displayWhile {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int count = scn.nextInt();
        int cnt = 4;

        while(cnt < count){
            if(cnt % 4 ==0)
             System.out.println(cnt);

            cnt++;
        }
    }
}

Approach 2

import java.util.Scanner;

public class displayWhile {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int count = scn.nextInt();
        int cnt = 4;

        while(cnt < count){
            System.out.println(cnt + " ");
            cnt+=4;
        }
    }
}

Input

40

Output

4 8 12 16 20 24 28 32 36 

Approach2 is faster and efficient since count in while jumps in multiples of 4

Printing perfect Square

import java.util.Scanner;

public class PerfectSquare {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int cnt = 1;

        while(cnt*cnt < n){
            System.out.println(cnt*cnt + " ");
            cnt++;
        }
    }
}

Input

30

Output

1 4 9 16 25

Printing last character in string

import java.util.Scanner;

public class PrintIntval {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();
        Character[] arrChar = new Character[]{};

        String strCount = String.valueOf(number);
        int index = strCount.length()-1;

        while(index >= 0 ) {
            System.out.println(strCount.charAt(index));
            index--;
        }
    }
}

Input

1365

Output

5
6
3
1

Approach 2

import java.util.Scanner;

public class PrintIntval {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();

        while(number> 0 ) {
            System.out.println(number%10);
            number = number/10;
        }
    }
}

Input

1365

Output

5
6
3
1
-----------------------------------------------------
 n           n>0           n%10         n=n/10   
----------------------------------------------------
1365          Y             5            136
136           Y             6            13
13            Y             3            1
1             Y             1            0
0             N - Loop Stops 

Now how will you address if input is 0 por negative and you should get output?

To address above.. .

public class PrintIntval {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int number = scn.nextInt();

        if(number < 0)
            number *= -1;

        if(number == 0){
            System.out.println(number);
        }else {
            while (number > 0) {
                System.out.println(number % 10);
                number = number / 10;
            }
        }
    }
}

Input

-1365

Output

5
6
3
1