can someone help me mix two sketch

Hi, I would like to be able to put these two programs together in one.

#include <Servo.h> 

Servo servo;

const int buttonPin = 4;
const int buttonPin1 = 5;
int val;
int buttonState = 0;
int buttonState1 = 0;



void setup() 
{ 
  servo.attach(9);
  pinMode(buttonPin, INPUT);    
  pinMode(buttonPin1, INPUT);     
} 

void loop() 
{ 
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    val = 100;  
  }  

buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {     
    val = 0;  
  } 

    servo.write(val);
  delay(15); 
  
}
#include <Servo.h>   
Servo servo;     
int trigPin = 2;    
int echoPin = 3;   
int servoPin = 9;



long duration, dist, average;   
long aver[3];   


void setup() {       
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);       
    delay(1000);
    servo.detach();
} 

void measure() {  
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1;    
}
void loop() { 
  for (int i=0;i<=2;i++) {   
    measure();               
   aver[i]=dist;            
    delay(50);              
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    

if ( dist<10 ) {

 servo.attach(servoPin);
  delay(1);
 servo.write(100);  
 delay(5000);      
 servo.write(0);    
 delay(1000);
 servo.detach();      
}
}

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html