Composite Number in JAVA

Composite Number in JAVA


Composite Number is a positive integer that has at least one positive divisor other than one or the number itself.

Example : 4,6,8 etc are composite number.

Algorithm and Program below :-

ALGORITHM :-
  1. Get the number to check for Composite Number
  2. Hold the number in another variable
  3. Initialize a variable c=0 (let)
  4. Using loop find its factors
  5. For every factor increase the value of c by 1
  6. If c is greater than one print "Composite Number"
  7. If not print "Not a Composite Number"

PROGRAM below :-

import java.io.*;
import java.util.*;
class Composite
{
    public static void main(String args[])
    {
        Scanner in= new Scanner(System.in);
        int n,p,i,c=0;
        System.out.println("Enter Number");
        n= in.nextInt(); // Input number from user
        p=n; // store the entered number in "p" variable
        for(i=2;i<n;i++)
        {
            if(n%i==0) // checking factors of the number
            {
                c++;
            }
        }
        if(c>1) // if factors are greater than 1
        {
            System.out.println("It is Composite Number : "+p);
        }
        else
        {
            System.out.println("It is not Composite Number : "+p);
        }
    } // end of main method
} // end of class

For Complete Explanation watch this video :-
 
 
 
 
Video Link : Composite Number
 
Follow me on Instagram
 
All the Best :)
Keep Learning :) 

Comments

  1. I think the code on the below site is much easier to understand than this one.

    https://solved-programs.blogspot.com/2021/04/composite-number-in-java.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA