0% found this document useful (0 votes)
19 views21 pages

PBO Csharp

Uploaded by

Asaka Misha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views21 pages

PBO Csharp

Uploaded by

Asaka Misha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PBO C#

Hello World
• using System;

• namespace App // Note: actual namespace depends on the project name.


• {
• class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Hello World!!");
• }
• }
• }
Tipe Data
• Data Angka
• 1. Int
• 2.Double
• 3. Decimal
• 4. Float

• 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;

• namespace App // Note: actual namespace depends on the project name.


• {
• class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Hello World!!");

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("Enter your age:");


• int age = Convert.ToInt32(Console.ReadLine());
• Console.WriteLine("Your age is: " + age);
• int p,l,t;
• float luas, volume;
• Console.WriteLine("Perhitungan Balok");
• Console.Write("Masukkan Panjang :");
• p= int.Parse (Console.ReadLine());
• Console.Write("Masukkan Lebar :");
• l = int.Parse(Console.ReadLine());
• Console.Write("Masukkan Tinggi :");
• t = int.Parse(Console.ReadLine());
• // Rumus
• luas = 2 * ((p * l) + (p * t) + (l * t));
• volume = p * l * t;

• 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());

• if (suhu >= 20 && temp <=35)


•{
• Console.WriteLine(“cuacanya panas”)
•}
while loop
• Console.Write("Masukan nama:");
• String nama = Console.ReadLine();

• while (nama == "")


• {
• Console.Write("Masukan nama:");
• nama = Console.ReadLine();
• }

• Console.WriteLine("Halo " + nama);


• Console.ReadKey();
• // See https://aka.ms/new-console-template for more information
• using System;
• namespace ConsoleApplication5
• {
• class Mobil
• {
• public virtual void jalan()
• {
• Console.WriteLine("Mobil berjalan");
• }
• }
• // derived class of Mobil
• class Ferrari : Mobil
• {
• // overriding method from Animal
• public override void jalan()
• {
• Console.WriteLine("Mobil berjalan kencang");
• } }
• class Program
• {
• static void Main(string[] args)
• {
• // object of derived class
• Ferrari labrador = new Ferrari();
• // accesses overridden method
• labrador.jalan();
• } }}

You might also like