Guest User

Untitled

a guest
Dec 11th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Yhtalolaskuri
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int results = 0;
  13.             double result = 0, result1 = 0, result2 = 0, x = 0;
  14.  
  15.             //Start looking for the right x
  16.             while (results < 2)
  17.             {
  18.                 //Calculate the result with the current x
  19.                 result = (Math.Pow(x, 2.0) * 2.0) - 5.0 * x - 3.0;
  20.                 Console.WriteLine("x\t=\t{0}\tresult\t=\t{1}", x, result);
  21.  
  22.                 //If it's a correct answer, store to result1 or result2
  23.                 if (result == 0 && results < 1)
  24.                 {
  25.                     result1 = x;
  26.                     results++;
  27.                 }
  28.                 else if (result == 0 && results < 2)
  29.                 {
  30.                     result2 = x;
  31.                     results++;
  32.                 }
  33.  
  34.                 //Switch x sign and increment it if it's positive
  35.                 if (x > 0)
  36.                 {
  37.                     x *= -1;
  38.                 }
  39.                 else if (x <= 0)
  40.                 {
  41.                     x *= -1;
  42.                     x+= 0.1;
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine("Yhtälö on tosi kun x on {0} tai {1}", result1, result2);
  47.             Console.ReadKey();
  48.  
  49.         }
  50.     }
  51. }
Add Comment
Please, Sign In to add comment