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 . Write a Java-program to Break the loop which number is divisible by 2 and 3 in java ?
92 . Write a Java-program to print a to z character
93 . Write a Java-program to print A to Z character
94 . Write a Java-program to print character numbers
95 . Write a Java-program to print symbols
96 . Write a Java-program to print ASCII values of a to z
97 . Write a Java-program to print ASCII values of A to Z
98 . Write a Java-program to print ASCII values of Numbers
99 . Write a Java-program to print ASCII values of symbols
100 . Write a Java-program to print global and local variables
101 . Write a Java-program to print Max Number using no return type with no parameterized method