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 printf("Student id: %d\n", studentID); printf("Student age: %d\n", studentAge); printf("Student fee: %f\n", studentFee); printf("Student grade: %c", studentGrade);
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 printf("Length is: %d\n", length); printf("Width is: %d\n", width); printf("Area of the rectangle is: %d", area);
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; float cost_per_item = 9.99; float total_cost = items * cost_per_item; char currency = '$'; // Print variables printf("Number of items: %d\n", items); printf("Cost per item: %.2f %c\n", cost_per_item, currency); printf("Total cost = %.2f %c\n", total_cost, currency);
O‘zingiz sinab ko‘ring »

Misol

O‘yinda foydalanuvchi to‘plagan ballning maksimal ballga nisbatan foizini hisoblang:

// Set the maximum possible score in the game to 500 int maxScore = 500; // The actual score of the user int userScore = 423; // Calculate the percantage of the user's score in relation to the maximum available score float percentage = (float) userScore / maxScore * 100.0; // Print the percentage printf("User's percentage is %.2f", percentage);
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.



Boolean qiymatlar

Misol

Insonning ovoz berish uchun yoshi yetarli ekanligini aniqlang:

int myAge = 25; int votingAge = 18; printf("%d", myAge >= votingAge); // Returns 1 (true), meaning 25 year olds are allowed to vote!
O‘zingiz sinab ko‘ring »

Natijaga qarab turli amallarni bajarish uchun yuqoridagi kodni if...else bloki ichiga ham joylashtirishingiz mumkin:

Misol

Agar myAge qiymati 18 qiymatidan katta yoki unga teng bo‘lsa, "Old enough to vote!" matnini chiqaring. Aks holda "Not old enough to vote." matnini chiqaring:

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

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


Shartlar (If..Else)

Misol

Soat nechaligiga qarab biror matn chiqarish uchun if..else operatorlaridan foydalaning:

int time = 20; if (time < 18) {   printf("Good day."); } else {   printf("Good evening."); }
O‘zingiz sinab ko‘ring »

Misol

Foydalanuvchi to‘g‘ri kod kiritganini tekshiring:

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

Misol

Son musbat yoki manfiy ekanligini aniqlang:

int myNum = 10; if (myNum > 0) {   printf("The value is a positive number."); } else if (myNum < 0) {   printf("The value is a negative number."); } else {   printf("The value is 0."); }
O‘zingiz sinab ko‘ring »

Misol

Insonning ovoz berish uchun yoshi yetarli ekanligini aniqlang:

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

Misol

Sonning juft yoki toq ekanligini aniqlang:

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

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


switch

Misol

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

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

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


while sikllari

Misol

Oddiy "teskari sanoq" dasturini yaratish uchun while siklidan foydalaning:

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

Misol

Yatzy o‘yinini o‘ynash uchun while siklidan foydalaning:

int dice = 1; while (dice <= 6) {   if (dice < 6) {     printf("No Yatzy\n");   } else {     printf("Yatzy!\n");   }   dice = dice + 1; }
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 'revNumber'   revNumbers = revNumbers * 10 + numbers % 10;   // Remove the last number of 'numbers'   numbers /= 10; }
O‘zingiz sinab ko‘ring »

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


for sikllari

Misol

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

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

Misol

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

for (i = 0; i <= 100; i += 10) {   printf("%d\n", i); }
O‘zingiz sinab ko‘ring »

Misol

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

for (i = 2; i <= 512; i *= 2) {   printf("%d\n", i); }
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++) {   printf("%d x %d = %d\n", number, i, number * i); } return 0;
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[] = {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 i = 0; i < length; i++) {   sum += ages[i]; } // Calculate the average by dividing the sum by the length avg = sum / length; // Print the average printf("The average age is: %.2f", avg);
O‘zingiz sinab ko‘ring »

Misol

Turli yoshlar orasidan eng kichigini topadigan dastur yarating:

// An array storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; // 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 i = 0; i < length; i++) {   if (lowestAge > ages[i]) {     lowestAge = ages[i];   } }
O‘zingiz sinab ko‘ring »

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


Stringlar

Misol

Oddiy salomlashuv xabarini yaratish uchun stringlardan foydalaning:

