התרגילים שעשינו בכיתה

מציאת מינימום ומקסימום

int num,max,min;

System.out.println("please insert the first number");

num = reader.nextInt();

max = num;

min = num;

for (int i=0;i<9;i++){

     System.out.println("please insert the next number");

     num = reader.nextInt();

     if (num > max){

          max = num;

     }

     if (num < min){

          min = num;

     }

}

System.out.println("max = "+max+" min = "+min);

 

מספר זוגות עוקבים

 

int count = 0,prev,curr;

System.out.println("please insert the first number");

curr = reader.nextInt();

for (int i=0;i<9;i++){

     prev = curr;

     System.out.println("please insert the next number");

     curr = reader.nextInt();

     if (curr == prev+1){

          count++;

     }

}

System.out.println("count = "+count)

 

מקום ראשון + שני

int max,sec,num;

System.out.println("please insert the first number");

max = reader.nextInt();

System.out.println("please insert the second number");

num = reader.nextInt();

if (num > max){

     sec = max;

     max = num;

}

else{

     sec = num;

}

for (int i=0;i<9;i++){

     System.out.println("please insert the next number");

     num = reader.nextInt();

     if (num > max){

           sec = max;

           max = num;

     }

     else if (num > sec){

           sec = num;

     }

}

System.out.println("first = "+max+" second = "+sec);