PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to stop a division if the result is a float number?

 July 19, 2022     c, floating-point, if-statement, integer     No comments   

Issue

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;
}

Solution

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)


Answered By - Goswin von Brederlow
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing