Formatted output
/csbook/solutions/07/A02.cs
using System;
class Test {
static void PrintTable(int n) {
for (int i = 1; i <= n; i++)
Console.WriteLine("{0,3}: {1,5} {2,7:f3}", i, i*i, Math.Sqrt(i));
}
static void Main(string[] arg) {
try {
PrintTable(Convert.ToInt32(arg[0]));
} catch {
Console.WriteLine("-- positive ganze Zahl als Argument erwartet");
}
}
}
|
A call of Test 10 yields:
1: 1 1.000
2: 4 1.414
3: 9 1.732
4: 16 2.000
5: 25 2.236
6: 36 2.449
7: 49 2.646
8: 64 2.828
9: 81 3.000
10: 100 3.162
|