w3resource

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

C Basic Declarations and Expressions: Exercise-38 with Solution

Divide two numbers or print if division isn't possible

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

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-38.php