Minggu, 30 November 2025

rust tebak angka

// tebak angka 1-100
use std::io; //prelude

fn main() {
    println!("Guest the number!");
    println!("Please input your guess: ");

    let mut guess = String::new(); // by default, variable in rust is immutable

    io::stdin()
        .read_line(&mut guess) // method read_line()
        .expect("Failed to read line");
    
    println!("You guessed: {guess}");

}

output:

PS D:\rust-sc\guessing_game> cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\guessing_game.exe`
Guest the number!
Please input your guess: 
7
You guessed: 7

listing2.3.cpp using std cout

// listing2.3.cpp
#include <iostream>

int main()
{
    using std::cout;
    using std::endl;

    cout << "Hello world! using std cout" << endl;
    return 0;
}

output:
 
Hello world! using std cout

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.

listing2.2.cpp using namespace std

// listing2.2.cpp
#include <iostream>

int main()
{
    using namespace std;
    cout << "Hello world! using namespace std" << endl;
    return 0;
}

output:
 
Hello world! using namespace std

Process returned 0 (0x0)   execution time : 0.081 s
Press any key to continue.

ex1104.c alfa=alfa*4.3

/* ex1104.c */
#include <stdio.h>

int main()
{
    float alfa;

    alfa=501;
    printf("alfa = %.1f\n", alfa);
    alfa=alfa+99;
    printf("alfa = %.1f\n", alfa);
    alfa=alfa-250;
    printf("alfa = %.1f\n", alfa);
    alfa=alfa/82;
    printf("alfa = %.1f\n", alfa);
    alfa=alfa*4.3;
    printf("alfa = %.1f\n", alfa);
    return(0);
}

output:
 
alfa = 501.0
alfa = 600.0
alfa = 350.0
alfa = 4.3
alfa = 18.4

Process returned 0 (0x0)   execution time : 0.073 s
Press any key to continue.

Sabtu, 29 November 2025

ex1103.c a%value

/* ex1103.c */
#include <stdio.h>

int main()
{
    const int value=5;
    int a;

    printf("Modulus %d:\n", value);
    for(a=0;a<10;a++)
        printf("%d %% %d = %d\n",a,value,a%value);
    return(0);
}

output:
 
Modulus 5:
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
8 % 5 = 3
9 % 5 = 4

Process returned 0 (0x0)   execution time : 0.083 s
Press any key to continue.

Modulus adalah nilai sisa dari pembagian. Misal 7 mod 5 = 2, karena 7 / 5 = 1, sisanya 2. 

Jumat, 28 November 2025

Mengenal cargo di rust

cargo adalah sistem build di lingkungan rust dan sekaligus sebagai package manager. Jadi, semisal kita membuat proyek besar dan melibatkan banyak file dan library, maka penggunaan cargo ini sangat membantu.

Urutan penggunaan cargo biasanya seperti ini:

  1. >>cargo new nama_project
  2. >>cd nama_project
  3. >>cargo check 
  4. >>cargo build
  5. >>cargo run
cargo new dipakai untuk membuat folder proyek. cargo check untuk cek kompilasi semua file berjalan sesuai harapan tanpa membuat file .exe nya, jadi relatif lebih irit waktu. cargo build untuk membuat file .exe dari proyeknya. Kemudian cargo run, untuk menjalankan proyek tersebut.

hello_world.rs

fn main(){
    println!("Hello World!!");
}

output:
 
PS D:\rust-sc\000.helloworld> .\hello_world.exe
Hello World!!

Nyoba Hello World! di rust. Memakai Visual Studio Code. Untuk kompilasi masih manual, menggunakan terminal:\>> rustc hello_world.rs, baru kemudian dijalankan file hello_world.exe nya.

034-01.cpp std cout

// 034-01.cpp
#include <iostream>

int main()
{
    int x=8;
    int y=6;
    std::cout << std::endl;
    std::cout << x-y << " " << x*y << " " << x+y;
    std::cout << std::endl;
    return 0;
}

output:
 
2 48 14

Process returned 0 (0x0)   execution time : 0.409 s
Press any key to continue.

ex1102.c a=b++ a=++b

/* ex1102.c */
#include <stdio.h>

int main()
{
    int a,b;
    b=16;
    printf("Before, a is unassigned and b=%d\n",b);
    a=b++; /* tidak ngaruh, seharusnya pakai ++b */
    printf("After, a=%d and b=%d\n",a,b);
    return(0);
}

output:
 
Before, a is unassigned and b=16
After, a=16 and b=17

Process returned 0 (0x0)   execution time : 0.079 s
Press any key to continue.

a=b++ tidak merubah nilai a. Nilai a ternyata tetap sama dengan b = 16. Jika ingin increment, maka penulisan increment bukan a=b++ tetapi a=++b, maka nilai a akan menjadi 17.

Kamis, 27 November 2025

Hello.cpp

// Hello.cpp
#include <iostream>

int main()
{
    std::cout << "Hello World!!" << std::endl;
    return 0;
}

output:
 
Hello World!!

Process returned 0 (0x0)   execution time : 0.091 s
Press any key to continue.

ex1101.c for c++ c--

/* ex1101.c */
#include <stdio.h>

int main()
{
    int c;
    for(c=-5;c<5;c++)
        printf("%d ",c);
    for(;c>=-5;c--)
        printf("%d ",c);
    putchar('\n');
    return(0);
}

output:
 
-5 -4 -3 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 -2 -3 -4 -5

Process returned 0 (0x0)   execution time : 0.087 s
Press any key to continue.

Rabu, 26 November 2025

ex1008.c GRID forward backwards

/* ex1008.c */
#include <stdio.h>
#define GRID 3

/* prototype */
void forward(void);
void backwards(void);

int main()
{
    puts("Grid forward: ");
    forward();
    puts("Grid backwards: ");
    backwards();
    return(0);
}

void forward(void)
{
    int x,y;
    for(x=0;x<GRID;x++)
    {
        for(y=0;y<GRID;y++)
            printf("%d:%d\t",x,y);
        putchar('\n');
    }
}

void backwards(void)
{
    int x,y;
    for(x=GRID-1;x>=0;x--)
    {
        for(y=GRID-1;y>=0;y--)
            printf("%d:%d\t",x,y);
        putchar('\n');
    }
}

output:
 
Grid forward:
0:0     0:1     0:2
1:0     1:1     1:2
2:0     2:1     2:2
Grid backwards:
2:2     2:1     2:0
1:2     1:1     1:0
0:2     0:1     0:0

Process returned 0 (0x0)   execution time : 0.123 s
Press any key to continue.

Selasa, 25 November 2025

ex1007.c sebut urutan angka 0-tebakan

/* ex1007.c */
#include <stdio.h>

void limit(int stop);
int main()
{
    int s;
    printf("Masukkan nilai stop (0-100): ");
    scanf("%d",&s);
    limit(s);
    return(0);
}

void limit(int stop)
{
    int x;
    for(x=0;x<=100;x=x+1)
    {
        printf("%d ",x);
        if(x==stop)
        {
            puts("Kamu menang!");
            return;
        }
    }
    puts("Aku menang!");
}

output:
 
Masukkan nilai stop (0-100): 10
0 1 2 3 4 5 6 7 8 9 10 Kamu menang!

Process returned 0 (0x0)   execution time : 2.727 s
Press any key to continue.

Senin, 24 November 2025

ex1006.c float convert(float f) versi-2

/* ex1006.c */
#include <stdio.h>

float convert(float f);
int main()
{
    float temp_f;
    printf("Temperatur di Fahrenheit: ");
    scanf("%f",&temp_f);
    printf("%.1fF adalah %.1fC\n",temp_f,convert(temp_f));
    return(0);
}

float convert(float f)
{
    return(f-32)/1.8;
}

output:
 
Temperatur di Fahrenheit: 200
200.0F adalah 93.3C

Process returned 0 (0x0)   execution time : 2.111 s
Press any key to continue.

Minggu, 23 November 2025

ex1005.c fahrenheit ke celcius

/* ex1005.c */
#include <stdio.h>

float convert(float f);
int main()
{
    float temp_f,temp_c;
    printf("Temperatur di Fahrenheit: ");
    scanf("%f",&temp_f);
    temp_c=convert(temp_f);
    printf("%.1fF adalah %.1fC\n",temp_f,temp_c);
    return(0);
}

float convert(float f)
{
    float a;
    a=(f-32)/1.8;
    return(a);
}

output:
 
Temperatur di Fahrenheit: 132
132.0F adalah 55.6C

Process returned 0 (0x0)   execution time : 5.134 s
Press any key to continue.

Sabtu, 22 November 2025

ex1004.c void graph(int count)

/* ex1004.c */
#include <stdio.h>

void graph(int count);
int main()
{
    int value;
    value=2;
    while(value<=64)
    {
        graph(value);
        printf("Value is %d\n",value);
        value=value*2;
    }
    return(0);
}

void graph(int count)
{
    int x;
    for(x=0;x<count;x=x+1)
    {
        putchar('*');
    }
    putchar('\n');
}

output:
 
**
Value is 2
****
Value is 4
********
Value is 8
****************
Value is 16
********************************
Value is 32
****************************************************************
Value is 64

Process returned 0 (0x0)   execution time : 0.104 s
Press any key to continue.

Jumat, 21 November 2025

ex1003.c void vegas(void)

/* ex1003.c */
#include <stdio.h>

void vegas(void);

int main()
{
    int a;

    a=365;
    printf("In the main() function, a=%d\n",a);
    vegas();
    printf("In the main() function, a=%d\n",a);
    return(0);
}

void vegas(void)
{
    int a;

    a=-10;
    printf("In the vegas() function, a=%d\n",a);
}

output:
 
In the main() function, a=365
In the vegas() function, a=-10
In the main() function, a=365

Process returned 0 (0x0)   execution time : 0.076 s
Press any key to continue.

ex1002.c void prompt(void) first

/* ex1002.c */
#include <stdio.h>

void prompt(void)
{
    printf("C:\\DOS> ");
}

int main()
{
    int loop;
    char input[32];

    loop=0;
    while(loop<5)
    {
        prompt();
        fgets(input,32,stdin);
        loop=loop+1;
    }
    return(0);
}

output:
 
C:\DOS> a
C:\DOS> b
C:\DOS> c
C:\DOS> d
C:\DOS> e

Process returned 0 (0x0)   execution time : 6.433 s
Press any key to continue.

Kamis, 20 November 2025

ex1001.c basic function no return

/* ex1001.c */
#include <stdio.h>

void prompt(); /* function prototype */

int main()
{
    int loop;
    char input[32];

    loop=0;
    while(loop<5)
    {
        prompt();
        fgets(input,32,stdin);
        loop=loop+1;
    }
    return(0);
}

/* display prompt */
void prompt(void)
{
    printf("C:\\DOS>");
}

output:
 
C:\DOS>1
C:\DOS>2
C:\DOS>3
C:\DOS>4
C:\DOS>5

Process returned 0 (0x0)   execution time : 3.370 s
Press any key to continue.

Output saya input nomer 1-5 berturut-turut seperti gambar di atas.

Rabu, 19 November 2025

ex0911.c for() loop

/* ex0911.c */
#include <stdio.h>

int main()
{
    int x;
    for(x=0;x<=4;x=x+1,printf("%d\n",x));
    return(0);
}

output:
 
1
2
3
4
5

Process returned 0 (0x0)   execution time : 0.058 s
Press any key to continue.

.