Sunday 4 September 2011

Prime Number

Problem--> Write a program which takes any number and checks whether it is prime or not.

Solution--> 
Logic-->  Before writing the program, you should understand the logic of the program. First of all, what is a prime number? 
 Any Number which is divisible by itself and 1 is called a prime number. So you have to check  if the number is divisible by any other number( starting from 2 upto one less than the number). If it is divisible, then it is not a prime number, else, it is.

          Open Eclipse IDE. Create a new Java class in the existing project. 

          Name the file "PrimeNumber.java". 

           If you have not learnt how to use Eclipse IDE, check out this tutorial.
   
Code--> 
 class PrimeNumber {
    //This is the starting point of the program 
public static void main(String[] args) {
  int number=29;// This variable stores the number to be checked 
  boolean flag=true;// This is a boolean flag     
  //Checking the number against each integer from 2 to (number-1)
                forint i=2; i<number; i++){
        //If the number is completely divisible by any other number then this block is executed 
           if(number%i==0){ 
        //flag is set to false denoting this is not a prime number 
        flag=false; 
        //the for loop exits completely 
        break; 
          } //end of if condition
              } //end of for loop
        //Checks the flag value to decide whether the number is prime or not 
        if(flag==false){ 
        System.out.println("This is not a prime number"); 
        } 
        else{ 
        System.out.println("This is a prime number"); 
        } 
    } //end of main function
} //end of PrimeNumber Class



 Save and run the program.



Explanations--> 

  • Flag variable-->
                         A flag variable is a boolean variable that can take two possible values, i.e. true or false. It is like a switch which can be either in on  state or off state. 
                         In this case the flag is set to true if the number is prime number, else it is set to false. By default, it is set to true.
  • for Loop-->
                       Loop statements are an important part of any programming language. The simplest form of for loop is shown here:
                   
                    for(initializationconditioniteration){
                             statement;
                   }     

                       The initializtion portion of the loop sets a loop control variable to an initial value.
 In our program,  variable i is the loop control variable and is set to an initial value=2.
                      
                     The condition portion is a Boolean expression that tests the loop control vaiable. If the outcome of the test is true, the for loop continues to iterate. If it is false, the loop terminates.
                   The iteration expression determines how the loop control variable is changed each time the loop iterates.

In our program, the condition portion checks if the control variable i is less than the value of the input variable number. The iteration portion increment control variable i by 1 after each iteration.

  • i++--> This is equivalent to i=i+1. Again ++i is also equivalent to i=i+1. The difference between i++ and ++i is that i++ is a post-increment operator, i.e. increment occurs after the statement has been executed and ++i is a pre-increment operator, i.e. increment occurs before the statement has been executed.
  • if Condition--> The if statement is Java's conditional branch statement. It can be used to route program execution through two different paths. Here is the general for of the if statement:
              if(condition) statement1;
              else statement2;
        Here, each statement may be a single statement or a compound statement enclosed in curly braces(that is, a block). The condition  is any expression that returns any boolean value. The else clause is optional.
        The if works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed. In no case, both statements will be executed.
        In our program, if flag is true, then print that the number is a prime number, else print that the number is  not a prime number.

        
  • The Modulus Operator ( %)-->
                     The modulus operator, %, returns the remainder of a division operator. It can be applied to floating-point data-types as well as Integer data-types. It is another type of arithmetic operator.

E.g.       10%3=1
              50%4=2
              30%10=0

                    

Related Java Problems--> Here is a list of programs for you to try at home. You should try the programs first & only check the solution if you are unable to solve any.

  • Fibonacci Series--> In mathematics, the Fibonacci numbers are the numbers in the following integer sequence:
    0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;  i.e. any number  is the sum of previous two numbers. 
    Write a program that takes the size of the fibonacci series to be printed and prints the series in the manner shown above.
    Example:- If the input value is 6, output should be 0,1,1,2,3,5.
  • Calculate Circle Perimeter-->Write a program that takes in the radius of a circle and produces the perimeter of the circle.
  • Swap number--> Write a program that takes in two numbers and swaps the number without using a third variable.
  • Factorial--> Write a program that takes a number and produces its factorial.


>>Next Tutorial>>      

_____________________________________________________________________________________