Koding C# Menghitung Jumlah
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
int hasil = 0;
Double x2 = 0.00, varians = 0;
String soal = "",
opr = "";
int angka = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Menghitung
Jumlah : ");
for (int i = 1; i
<= angka; i++)
{
if (i % 2 == 0)
{
opr = "+";
hasil = hasil + i;
}
else
{
if
(i == 1)
{
hasil = hasil + i;
}
else
{
opr = "-";
hasil = hasil - i;
}
}
x2 = x2 + Math.Pow(i, 2);
//membangun soal
soal = soal + " " + opr + " " + Convert.ToString(i);
}
//ini rumus Varians
varians = (x2 - (Math.Pow(hasil, 2))
/ angka) / angka - 1;
//tampilkan Soal hasil
Console.WriteLine(soal + "
= " + hasil + "\n");
//tampilkan Varians
Console.WriteLine("Varians
= " + varians);
Console.ReadKey();
}
}
}
0 komentar: