Amaliy ish 5-6 topshiriq


Download 50.31 Kb.
Sana19.06.2023
Hajmi50.31 Kb.
#1605676
Bog'liq
Amaliy ish 5-6



Amaliy ish 5-6

  1. topshiriq

12.Natural n soni va n ta haqiqiy sonlardan iborat ketma-ketlik berilgan. Toq o‘rinda turgan sonlar maksimumi va juft o‘rindagilaring minimuni topilsin.
Dastur kodi:
public class Main {
public static void main(String[] args) {
int[] array = {1, 4, 2, 8, 5, 7};
int maxOdd = array[0]; // toq sonlar uchun eng katta son
int minEven = array[1]; // juft sonlar uchun eng kichik son
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0 && array[i] > maxOdd) {
maxOdd = array[i];
} else if (array[i] % 2 == 0 && array[i] < minEven) {
minEven = array[i];
}
}
System.out.println("Toq sonlar uchun eng katta son: " + maxOdd);
System.out.println("Juft sonlar uchun eng kichik son: " + minEven);
}
}
Dastur natijasi:



2-topshiriq
12.Bir o`lchamli sonli massiv elеmеntlarini kvadratlarini yigindisi хisоblansin?
Dastur kodi:
import java.util.Scanner;
public class Eldor{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,s=0;
n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
s+=a[i]*a[i];
}
System.out.println(s);
}
}
Dastur natijasi:



Download 50.31 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling