Computer Science, asked by suraj24680, 9 months ago

write a java program to check weather the given number is composite or not​

Answers

Answered by sahil10august
0

// A optimized school method based C++ program to check

// if a number is composite.

#include <bits/stdc++.h>

using namespace std;

bool isComposite(int n)

{

// Corner cases

if (n <= 1) return false;

if (n <= 3) return false;

// This is checked so that we can skip

// middle five numbers in below loop

if (n%2 == 0 || n%3 == 0) return true;

for (int i=5; i*i<=n; i=i+6)

if (n%i == 0 || n%(i+2) == 0)

return true;

return false;

}

// Driver Program to test above function

int main()

{

isComposite(11)? cout << " true\n": cout << " false\n";

isComposite(15)? cout << " true\n": cout << " false\n";

return 0;

}

Answered by shivamdwivedi2310
1

Explanation:

If you are using blue j hope this will help

import java.util.*

class brainly

{

public static void main()

{int flag=0;

Scanner Sc = new Scanner (System.in);

System.out.println (" Enter the number to be checked");

Int n =sc.nextInt();

for (int i=1; i<=n ,i++)

{

if (n%i==0)//Checking for divisibility

flag++;

}//For closing

if (flag>2)

System.out.println( "Entered number is a composite number");

else

System.out.println("Enter number was not a composite number");

}//Main closing

}//Class closing

*Hope this helps :-)*

Similar questions