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


	

Comments are closed.