17-Laborotoriya Mavzu: for, while va do-while operatoriga doir dasturlar tuzish


Download 159 Kb.
Sana19.06.2023
Hajmi159 Kb.
#1606031
Bog'liq
17-laborotriya


17-Laborotoriya
Mavzu: for, while va do-while operatoriga doir dasturlar tuzish
1-misol
while operatori bilan sodda dastur:
Quyidagi misol bilan, 1 dan 10 gacha bo'lgan sonlarni chiqarishni ko'rsataman:
#include
using namespace std;

int main() {


int i = 1;
while (i <= 10) {
cout << i << " ";
i++;
}
cout << endl;
return 0;
}

2-misol. for operatori bilan sodda dastur:
Quyidagi misol bilan, 1 dan 10 gacha bo'lgan sonlarni chiqarishni ko'rsataman:
#include
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}

3-misol . do-while operatori bilan sodda dastur:
Quyidagi misol bilan, 1 dan 10 gacha bo'lgan sonlarni chiqarishni ko'rsataman:
#include
using namespace std;

int main() {


int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 10);
cout << endl;
return 0;
}

4-misol.
#include
using namespace std;
int main() {
for (int i = 1; i <= 2; ++i) {
cout << ": " << i << "\n";
for (int j = 1; j <= 3; ++j) {
cout << " Son: " << j << "\n";
}
}
return 0;

}

4-misol
#include


using namespace std;
int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
return 0;
}

5-Misol 1. Do-while siklidan foydalanib, birinchi n ta natural sonning yig‘indisini hisoblang

#include

int main() {


int i = 1, n, yig'indisi = 0;
printf ("n ga ixtiyoriy qiymat kiriting:");
scanf("%d", &n);
qil {
summa += i;
i++;
} while (i <= n);
printf("Birinchi %d natural sonning yig`indisi %d ga teng", n, yig'indi);
qaytish 0;
}

6-misol. 1 dan 5 gacha bo'lgan sonlarni chiqaruvchi dasturni do-while operatori bilan tuzayapman:

#include
using namespace std;

int main() {


int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
cout << endl;
return 0;
}

7-misol. Do-while siklidan foydalanib sonni ko‘paytirish jadvalini chop eting


#include

int main() {


int n, i = 1;
printf("n ga ixtiyoriy qiymat kiriting: ");
scanf("%d", &n);
printf("%d ning ko‘paytirish jadvali”, n);
do {
printf(“%d x %d = %d\n”, n, i, n*i);
i++;
} while (i <= 10);
return 0;
}

8-misol. Do-while siklidan foydalanib foydalanuvchi kiritgan sonlar to‘plamining o‘rtacha qiymatini toping
#include

int main() {


int num, count = 0;
float sum = 0, average;
char choice;
do {
printf(" a ga ixtiyoriy Raqam kiriting: ");
scanf("%d", &num);
sum += num;
count++;
printf("Boshqa raqam kiritmoqchimisiz? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
average = sum/count;
printf("Kiritilgan raqamlarning o'rtacha qiymati %.2f", average);
return 0;
}

Download 159 Kb.

Do'stlaringiz bilan baham:




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