Computer Science, asked by suyashdubey1715, 1 year ago

WAP to accept to number and check whether it is prime or composite

Answers

Answered by sakshamrai8
0
#include<stdio.h>
int main()
{
int i,n,c=0;
printf ("Enter a number \n");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if (c==2)
printf ("The number is PRIME");
else
printf ("The number is COMPOSITE");
return 0;
}
In JAVA
import java.util.Scanner;

public class PrimrOrComposite {
public static void main(String[] args) {
int a, b;
Scanner scan = new Scanner(System.in);

System.out.println("Enter a number to check Prime or Composite ");
b = scan.nextInt();

scan.close();

if (b == 0 || b == 1)
System.out.println(b +" is neither Prime nor Composite" );

for (a = 2; a <= b - 1; a++) {
if (b % a == 0) {
System.out.println(b +" is Composite number" );
break;
}
}
if (b == a)
System.out.println(b +" is Prime number " );

}
}
Similar questions