C言語で貯金をするプログラム
auです。
目標額を設定して毎月金額を入力すると、目標までいくらか教えてくれるプログラムを組みました。
#include <stdio.h> int main(void) { int goal; int save_money; int total = 0; int month = 0; int year; // 目標額の設定 printf("目標額: "); scanf("%d", &goal); // 目標額を合計額が超えるまでループ while (goal > total) { printf("今月の貯金額: "); scanf("%d", &save_money); total += save_money; // その月の貯金が終わったら+1 month++; // 目標に達しなかった場合現在の貯金が苦と目標まであと何円か教える if (goal >= total) { printf("現在の貯金額: %d\n", total); printf("目標まで%d円\n", goal - total); } } // かかった期間 year = month / 12; month = month % 12; printf("目標額を達成しました\n"); printf("%d年%dヶ月です\n", year, month); return 0; } // 実行結果 目標額: 10000 今月の貯金額: 500 現在の貯金額: 500 目標まで9500円 今月の貯金額: 100 現在の貯金額: 600 目標まで9400円 今月の貯金額: 500 現在の貯金額: 1100 目標まで8900円 今月の貯金額: 400 現在の貯金額: 1500 目標まで8500円 今月の貯金額: 500 現在の貯金額: 2000 目標まで8000円 今月の貯金額: 100 現在の貯金額: 2100 目標まで7900円 今月の貯金額: 2000 現在の貯金額: 4100 目標まで5900円 今月の貯金額: 5000 現在の貯金額: 9100 目標まで900円 今月の貯金額: 200 現在の貯金額: 9300 目標まで700円 今月の貯金額: 300 現在の貯金額: 9600 目標まで400円 今月の貯金額: 500 目標額を達成しました 0年11ヶ月です
ちゃんと計算してくれました。
ちなみに毎月稼いだ金額分何かしらに使ってしまうので全然お金が貯まらないです。