C++ hayotiy misollar


ULASHISH

Amaliy misollar

Bu sahifada real loyihalarda qo‘llaniladigan amaliy misollar ro‘yxati keltirilgan.


O‘zgaruvchilar va ma’lumot tiplari

Misol

Kollej talabasining turli ma’lumotlarini saqlash uchun o‘zgaruvchilardan foydalaning:

// Student data int studentID = 15; int studentAge = 23; float studentFee = 75.25; char studentGrade = 'B'; // Print variables cout << "Student ID: " << studentID << "\n"; cout << "Student Age: " << studentAge << "\n"; cout << "Student Fee: " << studentFee << "\n"; cout << "Student Grade: " << studentGrade << "\n";
O‘zingiz sinab ko‘ring »

Misol

To‘g‘ri to‘rtburchakning yuzini (uzunligi va enini ko‘paytirib) hisoblang:

// Create integer variables int length = 4; int width = 6; int area; // Calculate the area of a rectangle area = length * width; // Print the variables cout << "Length is: " << length << "\n"; cout << "Width is: " << width << "\n"; cout << "Area of the rectangle is: " << area << "\n";
O‘zingiz sinab ko‘ring »

Misol

Bir nechta mahsulotning umumiy narxini hisoblash va chiqarish uchun turli ma’lumot tiplaridan foydalaning:

// Create variables of different data types int items = 50; double cost_per_item = 9.99; double total_cost = items * cost_per_item; char currency = '$'; // Print variables cout << "Number of items: " << items << "\n"; cout << "Cost per item: " << cost_per_item << "" << currency << "\n"; cout << "Total cost = " << total_cost << "" << currency << "\n";
O‘zingiz sinab ko‘ring »

C++ tilidagi o‘zgaruvchilar va ma’lumot tiplari haqida batafsil o‘rganish uchun O‘zgaruvchilar va Ma’lumot tiplari boblarimizga o‘ting.


Stringlar

Misol

Oddiy salomlashuv xabarini yaratish uchun stringlardan foydalaning:

string message = "Good to see you, "; string fname = "John"; cout << greeting + fname;
O‘zingiz sinab ko‘ring »

C++ tilidagi stringlar haqida batafsil o‘rganish uchun Stringlar bobimizga o‘ting.



Boolean qiymatlar

Misol

Insonning ovoz berish uchun yoshi yetarli ekanligini aniqlang:

int myAge = 25; int votingAge = 18; cout << (myAge >= votingAge); // returns 1 (true), meaning 25 year olds are allowed to vote!
O‘zingiz sinab ko‘ring »

C++ tilidagi boolean qiymatlar haqida batafsil o‘rganish uchun Boolean qiymatlar bobimizga o‘ting.


Shartlar (If..Else)

Misol

Foydalanuvchi to‘g‘ri kod kiritganini tekshiring:

int doorCode = 1337; if (doorCode == 1337) {   cout << "Correct code.\nThe door is now open.\n"; } else {   cout << "Wrong code.\nThe door remains closed.\n"; }
O‘zingiz sinab ko‘ring »

Misol

Son musbat yoki manfiy ekanligini aniqlang:

int myNum = 10; // Is this a positive or negative number? if (myNum > 0) {   cout << "The value is a positive number.\n"; } else if (myNum < 0) {   cout << "The value is a negative number.\n"; } else {   cout << "The value is 0.\n"; }
O‘zingiz sinab ko‘ring »

Misol

Insonning ovoz berish uchun yoshi yetarli ekanligini aniqlang:

int myAge = 25; int votingAge = 18; if (myAge >= votingAge) {   cout << "Old enough to vote!\n"; } else {   cout << "Not old enough to vote.\n"; }
O‘zingiz sinab ko‘ring »

Misol

Sonning juft yoki toq ekanligini aniqlang:

int myNum = 5; if (myNum % 2 == 0) {   cout << myNum << " is even.\n"; } else {   cout << myNum << " is odd.\n"; }
O‘zingiz sinab ko‘ring »

C++ tilidagi shartlar haqida batafsil o‘rganish uchun If..Else bobimizga o‘ting.


switch

Misol

Hafta kuni raqamidan foydalanib, hafta kuni nomini aniqlang va chiqaring:

int day = 4; switch (day) {   case 1:     cout << "Monday";     break;   case 2:     cout << "Tuesday";     break;   case 3:     cout << "Wednesday";     break;   case 4:     cout << "Thursday";     break;   case 5:     cout << "Friday";     break;   case 6:     cout << "Saturday";     break;   case 7:     cout << "Sunday";     break; } // Outputs "Thursday" (day 4)
O‘zingiz sinab ko‘ring »

C++ tilidagi switch haqida batafsil o‘rganish uchun switch bobimizga o‘ting.


while sikllari

Misol

Oddiy "teskari sanoq" dasturini yarating:

int countdown = 3; while (countdown > 0) {   cout << countdown << "\n";   countdown--; } cout << "Happy New Year!!\n";
O‘zingiz sinab ko‘ring »

Misol

0 dan 10 gacha (10 ham kiradi) faqat juft sonlarni chiqaradigan dastur yarating:

int i = 0; while (i <= 10) {   cout << i << "\n";   i += 2; }
O‘zingiz sinab ko‘ring »

Misol

Sonning raqamlarini teskari tartibga keltirish uchun while siklidan foydalaning:

