C# :: Aufgabe #262 :: Lösung #8
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
#8
von miami666 (300 Punkte)
- 02.02.2020 um 00:37 Uhr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aufg_262
{
class Program
{
static void Main(string[] args)
{
int divisor;
Console.WriteLine("Geben sie die Ganzzahl ein durch die die nächstgelegene Zahl teilbahr sein soll:");
int.TryParse(Console.ReadLine(), out divisor);
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 gnz = Convert.ToInt32(i / divisor);
float f = i / divisor;
float diff = f - gnz;
Console.WriteLine(i+" -> "+naehsteZahlDurchX(i, divisor));
}
Console.ReadKey();
}
public static int naehsteZahlDurchX(double i, int v)
{
int gnz = Convert.ToInt32(i / v);
float f = (float)i / v;
float diff = f - gnz;
return (Convert.ToInt32(i / v)) * v;
}
}
}Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
