Do while - «gacha» – охirida qo`yilgan shart bilan takrоrlanuvchi jarayonni tashkil qilish quydagichadir. - Do while - «gacha» – охirida qo`yilgan shart bilan takrоrlanuvchi jarayonni tashkil qilish quydagichadir.
2) For operatori takrorlanish sonining boshlang’ich qiymatidan boshlanib shart buzilish holatigacha qadam bo’yicha takrorlanishni amalga oshiradi.
Misol. 1 dan n gacha sonlarning 4-darajalarini hamda ularning yig’indisi va ko’paytmasini 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; double s=0,p=1; System.out.print("n="); n=kirit.nextInt(); for(int i=1;i<=n;i++){ s+=Math.pow(i,4); //yig'indi p*=Math.pow(i,4); //ko'paytma System.out.println(i+"^"+i+"="+i*i*i*i); } System.out.println("s="+s); System.out.println("p="+p); } }
|
Natija n=5 1^1=1 2^2=16 3^3=81 4^4=256 5^5=625
s=979.0
p=2.0736E8
|
package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner kirit=new Scanner(System.in); int n,l=1; double s=0,p=1; System.out.print("n="); n=kirit.nextInt(); for(int i=1;i<=n;i++){ l*=i; //maxraj_faktorial p*=Math.pow(Math.cos(2),i); //surat s+=p/l; } System.out.println("s="+s); } }
|
Natija n=10 s=-0.45130851838657265
|
Takrorlanish a dan boshlab qadam -1 ga teng bo‘lgan holatda ham for operatorini ishlatish mumkin bunda boshlang’ich qiymat a dan boshlanib qadam -1 bo’yicha amalga oshiriladi. Bu jarayonni quyidagi dastur orqali tushunib olamiz.
Misol. Quyidagi yig’indini 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,p=1,l=0,k=-1; double s=0; System.out.print("n="); n=kirit.nextInt(); for(int i=n;i>=1;i--){ l++; //maxraj s+=k*Math.pow(i,3)/l; k*=-1; //ishora } System.out.println("s="+s); } }
|
|
Do'stlaringiz bilan baham: |