Write a Java-program to Break the loop which number is divisible by 2 and 3 in java ?

Write a Java-program to Break the loop which number is divisible by 2 and 3 in java ?

Program
public class Main
{
    public static void main(String args[])
    {
        int x,n=20;
        for(x=1;x<=n;x++)
        {
            if((x%2==0 && x%3==0))
            {
                break;
            }
            System.out.print(x);
        }
    }
}	

Output:

1
2
3
4
5

Using CommandLine Arguments
public class Main
{
    public static void main(String args[])
    {
        int x,n;
        n=Integer.parseInt(args[0]);
        for(x=1;x<=n;x++)
        {
            if((x%2==0 && x%3==0))
            {
                break;
            }
            System.out.print(x);
        }
    }
}	

Output:

1
2
3
4
5

Using Scanner Class
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        int x,n;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter n value : ");
        n=s.nextInt();
        for(x=1;x<=n;x++)
        {
            if(!(x%2==0 && x%3==0))
            {
                brek;
            }
            System.out.print(x);
        }
    }
}	

Output:

Enter n value : 20
1
2
3
4
5





More Questions


91 . Global and Local Variable Program
92 . MaxNumber Program Using No return type with No Parametarized method
93 . MaxNumber Program Using No return type with Parametarized method
94 . MaxNumber Program Using return type with No Parametarized method
95 . MaxNumber Program Using return type with Parametarized method
96 . Factorial Program Using No return type with No Parametarized method
97 . Factorial Program Using No return type with Parametarized method
98 . Factorial Program Using return type with No Parametarized method
99 . Factorial Program Using return type with Parametarized method
100 . Static variable Program
101 . Static Method Program