w3resource

Divide two numbers or print if division isn't possible


C Practice Exercise

Write a program that reads two numbers and divides the first number by the second number. If division is not possible print "Division is not possible".

Sample Solution:

C Code:

#include <stdio.h>
int main() {
    int x, y;
    float div_result;

    // Prompt for user input
    printf("Input two numbers: ");
    printf("\nx: ");
    scanf("%d", &x);
    printf("y: ");
    scanf("%d", &y);

    // Check if division is possible
    if(y != 0) {
        div_result = x/y;
        printf("%.1f\n", div_result);
    } else {
        printf("Division not possible.\n");
    }

    return 0;
}

Sample Output:

Input two numbers:                                                     
x: 25                                                                  
y: 5                                                                   
5.0

Flowchart:

C Programming Flowchart: Read two numbers and divide the first number by second number

For more Practice: Solve these Related Problems:

  • Write a C program to divide two numbers and display both the quotient and the remainder, handling division by zero gracefully.
  • Write a C program to perform division and, if the divisor is zero, prompt the user to enter a new value.
  • Write a C program to compute the division of two integers and display the result as a floating-point number with error handling for zero divisor.
  • Write a C program to divide two numbers and, if division is not possible, print an appropriate error message and exit.

C programming Code Editor:



Previous: Write a C program to read the coordinate(x, y) (in Cartesian system) and find the quadrant to which it belongs (Quadrant -I, Quadrant -II, Quadrant -III, Quadrant -IV).
Next: Write a C program to calculate the sum of all number not divisible by 17 between two given integer numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.