Featured

    Featured Posts

Program to Check Whether a Number is Prime or Not

v What us Prime Number

 §  If a whole number greater than 1, is divisible by the 1 and itself then it is known as Prime Number.

 §  A prime number is a number which is divisible by only two numbers: 1 and itself. So, if any number is divisible by any other number, it is not a prime number.

Different View of the Logic

 §  The number cannot divisible by any number between 2 to n-1


Example-1:

import java.util.Scanner;

public class PrimeNumber {



     static boolean isPrime(int n) {

              boolean flag = false;

              if(n>1) {

              for(int i=2;i<=n-1;i++) {

            // condition for nonprime number

                        if(n%i==0) {

                                  flag=true;

                                  break;

                        }

              }

              }

              if(!flag)

                        return true;

              else

                        return false;

     }

     public static void main(String[] args) {

              // TODO Auto-generated method stub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");

     }

}



Example-2:

§  The number can’t divisible by any number between 2 to n/2.

import java.util.Scanner;

public class PrimeNumber {

          static boolean isPrime(int n) {

                    boolean flag = false;

                             int i=2;

                  while(i <= n/2)

                  {

                      // condition for nonprime number

                      if(n% i == 0)

                      {

                          flag = true;

                          break;

                      }



                      ++i;

                  }



                  if (!flag)

                      return true;

                  else

                      return false;

              }

         

                   public static void main(String[] args) {

                   // TODO Auto-generated method s*- nhgtub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");



          }

}

Example-3:

§  The number can’t divisible by any number between 2 to sqrt(n)

import java.util.Scanner;



public class PrimeNumber {



          static boolean isPrime(int n) {

                    boolean flag = false;

                             int i=2;

                  while(i <= Math.sqrt(n))

                  {

                      // condition for nonprime number

                      if(n% i == 0)

                      {

                          flag = true;

                          break;

                      }



                      ++i;

                  }



                  if (!flag)

                      return true;

                  else

                      return false;

              }

         

                   public static void main(String[] args) {

                   // TODO Auto-generated method stub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");



          }

}

Example-4:

§  A number which having only two factors is known as prime number

for e.g.: -

3 having two factor 1 ,3 so 3 is a prime number

6 having more than two factor 1,2,3,6 so 6 is not a prime number.

import java.util.Scanner;



public class PrimeNumber {



          static int factorCount(int n) {

                   int count=0;

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

                             if(n%i==0)

                                      count++;

                   }

                   return count;

          }

          static boolean isPrime(int n) {

                   int c=factorCount(n);

                   if(c==2)

                             return true;

                   else

                             return false;

                            

          }

          public static void main(String[] args) {

                   // TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

System.out.println("Enter number");

int n=sc.nextInt();

boolean res=isPrime(n);

if(res==true)

          System.out.println(n+" is a Prime Number!");

else

          System.out.println(n+" is not a Prime Number!");

          }



}


Project Structure of Angular Project


Project Structure of Angular Project

 §  In this Session we will discuss all the files and folders in the Angular project



 §  One of the easiest ways to create a working angular project, is by using the Angular CLI. The following Angular CLI command creates a working Angular Project out of the box

ng new AngularDemo

 Ã˜ ng is the Angular CLI

 Ã˜ new for creating a new application

 Ã˜ AngularDemo is the name of your angular application




 §  A new folder with name AngularDemo is created.let's look the files in a project folder and how the execution flows in visual studio code we have the AngularDemo project folder there are lot of files so I am going to discuss only a few of them that are really important at this stage the first one is the package.json file.



 §  This file contains the dependency and the dev dependencies which are nothing is the libraries and module that are required for angular application to work.

 §  The package that it is listed here it installed when you run the command

ng new AngularDemo

 §  All the packages get installed inside the node module folder.

 §  we also have some of the scripts that can be executed ng serve command

then this is one of them which runs are application you can also execute npm start which will internally call ng serve command.

node_modules folders   



Data Structure & Algorithms - Tower of Hanoi




Data Structure & Algorithms - Tower of Hanoi

§  In this blog tutorial we will discuss about Tower of Hanoi problem so I am going to show you how to write an algorithm for Tower of Hanoi by taking a small example as well as large example with 3 disc.

§  The problem is there are three Towers given and one of the tower is having a stack of disc in the decreasing order of the sizes from bottom towards top we have to move all this disc from Tower A to Tower C and at any moment of time a larger disc should not be kept over small disc so this will not be easy to transfer all this disc so for  help one more auxiliary Tower is given .

§  First of all, we solve this problem by taking a small example with 2 disc.

Step-1:
 §  Move a disc from TowerA to TowerB using TowerC.

Step-2:
 §  Move a disc from TowerA to TowerC.
   Step-3:
 §  Move a disc from TowerB to TowerC using TowerA.

 v Now solve problem when there are three disc are given. if there are three disc given now we can call this problem little bit complex problem.


   Move 2 disc from TowerA to TowerB using TowerC




 §  move two disc from A to B using C actually we have to move one disk at a time but here I am saying move two disc. how to move two disk from one tower to another Tower already have seen it so you can apply that procedure  so its means this statement is recursive statement so it's not talking about one disc at a  time here is talking two disc at a time .so how to move to 2 disc  I have already seen the procedure so let us see two disc move hear this is done.

       Move 2 disc from TowerA to TowerB using TowerC




 §  Move a disc from TowerA to TowerC


 §  Move 2 Disc from TowerB to TowerC using TowerA


 §  Now these three steps are very important for devising a recursive procedure for Tower of Hanoi problem.
 §  If there are n disc than those three steps can be written like this.
Ø Move n-1 disc from TowerA to TowerB using TowerC.
Ø Move a disc from TowerA to TowerC
Ø Move n-1 disc from TowerB to TowerC using TowerA

 §  this is recursive algorithm for Tower of Hanoi.
A recursive algorithm for Tower of Hanoi can be driven as follows −
TowerA - Source Tower
TowerB - Auxiliary Tower
TowerC- Destination Tower
n- number of disc

START
Procedure TOH(n, TowerA, TowerB, TowerC)
   IF disc == 1, THEN
      move disk from TowerA, to TowerC
   ELSE
      Move n-1 disc from TowerA to TowerB using TowerC.
      TOH(n - 1, TowerA ,TowerC ,TowerB)     // Step 1
      move disk from TowerA to TowerC              // Step 2
     Move n-1 disc from TowerB to TowerC using TowerA
      Hanoi(disk - 1, TowerB , TowerA , TowerC)     // Step 3
   END IF
END P
 
Program for Tower of Hanoi

def TOH(n,A,B,C):
    if n>0:
        TOH(n-1,A,C,B)
        print("[{}-{}]".format(A,C))
        TOH(n-1,B,A,C)

TOH(3,1,2,3)
Output:
[1-3]
[1-2]
[3-2]
[1-3]
[2-1]
[2-3]
[1-3]








www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews