Commit 496b3bb8 by Patryk Czarnik

FunkcjaNaTablicach

parent 4b4d0007
package p08_funkcje.zadania;
public class FunkcjeNaTablicach {
static int suma(int[] t) {
int suma = 0;
for(int element : t) {
suma += element;
}
return suma;
}
// kolejna funkcja:
// zwraca sumę tych elementów tablicy, które są liczbami parzystymi
static int sumaParzystych(int[] t) {
int suma = 0;
for(int element : t) {
if(element % 2 == 0) {
suma += element;
}
}
return suma;
}
static int sumaParzystych2(int[] t) {
int suma = 0;
for(int i = 0; i < t.length; i++) {
if(t[i] % 2 == 0) {
suma += t[i];
}
}
return suma;
}
public static void main(String[] args) {
int[] a = {3, 5, 7, 12, 16, 18};
int[] b = {20, 15, 10};
int wynik = suma(a);
System.out.println("suma a: " + wynik);
System.out.println("suma b: " + suma(b));
System.out.println("sumaP a: " + sumaParzystych(a));
System.out.println("sumaP b: " + sumaParzystych(b));
}
}
package p08_funkcje.zadania;
public class PierwszeFunkcjeZWynikiem {
static double poleProstokata(double a, double b) {
return a*b;
}
static double obwodProstokata(double a, double b) {
return 2*a + 2*b;
}
static int max(int a, int b) {
if(a > b)
return a;
else
return b;
}
static int max(int a, int b, int c) {
if(a > b && a > c)
return a;
if(b > c)
return b;
return c;
}
public static void main(String[] args) {
System.out.println(poleProstokata(3, 4));
System.out.println(poleProstokata(5, 7.5));
System.out.println(obwodProstokata(5, 7.5));
System.out.println();
System.out.println(max(3, 4));
System.out.println(max(5, 5));
System.out.println(max(12, 10, 15));
System.out.println(max(15, 10, 15));
System.out.println(max(20, 20, 20));
System.out.println(max(7, 5, 3));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment