Skip to content

Roykoboy/Lab1-Basic-programming-concepts-on-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Lab1-Basic-programming-concepts-on-C

This is a simple console program, like a C tutorial, that demonstrates various basic programming concepts through an interactive menu.

It implements the following points:

  1. Basic things — text output, working with numbers, selection menu.

  2. Calculation of the purchase amount — adds up the prices of goods and outputs the total.

  3. Zodiac sign definition — asks the date of birth and tells who you are according to the horoscope.

  4. Multiplication without multiplication — multiplies two numbers by adding in a loop (as if you were counting manually: 3 × 4 = 4 + 4 + 4).

Functionality

The program provides a menu with several options:

Hello World! - Displays the classic message "Hello, World!"

Data Types - Demonstrates working with different types of data and calculating the amount of purchases

Branching Operators - Determines the zodiac sign by the entered date

Loops - Implements the multiplication of two numbers using loops

Description of functions

task1()
  • Demonstrates various data types (int, double, float, long double)
  • Calculates the total cost of purchases rounded to 2 decimal places
task2()
  • Asks the user for the day and month of birth
  • Identifies and outputs the corresponding zodiac sign
multiply(int a, int b)
  • Implements the multiplication of two integers through addition in a loop
  • Optimizes the number of iterations by choosing a smaller multiplier for the loop.

Code:

#include <stdio.h>
#include <locale.h>

// Функция для выполнения задания 1
void task1() {
    // Цены на товары
    int milk = 75;
    double cookies = 54.458;
    float sausage = 194.442;
    long double bread = 25.5;

    // Количество покупок
    int milkQuantity = 2;
    int sausageQuantity = 3;
    int breadQuantity = 5;
    int cookiesQuantity = 1;

    // Подсчет суммы покупок
    double totalPrice = (double)milk * milkQuantity;
    totalPrice += cookies * cookiesQuantity;
    totalPrice += (double)sausage * sausageQuantity;
    totalPrice += (double)bread * breadQuantity;

    // Округление до двух знаков после запятой
    totalPrice = ((int)(totalPrice * 100 + 0.5)) / 100.0;

    // Вывод итоговой суммы покупок
    printf("Итоговая сумма покупок: %.2f\n\n", totalPrice);
}

// Функция для выполнения задания 2
void task2() {
    int day, month;

    // Ввод даты
    printf("Введите день: ");
    scanf_s("%d", &day);

    printf("Введите месяц: ");
    scanf_s("%d", &month);

    // Проверка знака зодиака
    if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
        printf("Ваш знак зодиака: Водолей\n\n");
    }
    else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
        printf("Ваш знак зодиака: Рыбы\n\n");
    }
    else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
        printf("Ваш знак зодиака: Овен\n\n");
    }
    else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
        printf("Ваш знак зодиака: Телец\n\n");
    }
    else if ((month == 5 && day >= 21) || (month == 6 && day <= 20)) {
        printf("Ваш знак зодиака: Близнецы\n\n");
    }
    else if ((month == 6 && day >= 21) || (month == 7 && day <= 22)) {
        printf("Ваш знак зодиака: Рак\n\n");
    }
    else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
        printf("Ваш знак зодиака: Лев\n\n");
    }
    else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
        printf("Ваш знак зодиака: Дева\n\n");
    }
    else if ((month == 9 && day >= 23) || (month == 10 && day <= 22)) {
        printf("Ваш знак зодиака: Весы\n\n");
    }
    else if ((month == 10 && day >= 23) || (month == 11 && day <= 21)) {
        printf("Ваш знак зодиака: Скорпион\n\n");
    }
    else if ((month == 11 && day >= 22) || (month == 12 && day <= 21)) {
        printf("Ваш знак зодиака: Стрелец\n\n");
    }
    else {
        printf("Ваш знак зодиака: Козерог\n\n");
    }
}

// Функция для выполнения задания 3
int multiply(int a, int b) {
    int result = 0;

    if (a > b) {
        for (int i = 0; i < a; i++) {
            result += b;
        }
    }
    else {
        for (int i = 0; i < b; i++) {
            result += a;
        }
    }

    return result;
}

int main() {
    setlocale(LC_ALL, "Rus");
    int choice;

    do {
        // Вывод меню
        printf("Меню:\n");
        printf("[0] Выход\n");
        printf("[1] Привет Мир!\n");
        printf("[2] Типы данных (сумма покупок)\n");
        printf("[3] Операторы ветвления (знак зодиака)\n");
        printf("[4] Циклы (умножение через сложение)\n\n");

        // Ввод выбора пользователя
        printf("Выберите пункт: ");
        scanf_s("%d", &choice);

        // Очистка буфера ввода
        while (getchar() != '\n');

        // Выполнение действия
        switch (choice) {
        case 0:
            printf("До свидания!\n\n");
            break;
        case 1:
            printf("Привет, Мир!\n\n");
            break;
        case 2:
            task1();
            break;
        case 3:
            task2();
            break;
        case 4: {
            int a, b;
            printf("Введите число a: ");
            scanf_s("%d", &a);
            printf("Введите число b: ");
            scanf_s("%d", &b);
            int product = multiply(a, b);
            printf("Произведение чисел a и b: %d\n\n", product);
            break;
        }
        default:
            printf("Неверный выбор! Попробуйте еще раз.\n\n");
        }
    } while (choice != 0);

    return 0;
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published