Computer Science, asked by divyanshi261, 1 year ago

write a program in Java to check whether the number is prime or composite


divyanshi261: nothing
divyanshi261: 9th
divyanshi261: what is city princess don't call me that
Lakkie: Guys stop commenting here its just annoying.
divyanshi261: yaa u r ryt

Answers

Answered by Lakkie
2
Your answer :

import java.util.Scanner;

class CheckNumberPrimeOrCompositeClass{
public static void main(String[] args){
//create Scanner class object for user input
Scanner sc = new Scanner(System.in);

// Display message for user understanding
System.out.println("Enter any number");

//Accept input from keyboard
int input_num = sc.nextInt();
// i variable initialize by 2 just
// because every number is divisible by 1
int i=2;
//Loop continues till input_num to 0
while(input_num>0){
// Entered number will be checked by i if we get 0
// then number is not prime then will check from next increment i
if(input_num%i==0)
break;
i++;
}

if(input_num == i)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
}

Output :

Run(1)
D:\Java Articles>java CheckNumberPrimeOrCompositeClass
Enter any number
7
Number is prime

Run(2)
D:\Java Articles>java CheckNumberPrimeOrCompositeClass
Enter any number
10
Number is not prime


Hope this helps.

Have a nice day =)

divyanshi261: Welcm
divyanshi261: yaa
divyanshi261: hmm
divyanshi261: hello
divyanshi261: I am fyn don't call me princess
Answered by Rituraj04
0

Answer:

I  have uploaded the answer in a video you can check that with comments

check out CyberDream channel on y outube you will

find  may programs threre

please subscribe

Similar questions