1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C++の基本

Posted at

出力コード

#include <iostream>
using namespace std;

int main(){
    cout << "Hello World" << "\n";
    return 0;
}

いろいろな値を出力

#include <iostream>
using namespace std;

int main() {
	cout << "A" << "\n";
	cout << "ようこそC++へ" << "\n";
	cout << 123 << "\n";
	return 0;
}

文字コードの出力

#include <iostream>
using namespace std;

int main() {
	cout << "8進数101の文字コードを持つ文字は" << "\101" << "です" << "\n";
	cout << "16進数41の文字コードを持つ文字は" << "\x41" << "です" << "\n";
	return 0;
}

数値リテラルの出力

#include <iostream>
using namespace std;

int main(){
    cout << "10進数の5は" << 5 << "です。" << "\n";
    cout << "8進数の5は" << 05 << "です。" << "\n";
    cout << "16進数の5は" << 0x5 << "です。" << "\n";
    cout << "16進数のAは" << 0xA << "です。" << "\n";
    return 0;
}

変数で出力

#include <iostream>
using namespace std;

int main() {
	int num;
	num = 3;
	cout << "変数numの値は" << num << "です" << "\n";
	return 0;
}

変数の上書き出力

#include <iostream>
using namespace std;

int main() {
	int num;
	num = 1;
	cout << "変数numの値は" << num << "です" << "\n";
	num = 3;
	cout << "変数の値を変更します" << "\n";
	cout << "変数numの新しい値は" << num << "です" << "\n";
	return 0;
}

複数の変数

#include <iostream>
using namespace std;

int main() {
	int num1, num2;
	num1 = 1;
	cout << "変数num1の値は" << num1 << "です" << "\n";
	num2 = 3;
	cout << "変数num2の値は" << num2 << "です" << "\n";
	return 0;
}

複数の変数の入力

#include <iostream>
using namespace std;

int main(){
    int num1, num2;
    cout << "2つの整数を入力してください。" << "\n";
    cin >> num1 >> num2;
    cout << "num1の値は" << num1 << "です。" << "\n";
    cout << "num2の値は" << num2 << "です。" << "\n";
    return 0;
}

整数と浮動小数点の変数比較

#include <iostream>
using namespace std;

int main() {
	int num1;
	double num2;

	num1 = 3.14;
	num2 = 3.14;

	cout << "変数num1の値は" << num1 << "です" << "\n";
	cout << "変数num2の値は" << num2 << "です" << "\n";
	return 0;
}

if文

#include <iostream>
using namespace std;

int main() {
	int res;
	cout << "整数を入力してください" << endl;
	cin >> res;

	if (res == 1) {
		cout << "1を入力しました" << endl;
	}
	cout << "処理を終了します" << endl;
	return 0;
}

if elseif else文

#include <iostream>
using namespace std;

int main() {
	char res;
	cout << "あなたは男性ですか" << endl;
	cout << "YまたはNを入力してください" << endl;
	
	cin >> res;

	if (res == 'Y' or res == 'y') {
		cout << "あなたは男性です" << endl;
	}
	else if (res == 'N' or res == 'n') {
		cout << "あなたは女性です" << endl;
	}
	else {
		cout << "YかNを入力してください";
	}
	return 0;
}

switch文

#include <iostream>
using namespace std;

int main() {
	int res;
	cout << "整数を入力してください" << endl;
	cin >> res;
	switch (res) {
	case 1:
		cout << "1が入力されました" << endl;
		break;
	case 2:
		cout << "2が入力されました" << endl;
		break;
	case 3:
		cout << "1か2を入力してください" << endl;
	}
	return 0;
}

for文

#include <iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 10; i++) {
		cout << i << "番目繰り返しです" << endl;
	}
	return 0;
}

for文での総和

#include <iostream>
using namespace std;

int main() {
	int num;
	int sum = 0;

	cout << "いくつまでの合計を求めますか" << endl;
	cin >> num;

	for (int i = 1; i <= num; i++) {
		sum += i;
	}

	cout << "1から" << num << "までの合計値は" << sum << "です" << endl;
	return 0;
}

while文

#include <iostream>
using namespace std;

int main() {
	int i = 1;
	while (i <= 10) {
		cout << i << "番目繰り返しです" << endl;
		i++;
	}
	return 0;
}

while文での総和

#include <iostream>
using namespace std;

int main() {
	int i = 1;
	int num;
	int sum = 0;

	cout << "いくつまでの合計を求めますか" << endl;
	cin >> num;

	while (i <= num) {
		sum += i;
		i++;
	}

	cout << "1から" << num << "までの合計値は" << sum << "です" << endl;
	return 0;
}

do while文

#include <iostream>
using namespace std;

int main() {
	int i = 1;
	do {
		cout << i << "番目繰り返しです" << endl;
		i++;
	} while (i <= 10);
	return 0;
}

二重for文

#include <iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 3; i++) {
		for (int j = 1; j <= 3; j++) {
			cout << "iは" << i << "jは" << j << "です" << endl;
		}
	}
	return 0;
}

関数

#include <iostream>
using namespace std;

void buy() {
	cout << "りんごを買いました" << endl;
}

int main() {
	buy();
	return 0;
}

関数 引数有

#include <iostream>
using namespace std;

void buy(int x) {
	cout << x << "円のりんごを買いました" << endl;
}

int main() {
	buy(100);
	buy(50);
	return 0;
}

関数 引数2つ

#include <iostream>
using namespace std;

void buy(int x, int y) {
	cout << x << "円と" << y << "円のりんごを買いました";
}

int main() {
	buy(100, 50);
	return 0;
}

関数 戻り値

#include <iostream>
using namespace std;

int sum(int x, int y) {
	return x + y;
}

int main() {
	int num1, num2, ans;
	cout << "1番目の整数を入力" << endl;
	cin >> num1;
	cout << "2番目の整数を入力" << endl;
	cin >> num2;

	ans = sum(num1, num2);

	cout << "合計は" << ans << "です" << endl;
	return 0;
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?