Mavzu: java tilida takrorlanuvchi jarayonlar va parametr bo‘yicha dasturlash


Natija n=10 s=-749.3400793650793


Download 0.73 Mb.
bet4/5
Sana01.03.2023
Hajmi0.73 Mb.
#1240900
1   2   3   4   5
Bog'liq
4- dars

Natija n=10
s=-749.3400793650793

Takrorlanuvchi jarayonlarni dasturlash vaqtida takrorlanish qadami a dan boshlanib qadam x bilan b gacha bajarilish ham mumkin. Bu jarayonni quyidagi dastur orqali tushunib olamiz.
Misol. n dan m gacha sonlarning 3-darajalarining yig’indisini x qadam bilan hisoblash dasturini tuzing.

package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner kirit=new Scanner(System.in); int n,m,x; double s=0; System.out.print("n="); n=kirit.nextInt(); System.out.print("m="); m=kirit.nextInt(); System.out.print("x="); x=kirit.nextInt(); for(int i=n;i<=m;i+=x){ s+=Math.pow(i,3); } System.out.println("s="+s); } }

Natija n=5 m=20 x=5 s=12500.0

Misol. Sonning natural bo‘luvchilar soni va natural bo‘luvchilar yigindisini hisoblash dasturi tuzilsin.
Dastur algoritmi, berilgan sonni yarmigacha natural sonlarga bo‘lib chiqiladi qaysi songa qoldiqsiz bo‘linsa usha son olinadi va oxirida shu sonning uzi ham hisobga olinadi.

package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner kirit=new Scanner(System.in); int n,p=0; double s=0; System.out.print("n="); n=kirit.nextInt(); for(int i=1;i<=n/2;i++){ if(n%i==0) { s+=i; p++; } } s+=n; p++; System.out.println("p="+p); System.out.println("s="+s); } }

Natija n=4 p=3 s=7.0

3) Ichma ich sikllarni tashkil qilish
Har qanday dasturlash tillarida parametrli sikl operatori yoritilgan vaqtda, albatta ichma ich sikllarni tashkil qilish keltirib o‘tiladi. Java dasturlash tilida ham ichma ich sikllarni tashkil qilish imkoniyati mavjud. Ichma ich sikllarni for sikl operatori orqali hosil qilinadi. Ichma ich sikllar – bu takrorlanish ichida yana takrorlanish hosil qilinadi, takrorlanishning har bir qadamida, ichki sikl yana n marta takrorlanishni amalga oshiradi. Ichma ich sikllarni tashkil etish jarayonini misollar yordamida tushuntirib o‘tamiz.
Misol

package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner kirit=new Scanner(System.in); int n; double s=0,p; System.out.print("n="); n=kirit.nextInt(); for(int i=1;i<=n;i++){ p=1; for(int j=1;j<=n;j++){ p*=(Math.cos(j)+i*i*i); } s+=p; } System.out.println("s="+s); } }


Download 0.73 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5




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