roberthsu2003 / cAndC-

48 stars 18 forks source link

練習4:while迴圈 #28

Closed roberthsu2003 closed 7 months ago

roberthsu2003 commented 8 months ago

請設計一個程式,讓使用者輸入數值,只有加總正偶數值,不加總正奇數值,如果輸入負數,結束程式。

yuki950120 commented 8 months ago
# include <iostream>
using namespace std;
int main(){
    int n=0,num,sum=0;
    while(true){
        n=n+1;
        cout <<"請輸入第"<<n<<"個數值:";
        cin>>num;
    if(num==-1){
        break;}
    else if(num%2==0){
        sum=sum+num;
    }
    }
    cout <<"所有輸入的正偶數的加總是"<<sum;
    return 0;
} 
a86685566 commented 8 months ago
#include <iostream>
using namespace std;

//請設計一個程式,讓使用者輸入數值,只有加總正偶數值,不加總正奇數值,如果輸入負數,結束程式。

int main() {
  int n; //用戶輸入的數字
  int total = 0; //總數
  int remainder; //計算餘數
  string text = "符合正偶數值的數字是:"; //紀錄輸入過的文字  課程還沒教
  cout << "如果輸入負數,結束程式。" << endl;
  while(true){
    cout << "請輸入一個數值:";
    cin >> n;
    remainder = n % 2;//計算餘數
    if(n < 0){
      cout << "您輸入了負數,結束程式。" << endl;
      break;
    }else if(remainder > 0 || n == 0){//餘數大於0代表為正奇數,跳過
      continue;
    }
    total += n;
    text += to_string(n) + " "; //課程還沒教
  }
  cout << text << endl << "正偶數值的合為:" << total;
}
SamHsieh0409 commented 8 months ago
#include <iostream>
using namespace std;

int main() {
  int value;
  int num = 0;
  int sum = 0;
  while(true){
    cout << "請輸入第" << num+1 << "個數值:";
    cin >> value;
    if(value < 0){
      break;
    }else if(value%2 == 1){
      num += 1;
      continue;
    }
  sum += value;
  num += 1;
  }
  cout << "所有輸入的正偶數的加總是:" << sum << endl;
}
fjkppol commented 8 months ago
#include <iostream>

using namespace std;

int main(){
    int score, sum = 0, num = 0;

    while(true){
        cout << "請輸入第" << num+1 << "個數值:";
        cin >> score;
        if(score%2 == 0) {
            sum += score;
            num += 1;
        }
        else if(score < 0) break;
        else {
            num += 1;
            continue;
        }
    }   
    printf("所有輸入的正偶數的加總是:%d\n", sum);
}
Lancehsu1030 commented 8 months ago
#include <iostream>
using namespace std;
int main() {
  int oddNO = 0;
  int num=0;
  int sum=0;  

  while(true){
    cout << "請輸入一個整數:";
    cin >> oddNO;
    if(oddNO > 0 && oddNO % 2 == 0){
      sum += oddNO;
      num += 1;      
    }else if (oddNO ==0 || oddNO %2 == 1){
      continue;
    }else if (oddNO < 0){
      break;
    }
  }    
  cout <<"你共輸入" << num << "個偶數," << "所有偶數的總和為:" << sum << endl;  
}
jackak1003 commented 8 months ago
#include <iostream>
using namespace std;
//使用while迴圈,只有偶數相加,奇數不相加,輸入負數就結束
int main() {
  int input;
  int sum=0;
  while (true){
    cout<<"請輸入任一數";
    cin>>input;
    if(input<0){
      break;
    }
    else if(input%2!=0){
      sum+=0;
    } 
    else{
      sum+=input;
    }
  }
  cout<<"所有輸入的正偶數加總為:"<<sum;  
}
aron1116 commented 8 months ago
#include<iostream> 
using namespace std;

int main() {
  int num = 0;
  int input = 0;
  int total = 0;

  while (true) {
    cout << "請輸入第" << num + 1 << "個數字: ";
    cin >> input;

    if (input < 0) {
      break; // 輸入為負數時,跳出循環
    } else if (input % 2 == 0) {
      total += input; // 輸入為偶數時,加入總和當中
      num++;
    }
  }

  cout << "所有輸入的正偶數的加總是: " << total << endl;

  return 0;
}