Programs Progress
0%
Programs P5 – P8  ·  Input & Operators

User Input & Arithmetic Operators

Learn how to take input from the user with scanf, and perform all arithmetic operations with simple working examples.

P5 · Single Input
P6 · Multiple Inputs
P7 · Calculator
P8 · Modulus
Extra Examples
📥

How scanf Works — Quick Recap

scanf() reads input from the keyboard and stores it in a variable. It needs two things every time:

  • Format specifier — tells scanf what type to expect: %d int, %f float, %lf double, %c char, %s string
  • & operator — gives scanf the memory address of the variable so it knows where to store the value

Without & the program will crash or give wrong values. Only char[] (strings) do not need & because the array name is already an address.

Data Typescanf specifierprintf specifierExample
int%d%dscanf("%d",&n)
float%f%fscanf("%f",&x)
double%lf%f or %lfscanf("%lf",&d)
char%c%cscanf("%c",&c)
string%s%sscanf("%s",name) — no &
1

P5 — Simple User Input

Single integer

This is the simplest possible input program. It asks the user to type a number, reads it with scanf, and immediately prints it back. This proves the input was received and stored correctly.

Program P5 · user_input.c
user_input.c
C
#include <stdio.h>

int main() {
    int iNumber = 0;

    printf("Please enter a number: ");
    scanf("%d", &iNumber);   // %d reads an integer

    printf("You entered the number: %d\n", iNumber);

    return 0;
}
terminal — sample run
output
Please enter a number: 42
You entered the number: 42

Line by line explanation:

  • int iNumber = 0; — declares variable, initialised to 0 so it does not hold garbage
  • printf("Please enter a number: "); — prompts user without \n so cursor stays on same line
  • scanf("%d", &iNumber); — waits for user to type, reads an integer, stores at address of iNumber
  • printf("...%d\n", iNumber); — prints back what was stored
Extra · Read a float
input_float.c
C
#include <stdio.h>

int main() {
    float price = 0.0;

    printf("Enter item price: ");
    scanf("%f", &price);   // %f for float

    printf("Price entered: %.2f\n", price);

    return 0;
}
Extra · Read name and age
input_name_age.c
C
#include <stdio.h>

int main() {
    char name[50];
    int  age = 0;

    printf("Enter your name: ");
    scanf("%s", name);      // no & for char array

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Hello %s! You are %d years old.\n", name, age);

    return 0;
}
terminal
output
Enter your name: Ananta
Enter your age: 25
Hello Ananta! You are 25 years old.
💡 Always prompt before scanf! Always write a printf() prompt before every scanf() — otherwise the program just freezes with a blank screen and the user does not know what to do.
2

P6 — Multiple Inputs

Two values at once

You can read multiple values in a single scanf() call by listing multiple format specifiers. The user separates values with spaces or Enter. C reads them in order.

Program P6 · multiple_inputs.c
multiple_inputs.c
C
#include <stdio.h>

int main() {
    int iNum1, iNum2;

    printf("Enter two integers separated by a space: ");
    scanf("%d %d", &iNum1, &iNum2);  // reads two ints

    printf("First:  %d\n", iNum1);
    printf("Second: %d\n", iNum2);

    return 0;
}
terminal
output
Enter two integers separated by a space: 10 25
First:  10
Second: 25
Extra · Read three different types
input_mixed.c
C
#include <stdio.h>

int main() {
    int   qty   = 0;
    float price = 0.0;
    char  grade = ' ';

    printf("Enter quantity, price, grade: ");
    scanf("%d %f %c", &qty, &price, &grade);

    printf("Qty:   %d\n",    qty);
    printf("Price: %.2f\n", price);
    printf("Grade: %c\n",    grade);

    return 0;
}
terminal
output
Enter quantity, price, grade: 3 99.50 A
Qty:   3
Price: 99.50
Grade: A
⚠️ Each variable needs its own &scanf("%d %d", &a, &b) is correct. Writing scanf("%d %d", &a, b) — forgetting & on b — is a common mistake that causes wrong or garbage output.
arithmetic operators
3

P7 — Simple Calculator

+ − * / %

C has five arithmetic operators. They follow mathematical precedence — * / % are evaluated before + and -. Use parentheses () to control order.

OperatorNameExampleResultNote
+Addition10 + 313Works on int & float
-Subtraction10 - 37Works on int & float
*Multiplication10 * 330Works on int & float
/Division10 / 33 (not 3.33!)Int division truncates!
%Modulus10 % 31Only works on integers
Program P7 · calculator.c
calculator.c
C
#include <stdio.h>

int main() {
    int a, b;
    int sum, diff, product;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    sum     = a + b;
    diff    = a - b;
    product = a * b;

    printf("Sum:        %d\n", sum);
    printf("Difference: %d\n", diff);
    printf("Product:    %d\n", product);

    return 0;
}
terminal
output
Enter two numbers: 12 4
Sum:        16
Difference: 8
Product:    48
Extra · Full calculator with division
full_calc.c
C
#include <stdio.h>

int main() {
    float a, b;

    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);

    printf("%.2f + %.2f = %.2f\n", a, b, a + b);
    printf("%.2f - %.2f = %.2f\n", a, b, a - b);
    printf("%.2f * %.2f = %.2f\n", a, b, a * b);

    if (b != 0)
        printf("%.2f / %.2f = %.2f\n", a, b, a / b);
    else
        printf("Cannot divide by zero!\n");

    return 0;
}
terminal
output
Enter two numbers: 9 2
9.00 + 2.00 = 11.00
9.00 - 2.00 = 7.00
9.00 * 2.00 = 18.00
9.00 / 2.00 = 4.50
Extra · Integer division vs float division
division_types.c
C
#include <stdio.h>

