Autor Beitrag
BlackPhantom
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 144

Win 2000, Win XP, UNIX
D7 Prof, C, HTML, VB 5
BeitragVerfasst: Di 19.04.05 08:47 
Hi,

wie es aus dem Titel oben herausgeht: Ich möchte ein kleines Prog schreiben mit dem ich eine Zahl eingebe und dann überprüfe ob es eine gerade Zahl ist.

ich habe einfach zahl % 2 == 0 geschrieben und ausgegeben. Klappt eigentlich bei vielen Zahlen aber zB nicht bei der Zahl 30. Logisch :wink: aber wie mach ich das, dass das Prog mir dann ausgibt das auch 30 eine gerade Zahl ist?

Einer eine Idee?

thx4all

peace
BlackP

_________________
Wir meinen es immer persönlich, wir müssen es nur mit Abstand tun!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 19.04.05 08:55 
Hilf mir mal schnell auf die Sprünge...Wieso ist 30%2!=0, in Delphi also 30 MOD 2 <>0?

_________________
We are, we were and will not be.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 19.04.05 09:01 
user profile iconBlackPhantom hat folgendes geschrieben:
Hi,

wie es aus dem Titel oben herausgeht: Ich möchte ein kleines Prog schreiben mit dem ich eine Zahl eingebe und dann überprüfe ob es eine gerade Zahl ist.

ich habe einfach zahl % 2 == 0 geschrieben und ausgegeben. Klappt eigentlich bei vielen Zahlen aber zB nicht bei der Zahl 30. Logisch :wink: aber wie mach ich das, dass das Prog mir dann ausgibt das auch 30 eine gerade Zahl ist?

Einer eine Idee?

thx4all

peace
BlackP


30%2 ist nicht 0 :shock: Und ich habe immer gedacht, ich bin in Mathe halbwegs fit...

AXMD
opfer.der.genauigkeit
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 754
Erhaltene Danke: 1



BeitragVerfasst: Di 19.04.05 09:08 
ausblenden Quelltext
1:
2:
if( ( value % 2 ) == 0 ) 
{your command]


% ist modulo

Ist doch richtig so, warum spuckt der bei 30 was anderes raus?

//Edit:

ausblenden Quelltext
1:
2:
3:
4:
5:
float result // gibt es diesen Datentypen bei C?
result = ( value / 2 );

if( ( result - int()result ) == 0 )
{your command}


Könnte klappen :gruebel:

_________________
Stellen Sie sich bitte Zirkusmusik vor.
BlackPhantom Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 144

Win 2000, Win XP, UNIX
D7 Prof, C, HTML, VB 5
BeitragVerfasst: Di 19.04.05 12:13 
Es hat eigentlich gut geklappt.

Danke @ opfer



PS: Für alle Nicht-Wissenden "30 % 2 == 0" heisst in der C-Sprache soviel wie: Teile 30 durch 2 und gucke ob der Rest gleich Null ist.

_________________
Wir meinen es immer persönlich, wir müssen es nur mit Abstand tun!
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Di 19.04.05 12:23 
Ja, dann stimmts doch, oder nicht?

ausblenden Quelltext
1:
30 / 2 = 15 Rest 0					

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
blackbirdXXX

ontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1077
Erhaltene Danke: 1

Ubuntu Dapper

BeitragVerfasst: Di 19.04.05 21:09 
Also ich komme auf
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
#include <stdio.h>

int main () {
        int ausgabe = 30 % 2;
        printf("%i\n",ausgabe);
        return 0;
}


ausblenden Quelltext
1:
2:
3:
blackbird@activetravel:~/Developement/C/area51$ g++ modulo.c
blackbird@activetravel:~/Developement/C/area51$ ./a.out
0


Schaut so aus, als wäre der Rest sehr wohl 0. Und warum sollte er es nicht sein?

_________________
Klein, schwarz und ärgert Techniker? Jumper!