📖 Scenario: You are helping a small shop calculate the total cost of items bought by a customer. Each item has a price, and the customer buys a certain quantity of each item.
🎯 Goal: Build a simple C program that calculates the total cost for each item using arithmetic operators and then sums all the costs to find the grand total.
📋 What You'll Learn
Create variables for item prices and quantities
Use arithmetic operators to calculate total cost per item
Sum all item totals to get the grand total
Print the grand total
💡 Why This Matters
🌍 Real World
Calculating costs and totals is common in shopping, billing, and budgeting applications.
💼 Career
Understanding arithmetic operators and printing output is essential for any programming job involving calculations.
Progress0 / 4 steps
1
Create variables for item prices and quantities
Create four variables in C: price1 with value 10, quantity1 with value 2, price2 with value 15, and quantity2 with value 3.
C
Hint
Use int to declare integer variables and assign the exact values given.
2
Create variables for total cost per item
Create two variables in C: total1 and total2. Use the multiplication operator * to calculate total1 as price1 * quantity1 and total2 as price2 * quantity2.
C
Hint
Use the * operator to multiply price and quantity for each item.
3
Calculate the grand total
Create a variable grand_total and use the addition operator + to sum total1 and total2.
C
Hint
Use the + operator to add the two totals.
4
Print the grand total
Use printf to display the text "Grand total: " followed by the value of grand_total and a newline.
C
Hint
Use printf("Grand total: %d\n", grand_total); to print the result.
Practice
(1/5)
1. Which arithmetic operator in C gives the remainder of a division?
easy
A. The multiplication operator (*)
B. The division operator (/)
C. The modulus operator (%)
D. The subtraction operator (-)
Solution
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division of two integers.
2. Which of the following is the correct syntax to multiply two integers a and b in C?
easy
A. a x b;
B. a * b;
C. a . b;
D. a & b;
Solution
Step 1: Recall multiplication operator in C
The multiplication operator in C is the asterisk symbol (*).
Step 2: Check each option
a * b; uses '*', which is correct. Options B, C, D use invalid symbols for multiplication.
Final Answer:
a * b; -> Option B
Quick Check:
Multiply with * operator [OK]
Hint: Use * for multiplication in C [OK]
Common Mistakes:
Using 'x' instead of '*' for multiplication
Using '.' or '&' mistakenly
Missing semicolon at end
3. What is the output of this C code?
int a = 7, b = 3;
printf("%d", a / b);
medium
A. 1
B. 2.333333
C. 3
D. 2
Solution
Step 1: Understand integer division in C
Both a and b are integers, so a / b performs integer division, which discards the decimal part.
Step 2: Calculate 7 / 3 using integer division
7 divided by 3 is 2.333..., but integer division returns 2.
Final Answer:
2 -> Option D
Quick Check:
Integer division truncates decimals [OK]
Hint: Integer division drops decimals, result is integer [OK]
Common Mistakes:
Expecting floating-point result
Confusing remainder with division result
Using wrong format specifier in printf
4. Find the error in this C code snippet:
int x = 10;
int y = 3;
int z = x / y.0;
printf("%d", z);
medium
A. Using a floating-point number in integer division
B. Incorrect format specifier in printf
C. Missing semicolon after variable declaration
D. Division by zero error
Solution
Step 1: Analyze the division expression
The expression x / y.0 uses y.0 which attempts a floating-point literal but causes invalid syntax, as x and y are integers.
Step 2: Understand the syntax error
Using y.0 causes a compilation error due to invalid syntax in the integer division context.
Final Answer:
Using a floating-point number in integer division -> Option A
Quick Check:
Integer division must use integers only [OK]
Hint: Integer division needs integer operands only [OK]
Common Mistakes:
Thinking printf format is wrong
Ignoring the decimal in y.0
Assuming division by zero
5. You want to calculate the average of two integers a and b in C and get a floating-point result. Which code snippet correctly does this?
hard
A. float avg = (a + b) / 2.0;
B. float avg = (a + b) / 2;
C. int avg = (a + b) / 2.0;
D. float avg = a / b + 2;
Solution
Step 1: Understand integer vs floating-point division
Dividing by 2 (integer) causes integer division, losing decimals. Dividing by 2.0 (double) forces floating-point division.
Step 2: Check each option for correct average calculation
float avg = (a + b) / 2; does integer division then converts to float, losing decimals. float avg = (a + b) / 2.0; divides by 2.0, giving float result. int avg = (a + b) / 2.0; stores float result in int, losing decimals. float avg = a / b + 2; is incorrect formula.
Final Answer:
float avg = (a + b) / 2.0; -> Option A
Quick Check:
Divide by 2.0 for float average [OK]
Hint: Divide by 2.0 to get float average [OK]
Common Mistakes:
Dividing by integer 2 causing integer division
Storing float result in int variable
Wrong formula for average
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.