// A variable with some specific numbers int numbers = 12345; // A variable to store the reversed number int revNumbers = 0; // Reverse and reorder the numbers while (numbers) {   // Get the last number of 'numbers' and add it to 'revNumbers'   revNumbers = revNumbers * 10 + numbers % 10;   // Remove the last number of 'numbers'   numbers /= 10; } cout << "Reversed numbers: " << revNumbers << "\n";
O‘zingiz sinab ko‘ring »

Misol

Yatzy o‘yinini o‘ynash uchun while siklidan if else operatori bilan birga foydalaning:

int dice = 1; while (dice <= 6) {   if (dice < 6) {     cout << "No Yatzy\n";   } else {     cout << "Yatzy!\n";   }   dice = dice + 1; }
O‘zingiz sinab ko‘ring »

C++ tilidagi while sikllari haqida batafsil o‘rganish uchun while sikli bobimizga o‘ting.


for sikllari

Misol

100 gacha o‘ntalab sanaydigan dastur yaratish uchun for siklidan foydalaning:

for (int i = 0; i <= 100; i += 10) {   cout << i << "\n"; }
O‘zingiz sinab ko‘ring »

Misol

0 dan 10 gacha bo‘lgan faqat juft qiymatlarni chiqaradigan dastur yaratish uchun for siklidan foydalaning:

for (int i = 0; i <= 10; i = i + 2) {   cout << i << "\n"; }
O‘zingiz sinab ko‘ring »

Misol

Faqat toq sonlarni chiqaradigan dastur yaratish uchun for siklidan foydalaning:

for (int i = 1; i <= 10; i = i + 2) {   cout << i << "\n"; }
O‘zingiz sinab ko‘ring »

Misol

2 ning 512 gacha bo‘lgan darajalarini chiqarish uchun for siklidan foydalaning:

for (int i = 2; i <= 512; i *= 2) {   cout << i << "\n"; }
O‘zingiz sinab ko‘ring »

Misol

Berilgan sonning (bu misolda 2 ning) ko‘paytirish jadvalini chiqaradigan dastur yaratish uchun for siklidan foydalaning:

int number = 2; int i; // Print the multiplication table for the number 2 for (i = 1; i <= 10; i++) {   cout << number << " x " << i << " = " << number * i << "\n"; }
O‘zingiz sinab ko‘ring »

C++ tilidagi for sikllari haqida batafsil o‘rganish uchun for sikli bobimizga o‘ting.


Massivlar

Misol

Turli yoshlarning o‘rtacha qiymatini hisoblaydigan dastur yarating:

// An array storing different ages int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70}; float avg, sum = 0; int i; // Get the length of the array int length = sizeof(ages) / sizeof(ages[0]); // Loop through the elements of the array for (int age : ages) {   sum += age; } // Calculate the average by dividing the sum by the length avg = sum / length; // Print the average cout << "The average age is: " << avg << "\n";
O‘zingiz sinab ko‘ring »

Misol

Turli yoshlar orasidan eng kichigini topadigan dastur yarating:

// An array storing different ages int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70}; int i; // Get the length of the array int length = sizeof(ages) / sizeof(ages[0]); // Create a variable and assign the first array element of ages to it int lowestAge = ages[0]; // Loop through the elements of the ages array to find the lowest age for (int age : ages) {   if (lowestAge > age) {     lowestAge = age;   } } // Print the lowest age cout << "The lowest age is: " << lowestAge << "\n";
O‘zingiz sinab ko‘ring »

C++ tilidagi massivlar haqida batafsil o‘rganish uchun Massivlar bobimizga o‘ting.


Strukturalar (structs)

Misol

Avtomobillar haqidagi turli ma’lumotlarni saqlash va chiqarish uchun strukturadan foydalaning:

// Declare a structure named "car" struct car {   string brand;   string model;   int year; }; int main() {   // Create a car structure and store it in myCar1;   car myCar1;   myCar1.brand = "BMW";   myCar1.model = "X5";   myCar1.year = 1999;   // Create another car structure and store it in myCar2;   car myCar2;   myCar2.brand = "Ford";   myCar2.model = "Mustang";   myCar2.year = 1969;     // Print the structure members   cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";   cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";     return 0; }
O‘zingiz sinab ko‘ring »

C++ tilidagi strukturalar haqida batafsil o‘rganish uchun Strukturalar bobimizga o‘ting.


Funksiyalar

Misol

Qiymatni Farengeytdan Selsiyga o‘tkazadigan dastur yarating:

// Function to convert Fahrenheit to Celsius float toCelsius(float fahrenheit) {   return (5.0 / 9.0) * (fahrenheit - 32.0); } int main() {   // Set a fahrenheit value   float f_value = 98.8;   // Call the function with the fahrenheit value   float result = toCelsius(f_value);   // Print the fahrenheit value   cout << "Fahrenheit: " << f_value << "\n";   // Print the result   cout << "Convert Fahrenheit to Celsius: " << result << "\n";   return 0; }
O‘zingiz sinab ko‘ring »

Misol

Sonni ikki baravar oshiradigan dastur yarating:

int doubleGame(int x) {   return x * 2; } int main() {   for (int i = 1; i <= 5; i++) {     cout << "Double of " << i << " is " << doubleGame(i) << endl;   }   return 0; }
O‘zingiz sinab ko‘ring »

C++ tilidagi funksiyalar haqida batafsil o‘rganish uchun Funksiyalar bobimizga o‘ting.



W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!