Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

dringend hilfe

Empfohlene Antworten

Veröffentlicht

Hallo,

ich habe eine Zahl in folgenden Format:

123456789

Hier soll eine Prüfziffer ausgegeben werden.

Die obige Zahl soll addiert werden= 1+2+3+4+5+6+7+8+9

Davon soll dann der Modulo % zurückgegeben werden.

wie wird so etwas programmiert.

Bsp wäre nett

zwei Möglichkeiten:

Erste)

Einzelziffern durch Division mit Zehnerpotenz erhalten:

123.456.789 / 100.000.000 = 1,23456789

(1,23456789 - 1) * 10 = 2,3456789

(2,3456789 - 2) * 10 = 3,456789

(3,456789 - 3) * 10 = 4,56789

...

und entsprechend kürzen, glaube über Math.ceil(zahl)

Zweite)

Zahl in String umwandeln:

String.valueOf(zahl)

und dann über substring jede String-Ziffer erhalten und wieder in 'echte' Ziffer verwandeln...

Die Aufgabe kann man gut mit den 2 Operatoren Div und Mod (/ und %) lösen:

Div ist eine Ganzzahlige Division ohne Rest (123/10=12)

Modulo bestimmt den Rest einer Division (123%10=3)

Dein Programm könnte folgendermaßen aussehen:

Solange deine Zahl > 1 ist,

| Bestimme mit %10 die letzte Stelle der Zahl (Ergebnis in Summe speichern ...)

| Dividiere die Zahl mit /10 (Zahl=Zahl/10)

Ausgabe der Summe

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.