negative Zahlen in positive Zahlen umwandeln
-
Oder ohne eine extra-Funktion:
int x = -3; x = -x; cout << x; // Ausgabe: 3
Caipi
-
Mathematisch: *(-1)
Kann man 1 zu 1 auch in Code übernehmen.
-
So könnte dann ein ganzes Programm aussehen:
Das Programm wandelt z.B. 3 in -3 und -3 in 3!
#include <iostream> #include <conio.h> using namespace std; int zahl; int positivezahl; int main() { cout <<"\n Bitte geben sie eine negative Zahl ein."; cin >> zahl; positivezahl= zahl*(-1); cout <<"\n" << positivezahl; getch(); }
-
int a = -4; a = a < 0 ? a * -1 : a;
-
#include <algorithm> int main() { int a = abs(-7); }
siehe erste Antwort
-
Artchi schrieb:
Mathematisch: *(-1)
Kann man 1 zu 1 auch in Code übernehmen.
Was man allerdings nicht tun sollte. Das verringert nur die Lesbarkeit. Wozu gibts denn den Negations Operator?
-
int b = -3; b = (b^-1)+1;
-
Wie wärs mit was ganz doofem?
if (a < 0) a = -a;
-
oder auch
#include <cmath> double MiNuSzUpLuS(double foo) { return std::sqrt(foo*foo); }
versuchen wir uns nicht gerade in "obfuscated programming" oder wie das heißt?
-
oder auch
a*=2*(a>=0)-1;