int main() {
    int a = 9, b = 2;

    printf("Int division:   9 / 2 = %d\n",   a / b);         // 4
    printf("Float division: 9 / 2 = %.2f\n", (float)a / b); // 4.50
    printf("Float literal:  9 / 2 = %.2f\n", 9.0 / 2);      // 4.50

    return 0;
}
terminal
output
Int division:   9 / 2 = 4       ← decimal dropped!
Float division: 9 / 2 = 4.50   ← (float) cast fixes it
Float literal:  9 / 2 = 4.50   ← 9.0 forces float math
⚠️ Integer division truncates — it does NOT round!
9 / 2 = 4 not 4.5. 7 / 4 = 1 not 1.75. The decimal part is simply dropped. To get the real decimal result, cast one operand to float: (float)a / b
4

P8 — Modulus Operator %

Remainder of division

The modulus operator % gives the remainder after integer division. It only works with integers — not float or double. It is one of the most useful operators in programming.

  • 10 % 3 = 1  because 10 = 3×3 + 1
  • 15 % 5 = 0  because 15 = 5×3 + 0 (divisible exactly)
  • 7 % 2 = 1   because 7 = 2×3 + 1 (odd number test)
Program P8 · modulus.c
modulus.c
C
#include <stdio.h>

int main() {
    int dividend, divisor, remainder;

    printf("Enter two numbers: ");
    scanf("%d %d", &dividend, &divisor);

    remainder = dividend % divisor;

    printf("Remainder of %d / %d = %d\n",
            dividend, divisor, remainder);

    return 0;
}
terminal
output
Enter two numbers: 17 5
Remainder of 17 / 5 = 2
Extra · Even or Odd checker
even_odd.c
C
#include <stdio.h>

int main() {
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    // n % 2 gives 0 if even, 1 if odd
    if (n % 2 == 0)
        printf("%d is EVEN\n", n);
    else
        printf("%d is ODD\n", n);

    return 0;
}
terminal
output
Enter a number: 14
14 is EVEN

Enter a number: 7
7 is ODD
Extra · Divisibility checker
divisible.c
C
#include <stdio.h>

int main() {
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 3 == 0 && n % 5 == 0)
        printf("Divisible by both 3 and 5\n");
    else if (n % 3 == 0)
        printf("Divisible by 3 only\n");
    else if (n % 5 == 0)
        printf("Divisible by 5 only\n");
    else
        printf("Not divisible by 3 or 5\n");

    return 0;
}
Extra · Extract last digit & remove it
digits.c
C
#include <stdio.h>

int main() {
    int n = 4567;

    printf("Number: %d\n", n);
    printf("Last digit:    %d\n", n % 10);   // 7
    printf("Remove last:   %d\n", n / 10);   // 456
    printf("Second digit:  %d\n", (n / 10) % 10); // 6

    return 0;
}
terminal
output
Number: 4567
Last digit:    7
Remove last:   456
Second digit:  6
💡 Most common uses of % in real programs:
n % 2 == 0 → even/odd check
n % 10 → extract last digit
n / 10 → remove last digit
n % 3 == 0 → divisibility check
count % 5 == 0 → do something every 5th iteration
bonus programs
5

Bonus — Putting It All Together

Combined examples
Bonus · Bill calculator with tax
bill_tax.c
C
#include <stdio.h>

int main() {
    float price, quantity, subtotal, tax, total;

    printf("Enter item price:    ");
    scanf("%f", &price);

    printf("Enter quantity:      ");
    scanf("%f", &quantity);

    subtotal = price * quantity;
    tax      = subtotal * 0.18;    // 18% GST
    total    = subtotal + tax;

    printf("\n--- BILL ---\n");
    printf("Subtotal: Rs %.2f\n", subtotal);
    printf("GST 18%%:  Rs %.2f\n", tax);
    printf("Total:    Rs %.2f\n", total);

    return 0;
}
terminal
output
Enter item price:    250
Enter quantity:      3

--- BILL ---
Subtotal: Rs 750.00
GST 18%:  Rs 135.00
Total:    Rs 885.00
Bonus · BMI calculator
bmi.c
C
#include <stdio.h>

int main() {
    float weight, height, bmi;

    printf("Enter weight (kg): ");
    scanf("%f", &weight);

    printf("Enter height (m):  ");
    scanf("%f", &height);

    bmi = weight / (height * height);  // BMI formula

    printf("Your BMI: %.1f\n", bmi);

    if (bmi < 18.5)
        printf("Category: Underweight\n");
    else if (bmi < 25.0)
        printf("Category: Normal\n");
    else if (bmi < 30.0)
        printf("Category: Overweight\n");
    else
        printf("Category: Obese\n");

    return 0;
}
terminal
output
Enter weight (kg): 70
Enter height (m):  1.75
Your BMI: 22.9
Category: Normal
quiz
Q

Quick Quiz

Question 1 of 5

Why do we write &n inside scanf("%d", &n)?

Question 2 of 5

What is the result of 9 / 2 when both are int?

Question 3 of 5

What does 17 % 5 equal?

Question 4 of 5

Which is the correct way to read a float with scanf?

Question 5 of 5

How do you check if a number n is even using modulus?

Lesson Checklist

  • I know the format specifier for int (%d), float (%f), char (%c), string (%s)
  • I understand why & is needed in scanf for variables
  • I know char arrays do NOT need & in scanf
  • I can read multiple inputs in one scanf call
  • I understand all 5 arithmetic operators: + - * / %
  • I know integer division truncates — 9/2 = 4 not 4.5
  • I can use (float) cast to get decimal division
  • I understand modulus % gives the remainder
  • I can use % to check even/odd and divisibility
  • I completed the quiz