Advertisement
tom19960222

列出偶數個數&總和

Oct 22nd, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. /* 既然老師說不考陣列 那我乾脆也不用陣列了... */
  2. /* 寫到一半的OS: WTF 如果不用陣列 又不從檔案輸入測試資料的話 要怎麼印出所有偶數? 不會變成邊打邊印嗎?
  3.    決定還是用陣列了..... XD */
  4. #include <stdio.h>
  5.  
  6. int main ()
  7. {
  8.     int num, sum=0, even_count=0, even_array[100], i;
  9.    
  10.     printf ("Please input numbers (-1 to exit): ");
  11.    
  12.     while (1)
  13.     {
  14.         scanf ("%d", &num);
  15.         if (num == -1) break;
  16.         if (num % 2 == 0)
  17.         {
  18.             even_array[even_count] = num;
  19.             even_count++;
  20.             sum += num;
  21.         }
  22.     }
  23.    
  24.     printf ("There are %d even numbers are in the array:\n", even_count);
  25.     for (i = 0; i < even_count; i++)
  26.         printf ("%d ", even_array[i]);
  27.     printf ("\n\nThe sum of these numbers are %d\n", sum);
  28.    
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement