PBO Csharp
PBO Csharp
Hello World
• using System;
• Data Huruf
• 1.Char
• 2. String
• Boolean
• 1.False
• 2. True
• int myNum = 5; // Integer (whole number)
• double myDoubleNum = 5.99D; // Floating point number
• char myLetter = 'D'; // Character
• bool myBool = true; // Boolean
• string myText = "Hello"; // String
Output tulisan
• using System;
Console.WriteLine("Hello \n World!!");
• }
• }
• }
Input
• using System;
• namespace App
• {
• class Program
• {
• static void Main(string[] args)
• int x;
• x=133;
• int y=122;
• Console.WriteLine(x);
• Console.WriteLine(y);
• }
}
• }
output variabel
• using System;
• namespace App // Note: actual namespace depends on the project name.
• {
• class Program
• {
• static void Main(string[] args)
• {
• int x;
• x=133;
• int y=122;
• int z= x+y;
• Console.WriteLine(x);
• Console.WriteLine(y);
• Console.WriteLine(z);
• }
• }
• }
Output
• string firstName = "John ";
• string lastName = "Doe";
• string fullName = firstName + lastName;
• Console.WriteLine(fullName);
• using System;
• namespace App
• {
• class Program
• {
• static void Main(string[] args){
• int umur = 15;
• double tinggi_badan=170.5;
• bool pelajar = true;
• char symbol = '@';
• String nama = "namaku";
• Console.WriteLine(nama);
• Console.WriteLine(umur);
• Console.WriteLine(tinggi_badan);
• Console.WriteLine(pelajar);
String username = symbol + nama;
• Console.WriteLine(username);}
• }
• }
Input
• Console.WriteLine("Enter username:");
• string userName = Console.ReadLine();
• Console.WriteLine("Username is: " + userName);
• Console.WriteLine("\n\n========================================");
• Console.WriteLine("Luas Balok adalah " + luas);
• Console.WriteLine("Volume Balok adalah " + volume);
• Console.WriteLine("\n\n========================================");
Casting
• int Intsaya = 9;
• double doubles= Intsaya; // Automatic casting: int to double
• Console.WriteLine(myInt); // Outputs 9
• Console.WriteLine(myDouble); // Outputs 9
Random number
• Random random = new Random();
• int num = random.Next(1, 7);
• //double num= random.NextDouble();
• Console.WriteLine(num);
• Console.ReadKey();
if else
• if (20 > 18)
•{
• Console.WriteLine("20 is greater than 18");
•}
• int nilai;
• Console.Write("Masukan Nilai =");
• nilai = int.Parse(Console.ReadLine());
• if (nilai % 2 == 0)
• {
• Console.WriteLine(" Bilangan genap");
• } else
• Console.WriteLine(" Bilangan ganjil");
• int nilai;
• Console.Write("Masukan Nilai = ");
• nilai = int.Parse(Console.ReadLine());
• if (nilai <= 70)
• {
• Console.WriteLine(" Grade C");
• } else if (nilai <= 80) {
• Console.WriteLine(" Grade B"); }
•
• else if (nilai <= 100) {
• Console.WriteLine(" Grade A");
For
• int angka;
• for (angka=1;angka<=10;angka++) {
• Console.WriteLine("ini pengulangan 10 kali");
For
• int angka;
• for (angka=0;angka<=100;angka+=10) {
• Console.WriteLine(angka);
logical operator
• Console.Write("Masukan berapa suhu cuaca diluar = ");
• double suhu = Convert.ToDouble(Console.ReadLine());