Showing posts with label Prime or Composite .. Show all posts
Showing posts with label Prime or Composite .. Show all posts

Tuesday, 13 January 2015

Cpp Program to find whether a number is prime or composite.

This is a basic program in Cpp which checks whether the number is prime number or composite number.
A number is said to be prime if it is divided by 1 and itself other numbers are called composite however the numbers 0 and 1 are neither prime or composite.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int i,n;
    cout << "Enter any number : \t" << endl;
    cin  >> n ;

    if(n == 0 || n == 1)
        cout << n << " is neither PRIME nor COMPOSITE" << endl;

    for ( i = 2 ; i <= n-1 ; i++ )
    {
      if ( n%i == 0 )
      {
         cout << n << " is COMPOSITE number" << endl;
         break;
      }
    }
    if ( n == i )
      cout << n << " is PRIME number" << endl;

    return 0;
}

OUTPUT :
Cpp - Prime Or Composite 
Cpp  - Prime Or Composite











Cpp - Prime Or Composite

Monday, 5 January 2015

Java Program to check whether a number is Prime Or Composite


This is a basic program in Java which checks whether the number is prime number or composite number.

PROGRAM :


 package codingcorner.in;

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 " );
  
 }
}
OUTPUT :