程式作業 第9題 for 四子一丙

第九題 剛剛我自己在看題目的時候才發現到題目是要輸出Sum(100) Sum(1000)...等
但是我是輸出使用者任意打的數字的總和 第一個是我原來寫的 第二個是後來改的
差別就多了一個自動輸出Sum(100) Sum(1000)...等
剛抄的同學可以直接抄第三個

#include <stdio.h>
#include <stdlib.h>

int Sum(int n)
{
 int i = 1,total = 0;
 while(i <= n ) 
 {
     total += i;
     i++;
 }
 return (total);
}
int main(void)
{
 unsigned long iTotal = 0;
 
        printf("請輸入n ");
        scanf("%d",&iTotal);
    
        printf("總和為%d\n",Sum(iTotal));
   
 system("pause"); return(0);
}

#include <stdio.h>
#include <stdlib.h>

int Sum(int n)
{
 int i = 1,total = 0;
 while(i <= n ) 
 {
     total += i;
     i++;
 }
 return (total);
}
int main(void)
{
 unsigned long iTotal = 0;
 
        printf("請輸入n ");
        scanf("%d",&iTotal);
    
        printf("輸入的總和為%d\n",Sum(iTotal));
        printf("Sum(100)總和為%d\n",Sum(100));
        printf("Sum(1000)總和為%d\n",Sum(1000));
        printf("Sum(1000)總和為%d\n",Sum(1000));
        printf("Sum(50000)總和為%d\n",Sum(50000));
   
 system("pause"); return(0);
}

#include <stdio.h>
#include <stdlib.h>

int Sum(int n)
{
 int i = 1,total = 0;
 while(i <= n ) 
 {
     total += i;
     i++;
 }
 return (total);
}
int main(void)
{
        printf("Sum(100)總和為%d\n",Sum(100));
        printf("Sum(1000)總和為%d\n",Sum(1000));
        printf("Sum(1000)總和為%d\n",Sum(1000));
        printf("Sum(50000)總和為%d\n",Sum(50000));
   
 system("pause"); return(0);
}

留言

這個網誌中的熱門文章

C# 井字遊戲