Tanlash opretori (Switch) Labaratoriya
Labaratoriya №3 Mavzu: Tanlash opretori (Switch) 2.K butun soni berilgaan. Baho natijalari chiqaruvchi dastur tuzing. (1-yomon, 2- qoniqarsiz, 3-qoniqarli, 4-yaxshi, 5-a’lo). Agar k soni 1-5 oraliqqa tegishli bo’lmasa “xato” deb chiqarilsin. Yechish: #include<iostream> #include<math.h> using namespace std; int main () { int k; cin>>k; switch(k) { case 1:cout<<"yomon";break; case 2:cout<<"qoniqrsiz";break; case 3:cout<<"qoniqarli";break; case 4:cout<<"yaxshi";break; case 5:cout<<"a'lo";break; default:cout<<"xato"; } return 0; } Javobi:
2. Doiraning elementlari quyidagi tartibda nomerlangan: 1-radius R, 2-diametr D = 2*R, 3-uzunligi L =2*pi*R, 4-doiraing yuzi S=pi*R². Shu elementlardan bittasi berilgan qolganlarnii topuvchi dastur tuzilsin. Pi=3.14. Yechish: #include<iostream> #include<math.h> using namespace std; int main () { int k, r=3; float d,l,s,pi=3.14; cin>>k; d=2*r; l=2*pi*r; s=pi*pow(r,2); switch(k) { case 1:cout<<"radius="<<r;break; case 2:cout<<"diametr="<<d;break; case 3:cout<<"uzunligi="<<l;break; case 4:cout<<"yuzi="<<s;break; default:cout<<"xato"; } return 0; } Javobi:
3. O’yin kartasi turlari brilgan: 1-g’isht, 2-olma, 3-chillak, 4-qarg’a, 10 lik kartadan katta kartalar quyidagi qiymatlarni o’zlashtirsin: 12-dama, 13-qirol, 14-tuz. Ikkita butun son berilgan N-karta qiymati (6 ≤N≤14) va M-karta turi (1≤M≤4) kiritilganda karta nomlarini (masalan, “olti qarg’a”) chiqarib beruvchi dastur tuzilsin. Yechish: #include<iostream> #include<math.h> using namespace std; int main() {int x,y; cin>>x>>y; switch(x) { case 6: cout<<"olti ";break; case 7: cout<<"yeti ";break; case 8: cout<<"sakkiz ";break; case 9: cout<<"to'qqiz ";break; case 10: cout<<"o'n ";break; case 11: cout<<"valet ";break; case 12: cout<<"dama ";break; case 13: cout<<"karol ";break; case 14: cout<<"tuz ";break; default : cout<<"xato"; } switch(y) { case 1:cout<<"g'isht ";break; case 2:cout<<"tappan ";break; case 3:cout<<"qarg'a ";break; case 4:cout<<"chillik ";break; default:cout<<"xato";break;