Home > Mobile >  How do I print out "No Output" if the program doesn't find any solution (C Language)
How do I print out "No Output" if the program doesn't find any solution (C Language)

Time:10-25

This is my program. I found that if I input n>480, the program wouldn't output any solution. How do I express it and print "No Output" in this program?

#include <stdio.h>
main()

{ int x,y,z,n;
scanf("%d",&n);
for(x=0;x<=n;x  )
for(y=0;y<=n;y  )
for(z=0;z<=n;z  )

if(5*x 2*y 0.1*z==120&&x y z==n)
printf("%d,%d,%d\n",x,y,z);
}

CodePudding user response:

Try counting the true-branch passes in the loops, as pointed out by Alex P:

#include <stdio.h>

int main() {
    int x,y,z,n;
    int results = 0;
    scanf("%d",&n);
    for(x=0;x<=n;x  ) {
        for(y=0;y<=n;y  ) {
            for(z=0;z<=n;z  ) {
                if(5*x 2*y 0.1*z==120 && x y z==n) {
                    printf("%d,%d,%d\n", x,y,z);
                    results  ;
                }
            }
        }
    }
        
    if (results == 0) {
        printf("No results");
    }
}
  •  Tags:  
  • c
  • Related