Java program for composite numbers

It is a simple program…Here we need to find composite number which is defined as a number which has a positive number has divisor other than 1 and itself…In a simple way we can define a composite number has a number which is not prime number.For programming this we just need to find whether a number has divisor or not.If it doesn’t have a divisor then it is called as prime number.
Let us write the program for it

import java.util.Scanner;
class finding
{
   int flag,n;
   finding()
     {
       System.out.print("Enter range to find the composite numbers:");
       Scanner in=new Scanner(System.in);
       n=in.nextInt();
       for(int i=2;i<=n;i++)
        {
            flag=0;
           for(int j=2;j<i;j++)
             {
                 if(i%j ==0)
                    flag++;
             }
           if(flag!=0)
            System.out.println("one of the composite number is:" + i);
        }
    }
}
class composite
{
   public static void main(String a[])    
      {
           finding ob=new finding();
      }
} 

Output:composite
You can download the source code:download

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Java program for composite numbers

  1. vasundhara goswami says:

    what we can use except scanner in the programme

    • Anuroop D says:

      we can give input to the programme from command line also. But it is not advisable to do so. Because we need to parse that input and separate each word from the other. So, the efficient way is to use scanner class only. You can try BufferedReader class in case you are dealing with files…..Hopes this help s. In case you have further doubts feel free to comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s