Divide two numbers or print if division isn't possible
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:
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.
Go to:
PREV : Determine quadrant of Cartesian coordinates (x, y).
NEXT : Sum all numbers between two integers, excluding multiples of 17.
C programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.