Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

تقسيم الفاتورة

جزء من قسم Fundamentals في رحلة R على Coddy — الدرس 36 من 78.

challenge icon

التحدي

سهل

في الدرس السابق، قمت بحساب مبلغ الإكرامية وإجمالي الفاتورة. الآن، أضف الخطوة الأخيرة: تقسيم الإجمالي على جميع الأشخاص.

بعد عرض مبلغ الإكرامية والإجمالي مع الإكرامية، احسب المبلغ الذي يجب على كل شخص دفعه عن طريق قسمة total على عدد people.

قم بتخزين هذه القيمة في متغير يسمى per_person.

ثم اعرض النتيجة النهائية بالتنسيق التالي:

Each person pays: $[per_person]

استخدم cat() لجميع المخرجات وقم بتضمين أحرف السطر الجديد \n عند الحاجة.

على سبيل المثال، إذا كانت المدخلات هي 100 و 20 و 4، فيجب أن تكون المخرجات الكاملة هي:

Welcome to the Bill Split Calculator!
Let's split your bill fairly.
Bill: $100
Tip: 20%
People: 4
Tip amount: $20
Total with tip: $120
Each person pays: $30

جرّب بنفسك

# TODO: Write your code below
# Use cat() to display the welcome message
# Remember to use \n for new lines
cat("Welcome to the Bill Split Calculator!\nLet's split your bill fairly.\n")

# Read user inputs
con <- file("stdin", "r")
bill <- as.numeric(suppressWarnings(readLines(con, n = 1)))
tip_percent <- as.numeric(suppressWarnings(readLines(con, n = 1)))
people <- as.numeric(suppressWarnings(readLines(con, n = 1)))
close(con)

# Display confirmation message
cat("Bill: $", bill, "\n", sep = "")
cat("Tip: ", tip_percent, "%\n", sep = "")
cat("People: ", people, "\n", sep = "")

# Calculate tip amount and total
tip_amount <- bill * tip_percent / 100
total <- bill + tip_amount

# Display calculation results
cat("Tip amount: $", tip_amount, "\n", sep = "")
cat("Total with tip: $", total, "\n", sep = "")

جميع دروس Fundamentals