C言語で年齢を計算するプログラムの作成方法
現在の日付とある人物の生年月日が与えられ、その人の現在の年齢(年・月・日)を計算するのが課題となります。本記事では、C言語を使って年齢を求めるプログラムの考え方と実装方法を解説します。
例
入力 -: 現在の日付 -: 21/9/2019
生年月日 -: 25/9/1996
出力 -: 現在の年齢
年: 22 月: 11 日: 26
使用するアプローチは以下の通りです −
- 現在の日付と生年月日を入力として受け取る
- 以下の条件をチェックする
- 現在の月が誕生月より小さい場合、今年はまだ誕生月に達していないため現在の年をそのまま使わず、現在の月に12を加算して月の差を計算します。
- 現在の日が誕生日より小さい場合、月をまたいで計算する必要があるため、現在の日付に前月の日数を加算してから差し引くことで、正しい日数の差を求めます。
- 条件の調整が完了したら、年・月・日をそれぞれ引き算して最終的な結果を求めます。
- 最終的な年齢を出力します。
アルゴリズム
開始
ステップ 1-> 年齢を計算する関数を宣言する
void age(int present_date, int present_month, int present_year, int birth_date, int birth_month, int birth_year)
各月の日数を格納する配列 int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } を用意
IF (birth_date > present_date)
present_date に month[birth_month - 1] を加算
present_month から 1 を減算
End
IF (birth_month > present_month)
present_year から 1 を減算
present_month に 12 を加算
End
int final_date = present_date - birth_date を計算
int final_month = present_month - birth_month を計算
int final_year = present_year - birth_year を計算
final_year, final_month, final_date を出力
ステップ 2-> main() 内で
int present_date = 21 を設定
int present_month = 9 を設定
int present_year = 2019 を設定
int birth_date = 25 を設定
int birth_month = 9 を設定
int birth_year = 1996 を設定
age() 関数を呼び出す
停止
サンプルコード
#include <stdio.h>
#include <stdlib.h>
// 現在の年齢を計算する関数
void age(int present_date, int present_month, int present_year, int birth_date, int birth_month, int birth_year) {
// 各月の日数(2月は28日として扱う)
int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (birth_date > present_date) {
present_date = present_date + month[birth_month - 1];
present_month = present_month - 1;
}
if (birth_month > present_month) {
present_year = present_year - 1;
present_month = present_month + 12;
}
int final_date = present_date - birth_date;
int final_month = present_month - birth_month;
int final_year = present_year - birth_year;
printf("Present Age Years: %d Months: %d Days: %d", final_year, final_month, final_date);
}
int main() {
int present_date = 21;
int present_month = 9;
int present_year = 2019;
int birth_date = 25;
int birth_month = 9;
int birth_year = 1996;
age(present_date, present_month, present_year, birth_date, birth_month, birth_year);
return 0;
}
出力
上記のコードを実行すると、以下の出力が得られます。
Present Age Years: 22 Months:11 Days: 26
このプログラムでは、単純な引き算だけでは正しい年齢が求められないケース(誕生日がまだ来ていない場合など)を考慮し、月ごとの日数を利用して繰り下げ処理を行っています。ただし、この実装では2月を常に28日として扱うため、うるう年への対応が必要な場合は別途判定処理を追加してください。
-
C言語でアームストロング数を判定するプログラムの作成方法
アームストロング数とは? 本記事では、入力された整数 n がアームストロング数(Armstrong number)であるかどうかを判定するCプログラムについて解説します。 アームストロング数とは、各桁の数字をそれぞれ「桁数(位数)」でべき乗し、その総和が元の数値と等しくなる数のことです。例えば、4桁の数 1634 は 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634 となるため、アームストロング数です。 アームストロング数を求める基本的な考え方は、次の式のように表せます。 計算式: wxyz…. = pow(w, n) + pow(x, n) + p
-
C言語で十角形の周囲長を計算する方法を解説
十角形(デカゴン)とは? 十角形(デカゴン)とは、10個の辺を持つ多角形のことで、「10角形」とも呼ばれます。10個の頂点と10本の辺を持ちます。特に正十角形の場合、すべての辺の長さが等しく、それぞれの内角は144度になります。 以下は十角形の図です。 十角形の周囲長を求めるには、次のシンプルな公式を使用します。 周囲長 = 10 × 辺の長さ 計算例 入力:side = 10 出力:十角形の周囲長:100 入力:side = 20 出力:十角形の周囲長:200 アルゴリズム 開始 ステップ1 → 周囲長を求める関数を宣言する void perimeter(int n)