char message[] = "Good to see you,"; char fname[] = "John"; printf("%s %s!", message, fname);
O‘zingiz sinab ko‘ring »

Misol

Muayyan so‘zdagi belgilar sonini sanaydigan dastur yarating:

char word[] = "Computer"; printf("The word '%s' has %d characters in it.", word, strlen(word));
O‘zingiz sinab ko‘ring »

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


Foydalanuvchi kiritishi

Misol

Foydalanuvchi ismini oling va uni chiqaring:

char fullName[30]; printf("Type your full name: \n"); fgets(fullName, sizeof(fullName), stdin); printf("Hello %s", fullName);
Misolni ishga tushirish »

C tilidagi foydalanuvchi kiritishi haqida batafsil o‘rganish uchun Foydalanuvchi kiritishi bobimizga o‘ting.


Funksiyalar

Misol

Qiymatni Farengeytdan Selsiyga o‘tkazadigan dastur yaratish uchun funksiyadan foydalaning:

// 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   printf("Fahrenheit: %.2f\n", f_value);   // Print the result   printf("Convert Fahrenheit to Celsius: %.2f\n", result);   return 0; }
O‘zingiz sinab ko‘ring »

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


Strukturalar

Misol

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

struct Car {   char brand[50];   char model[50];   int year; }; int main() {   struct Car car1 = {"BMW", "X5", 1999};   struct Car car2 = {"Ford", "Mustang", 1969};   struct Car car3 = {"Toyota", "Corolla", 2011};   printf("%s %s %d\n", car1.brand, car1.model, car1.year);   printf("%s %s %d\n", car2.brand, car2.model, car2.year);   printf("%s %s %d\n", car3.brand, car3.model, car3.year);   return 0; }
O‘zingiz sinab ko‘ring »

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


Xotirani boshqarish

Misol

struct list {   int *data; // Points to the memory where the list items are stored   int numItems; // Indicates how many items are currently in the list   int size; // Indicates how many items fit in the allocated memory }; void addToList(struct list *myList, int item); int main() {   struct list myList;   int amount;   // Create a list and start with enough space for 10 items   myList.numItems = 0;   myList.size = 10;   myList.data = malloc(myList.size * sizeof(int));   // Find out if memory allocation was successful   if (myList.data == NULL) {     printf("Memory allocation failed");     return 1; // Exit the program with an error code   }   // Add any number of items to the list specified by the amount variable   amount = 44;   for (int i = 0; i < amount; i++) {     addToList(&myList, i + 1);   }   // Display the contents of the list   for (int j = 0; j < myList.numItems; j++) {     printf("%d ", myList.data[j]);   }   // Free the memory when it is no longer needed   free(myList.data);   myList.data = NULL;   return 0; } // This function adds an item to a list void addToList(struct list *myList, int item) {   // If the list is full then resize the memory to fit 10 more items   if (myList->numItems == myList->size) {     myList->size += 10;     myList->data = realloc( myList->data, myList->size * sizeof(int) );   }   // Add the item to the end of the list   myList->data[myList->numItems] = item;   myList->numItems++; }
O‘zingiz sinab ko‘ring »

C tilida xotirani boshqarish haqida batafsil o‘rganish uchun Xotirani boshqarish bobimizga o‘ting.


Tasodifiy sonlar

Misol

Tasodifiy sonlarning hayotdagi keng tarqalgan qo‘llanilishlaridan biri — olti yoqli zar tashlash. Buni simulyatsiya qilish uchun rand() % 6 yordamida 0 dan 5 gacha sonlarni olamiz, so‘ng oraliq 1 dan 6 gacha bo‘lishi uchun 1 qo‘shamiz:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
  srand(time(NULL));

  int dice1 = (rand() % 6) + 1;
  int dice2 = (rand() % 6) + 1;
  printf("You rolled %d and %d (total = %d)\n", dice1, dice2, dice1 + dice2);

  return 0;
}

O‘zingiz sinab ko‘ring »

Dasturni har safar ishga tushirganingizda, xuddi hayotda ikkita zar tashlagandek, 1 dan 6 gacha bo‘lgan ikkita tasodifiy son olasiz.

C tilidagi tasodifiy sonlar haqida batafsil o‘rganish uchun Tasodifiy sonlar bobimizga o‘ting.



W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!