Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. namespace ConsoleApplication10
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.Write("Geben Sie eine Zahl ein: ");
  8.             double zahl = Convert.ToDouble(Console.ReadLine());
  9.            
  10.             //Umwandlung negative Zahl
  11.             if (zahl < 0)
  12.                 zahl = zahl * -1;
  13.             //Umwandlung Kommazahlen
  14.             while (zahl % 1 != 0)
  15.                 zahl = zahl * 10;
  16.  
  17.             double gesamt = 0;
  18.             double quotient = 1;
  19.             int counter = -1; //Für Stellenberechnung um 1 verschoben
  20.             double quersumme = 0;
  21.  
  22.             //Schleife für Anzahl der Stellen:
  23.             for (int i = 0; quotient >= 1; i++)
  24.             {
  25.                 gesamt = Math.Pow(10, i);
  26.                 counter++;
  27.                 quotient = zahl / (double) gesamt;
  28.             }
  29.  
  30.             //Console.WriteLine(counter);
  31.             double ncounter = counter-1; //Um 1 korrigiert für 10er-Potenzen
  32.  
  33.             //Schleife für Quersumme:
  34.             while (ncounter >= 0)
  35.             {
  36.                 double stelle = Math.Floor(zahl /(double) Math.Pow(10, ncounter));
  37.                 quersumme = quersumme + stelle;
  38.                 zahl = zahl - (stelle * Math.Pow(10, ncounter));
  39.                 ncounter--;
  40.             }
  41.             Console.WriteLine("Die Quersumme der Zahl ist: {0}", quersumme);
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement