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:
You can download the source code:download
what we can use except scanner in the programme
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