ChatGPTでプログラミングコードを作ってみた

chagpt

PythonでのFizzBuzz問題の解決方法:

pythonCopy codefor i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Javaでの階乗の計算:

javaCopy codeimport java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        int factorial = 1;
        
        for (int i = 1; i <= number; i++) {
            factorial *= i;
        }
        
        System.out.println("Factorial of " + number + " is " + factorial);
    }
}

C言語でのバブルソートの実装:

cCopy code#include <stdio.h>

void bubbleSort(int array[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (array[j] > array[j + 1]) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}

int main() {
    int array[] = {64, 34, 25, 12, 22, 11, 90};
    int size = sizeof(array) / sizeof(array[0]);
    
    bubbleSort(array, size);
    
    printf("Sorted array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", array[i]);
    }
    
    return 0;
}

Rubyでの素数の判定:

rubyCopy codedef prime?(number)
  return false if number <= 1

  (2..Math.sqrt(number)).none? { |i| number % i == 0 }
end

puts prime?(17)  # Output: true
puts prime?(25)  # Output: false

JavaScriptでの配列の要素の合計値の計算:

javascriptCopy codeconst numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((total, num) => total + num, 0);
console.log("Sum: " + sum);  // Output: Sum: 15

PHPでのファイルの読み込みと出力:

phpCopy code<?php
$file = fopen("example.txt", "r");
if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line;
    }
    fclose($file);
}
?>

Swiftでのクラスの定義とオブジェクトの使用:

swiftCopy codeclass Person {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self
       self.name = name
        self.age = age
    }
    
    func introduce() {
        print("My name is \(name) and I am \(age) years old.")
    }
}

let person = Person(name: "John", age: 25)
person.introduce()  // Output: My name is John and I am 25 years old.

C#での文字列の逆順反転:

csharpCopy codeusing System;

class Program {
    static void Main(string[] args) {
        string input = "Hello, World!";
        char[] charArray = input.ToCharArray();
        Array.Reverse(charArray);
        string reversed = new string(charArray);
        Console.WriteLine(reversed);  // Output: !dlroW ,olleH
    }
}

Kotlinでの非同期処理の実行:

kotlinCopy codeimport kotlinx.coroutines.*
import java.util.concurrent.Executors

fun main() {
    val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
    val job = GlobalScope.launch(dispatcher) {
        delay(1000)
        println("Delayed message")
    }
    
    runBlocking {
        job.join()
    }
}

TypeScriptでのクラスの継承とメソッドのオーバーライド:

typescriptCopy codeclass Animal {
    name: string;
    
    constructor(name: string) {
        this.name = name;
    }
    
    speak() {
        console.log(this.name + " makes a sound.");
    }
}

class Dog extends Animal {
    speak() {
        console.log(this.name + " barks.");
    }
}

const dog = new Dog("Buddy");
dog.speak();  // Output: Buddy barks.

以上が、10種類のプログラミングコードの例です。それぞれ異なる目的や機能を持つコードですので、お好みのものを選んでご利用ください。

コメント

タイトルとURLをコピーしました