C# :: Aufgabe #262 :: Lösung #3
8 Lösungen
#262
Array von Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln
Anfänger - C#
von Gustl
- 08.05.2019 um 20:08 Uhr
Schreibe ein Programm welches aus einem Array von Dezimalzahlen diese Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln.
Etwa so:
Etwa so:
Konsolenausgabe:
7.1 => 5
8.4 => 10
-2.4 => 0
-2.6 => -5
-8.3 => -10
#3
von Gisbert5020 (3120 Punkte)
- 30.05.2019 um 20:12 Uhr
using System;
using System.Collections.Generic;
using System.Text;
namespace Teilung5
{
class Program
{
static void Main(string[] args)
{
float[] arr = new float[5];
arr[0] = 7.1f;
arr[1] = 8.4f;
arr[2] = -2.4f;
arr[3] = -2.6f;
arr[4] = -8.3f;
foreach(float i in arr)
{
int ganz = Convert.ToInt32(i/5);
float dez = i / 5;
float div = dez - ganz;
int wert;
if (div >= 0.5)
{
wert = (ganz + 1) * 5;
Console.WriteLine(i + " => " + wert);
}
else
{
wert = ganz * 5;
Console.WriteLine(i + " => " + wert);
}
}
Console.ReadLine();
}
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
