Thursday 28 January 2016

Write a program in java to print the composite numbers within range.

Composite Number :

A Composite Number is a positive integer that has at least one positive divisor other than one or the number itself. In other words, a Composite Number is any positive integer greater than one that is not a Prime Number. Write a Java program that Input the Number And Find Number is Composite or Prime.

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

Output:
Enter range to find the composite numbers:
20

one of the composite number is:4
one of the composite number is:6
one of the composite number is:8
one of the composite number is:9
one of the composite number is:10
one of the composite number is:12
one of the composite number is:14
one of the composite number is:15
one of the composite number is:16
one of the composite number is:18
one of the composite number is:20

No comments:

Post a Comment