Home > Software engineering >  How can I increase the absolute value of a number by 1?
How can I increase the absolute value of a number by 1?

Time:10-30

I need that if for y>0 the value increases by 1, and if it is less then it does not change, how can i fix my code?

#include <stdio.h>

long double main() {
    long double y = (long long int)y;
    long double z = (long long int)z;
    long double v = (long long int)v;

    scanf("%lld", &y);
    v = (y > 0) ? y : y;
    printf("%lld", v);
    return 0;
}

example : 4 = 5 (4 1) | -3 = -3

CodePudding user response:

Here is a version of your program that outputs the same value if negative, or one more if positive.

#include <stdio.h>

int main(void) {                    // conforming definition
    long long y, v;                 // consistent types
    if(scanf("%lld", &y) != 1) {    // how many successful conversions?
        return 1;                   // bad entry
    }
    v = (y > 0) ? y   1 : y;        // ternary operator
    printf("%lld", v);
    return 0;
}

It has a hole on it though: when the entered value is LLONG_MAX which cannot be incremented.

  •  Tags:  
  • c
  • Related