Operators Assignment 4 — Divide Two Numbers Using the '/' Operator
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Divide Two Numbers Using the '/' Operator

📑 On this page:
  • The division operator
  • Complete C Programs
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How the / operator computes the quotient
  • Why integer division truncates (7 / 2 = 3, not 3.5)
  • How to get a fractional result using double or a cast
  • Why division by zero crashes or misbehaves, and how to prevent it
  • How integer division rounds toward zero for negative operands

The division operator

The division operator / computes the quotient of two operands. The critical thing to understand is that the result type depends entirely on the operand types. When both operands are integers, C performs integer division: the result is an integer, and the fractional part is truncated (discarded, not rounded). So 7 / 2 gives 3, not 3.5.

When at least one operand is a floating-point type, C performs floating-point division: the integer operand is promoted to double, and the result is a double. So 7.0 / 2, 7 / 2.0, and (double)7 / 2 all give 3.5.

Unlike addition, subtraction, and multiplication, division has a hard rule: the divisor (second operand) cannot be zero. Integer division by zero is undefined behavior — on most systems it triggers a SIGFPE signal (crash). Floating-point division by zero on IEEE 754 systems gives infinity or NaN, but it's still undefined behavior in the C standard. Always check for zero before dividing.

One more subtlety: for negative operands, C truncates toward zero. −7 / 2 = −3, not −4. This is the behavior mandated by C99 and later; older compilers might differ, but modern ones are consistent.

💡 In one line: a / b truncates when both operands are integers. Use double or a cast to get a fractional result. Always check b != 0 before dividing.
Division of two numbers in C — reading two integers with scanf, dividing them with the / operator, and printing the quotient with printf.

Complete C Programs

Here is the simplest version — dividing two integers (with a check for division by zero):

Program 1: Divide two integers

#include <stdio.h>

int main(void) {
    int a, b, quot;

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

    if (b == 0) {
        printf("Error: division by zero\n");
        return 1;
    }

    quot = a / b;

    printf("Quotient = %d\n", quot);

    return 0;
}
                                

The floating-point version, which preserves the fractional part:

Program 2: Divide two doubles

#include <stdio.h>

int main(void) {
    double a, b, quot;

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

    if (b == 0.0) {
        printf("Error: division by zero\n");
        return 1;
    }

    quot = a / b;

    printf("Quotient = %.4lf\n", quot);

    return 0;
}
                                

And a version that demonstrates the cast trick — reading integers but getting a floating-point result:

Program 3: Cast to get fractional results from integer inputs

#include <stdio.h>

int main(void) {
    int a, b;

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

    if (b == 0) {
        printf("Error: division by zero\n");
        return 1;
    }

    printf("%d / %d = %.4lf\n", a, b, (double)a / b);

    return 0;
}
                                

The (double)a cast promotes a to double before the division, so the operation becomes floating-point. Without the cast, a / b would be integer division. This is the idiomatic way to get a fractional quotient from integer inputs.

Program Output

Sample run of Program 1:

Enter two integers: 10 3
Quotient = 3
                            

10 / 3 = 3 — the true quotient is 3.333…, truncated to 3. That's integer division. Notice the truncation: 10 ÷ 3 = 3 remainder 1, and the fractional part is discarded.

With the same inputs and Program 3:

Enter two integers: 10 3
10 / 3 = 3.3333
                            

Now the result is the true quotient (rounded to 4 decimals). Same inputs, different types — that's the integer-vs-floating-point distinction in one example.

Program 2 with decimal inputs:

Enter two numbers: 7.5 2.5
Quotient = 3.0000
                            

Negative numbers (truncation toward zero):

Enter two integers: -7 2
Quotient = -3
                            

−7 / 2 is −3.5 mathematically, but integer division truncates toward zero, giving −3 (not −4).

Division by zero:

Enter two integers: 5 0
Error: division by zero
                            

The check prevents the crash. Without it, the program would terminate with a SIGFPE signal.

Step-by-Step Explanation

1. Declarations: int a, b, quot; declares two inputs and one result. The name quot (short for "quotient") documents the intent.

2. Input: scanf("%d %d", &a, &b) reads two integers. The & is essential — omitting it is the classic scanf bug.

3. Zero check: if (b == 0) catches division by zero before it can crash the program. This is non-negotiable for any program that divides.

4. The division: quot = a / b computes integer division. For 10 / 3, the result is 3. The CPU computes the quotient and discards the remainder.

5. Output: printf("Quotient = %d\n", quot) prints the integer result. For floating-point results, use %.4lf (four decimal places) with a double.

6. The cast trick: (double)a / b promotes a to double, forcing floating-point division. The result is 3.3333…, printed with decimals. This is the standard way to get a fractional quotient from integer inputs.

🎯 Key idea: int / int truncates. double / int or (double)a / b preserves the fraction. Division by zero is undefined — always check the divisor first.

Frequently Asked Questions

1. Why does 10 / 3 give 3 instead of 3.33?

Because 10 and 3 are both integers, so C performs integer division — the result is an integer, and the fractional part is truncated. For a fractional result, use 10.0 / 3, 10 / 3.0, or (double)10 / 3. All give 3.3333…

2. What happens if I divide by zero?

Integer division by zero is undefined behavior — on most systems, it triggers a SIGFPE signal (crash). Floating-point division by zero gives inf or NaN on IEEE 754 systems, but it's still undefined in the C standard. Always check the divisor before dividing: if (b != 0) quot = a / b;

3. How does integer division round negative numbers?

C truncates toward zero. So −7 / 2 = −3, not −4. The exact value is −3.5, but integer division drops the fractional part (truncation), moving toward zero, not down. This is the behavior mandated by C99 and later.

4. Can I get both the quotient and the remainder in one operation?

Yes — use / for the quotient and % for the remainder: quot = a / b; rem = a % b;. For 10 / 3, this gives quot = 3 and rem = 1. The two operations together are the classic "division algorithm" — a = b * quot + rem.

5. Why use (double)a / b instead of a / (double)b?

Both work — they both promote at least one operand to double, forcing floating-point division. The choice is stylistic. The first form reads naturally as "divide a (as a double) by b"; the second as "divide a by (b as a double)". Pick whichever reads clearer in context. Some style guides prefer casting the numerator because it emphasizes that a is being promoted.

💡 Tip: once this works, extend it to print both the quotient and the remainder (/ and % together). Then try a calculator that handles all four operators — you already have the code for +, −, and * from the previous assignments; only the / case needs the zero check.

📖 Related Tutorials

  • Previous: Multiply Two Numbers Using the '*' Operator
  • Next: Find the Remainder Using the '%' Operator
  • All 15 Operator Assignments in C
  • Arithmetic Operators in C
  • Operators and Expressions
  • Basic Input/Output Statements
  • Operators FAQ

Previous Topic: -->> 3. Multiply Two Numbers Using the '*' Operator   ||   Next Topic: -->> 5. Find the Remainder Using the '%' Operator


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.