6.     現在你修改第五題的程式,讓它計算數字的平方和 (如果你喜歡,你也可以算第一天存$1元、第二天存$4元、第三天存$9元,以此類推,有多少錢?這樣會比較真實些)C語言沒有平方函數,你可以用n*n取代之。

#include "stdafx.h"

#include <stdio.h>                         //引入stdio.h

#include <stdlib.h>                        //引入stdlib.h

int main(int argc, char* argv[])

{

    int count=0,sum=0,times=0;             //宣告3個變數countsumtimes,個別初始為

    printf("How many times that you want to calculate?\n");

    scanf("%d", &times);                   //使用者輸入要執行的次數

    while(count++ < times)                 //迴圈判斷,count累加前是否小於times

       sum = sum+ count*count;             //sum加上count的平方

    printf("sum = %d\n", sum);

    system("PAUSE");                       //「按任意鍵繼續...」的程式,讓程式暫停

    return 0;                              //函數結束,傳回整數並跳回原本呼叫的地方

}

 

arrow
arrow
    全站熱搜

    Johnny 鋼鍊 發表在 痞客邦 留言(0) 人氣()