To stop the geared DC motor based on the number of rotation.

I am doing a small project in which I need to stop rotating the dc geared motor based on its number of rotation (in my case I need only 2 rotation), after two rotation it should stop rotating. I am using Arduino Nano and DC geared motor.I have read about using rotary encoder but I am not using it. Can I control the number of rotation using coding?

You need to form a control loop in your code so that the motor’s drive is determined by the difference of where it is compared to where you want it to me. An encoder can tell you the actual position, you compute the motor drive and direction by comparing this to the current commanded position (that’s something you make up). A PID library might be imployed for this.

You need a motor driver too, not just an encoder.

Without any knowledge of the motor’s position you cannot control position.

I am using this motor (image is attached).I want to stop the motor rotation by (1) using number of revolution it made or (2) after some delay when the button is pressed.
This code immdiately stops the motor rotation i think the dealy is not working.

My code is:

const int  buttonPin = 6;    // the pin that limitswitch is attached to
const int  motorPin = 9;

int buttonState = 0;         // current state of the button


void setup() {

  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(motorPin, OUTPUT);

}


void loop() {

  buttonState = digitalRead(buttonPin);



    if (buttonState == 1 ){    //button pressed
      delay(2000);
      Serial.println("Motor Stop");
      digitalWrite(motorPin, LOW);
     }

    if(buttonState == 0){    //button released
        Serial.println("Motor rotating ");
        digitalWrite(motorPin, HIGH);
     }

}

Do you have a 10k pulldown resistor from buttonPin to GND? If not, the pin is "floating" between HIGH and LOW when button is not pressed and may cause false presses.
Have you thought about an opto interrupter and a flag on the output shaft to stop the motor?
https://www.amazon.com/Opto-interrupter-sensor-module-for-arduino/dp/B01H2JP5OM/ref=sr_1_4?keywords=opto+interrupter&qid=1574325468&sr=8-4
https://www.amazon.com/s?k=opto+interrupter&ref=nb_sb_noss_1

const int  buttonPin = 6;    // the pin that limitswitch is attached to
const int  motorPin = 9;

int buttonState = 0;         // current state of the button


void setup() {

  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(motorPin, OUTPUT);

}


void loop() {

  buttonState = digitalRead(buttonPin);



    if (buttonState == 1 ){    //button pressed
      delay(2000);
      Serial.println("Motor Stop");
      digitalWrite(motorPin, LOW);
     }

     else {    //button released
        Serial.println("Motor rotating ");
        digitalWrite(motorPin, HIGH);
     }

}

To control the position of DC motor, you SHOULD use a PID controller. See a project on Arduino Project Hub with PID library for Arduino

In practice you might get away with only a P term in the PID loop, which is the simplest approach (well worth trying first). The I term will reduce position error, but tends to reduce maximum response speed. The D term will help with speed, but can produce random jitter movement if overdone.

The performance you can achieve is limited by the position sensing accuracy and latency.