Write a Java-program count the number of divisible numbers in a given range(1-100)

Write a Java-program count the number of divisible numbers in a given range(1-100)

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

Output:

50

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

Output:

50

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

Output:

Enter n value : 2
50





More Questions


47 . Write a Java-program to display Even Number or Odd Number based on the N value using single for loop
48 . Write a Java-program count the number of divisible numbers in a given range(1-100) ?
49 . Write a Java-program count the number of divisible numbers in a given range(1-100) without loop
50 . Write a Java-program to print Factorial Number using for loop ?
51 . Write a Java-program to print Factorial of upto N Numbers using for loop ?
52 . Write a Java-program to print sum of natural numbers using for loop ?
53 . Write a Java-program to print sum of Even numbers using for loop ?
54 . Write a Java-program to print sum of Odd numbers using for loop ?
55 . Write a Java-program to print sum of even or odd numbers based on the N value using for loop ?
56 . Write a Java-program to print sum of even or odd numbers based on the N value using single for loop ?
57 . Write a Java-program to display the Table using for loop ?