Home > Enterprise >  How to stop a division if the result is a float number?
How to stop a division if the result is a float number?

Time:05-25

i have programmed this code but it show me partially the result as example 50/3 show me 16 while i want to introduce to stop this kind of operations if float. How to do?

#include <stdio.h>
int main(){
    int number, divisor, x, y;
    printf("Enter the Natural Number to find Divisors\n");
    scanf("%d", &number);
    printf("Enter the Natural Number to divide for\n");
    scanf("%d", &x);
        
    printf("Divisors of the number %d are \n", number);
    y=number/x;
    for (divisor = 1; divisor<=number;divisor  ){
    if((number%divisor)==0){
    printf("%d\n", divisor);
    
    }
    else{
    continue; }
    
}
printf("The result of the division is %d", y);
return 0;
}

CodePudding user response:

This is how far I have to read your code:

for (i=0;i<x;i  )
{
    j=x/i;

What do you think a division by 0 should do?

But lets keep going:

if("J=%d")

This tests if the string constant is NULL, which can never be.

Overall it looks like you are trying to test if x is a prime. In that case you need to start the loop at 2 and test the remainder:

if (x % i == 0)
  • Related