본문 바로가기
카테고리 없음

C++

by 수삼이는코딩중 2023. 9. 21.
728x90

If else

#include <iostream>
using namespace std;

int main() {
  int hours;
  double grossPay, rate;
  cout << "Enter the hourly rate of pay : $";
  cin >> rate;
  cout << "Enter the number of hours worked,\n"
  <<"rounded to a whole number of hours : ";
  cin >> hours;
  if (hours > 40) 
    grossPay = rate * 40 + 1.5*rate*(hours -40);
  else
    grossPay = rate * hours;

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);
  cout << "Hours =" << hours <<endl;
  cout << "Hourly pay rate = $" << rate << endl;
  cout << "Gross pay = $" << grossPay << endl;
  return 0;
}


While

#include <iostream>
using namespace std;

int main() {
  int countDown;
  cout << "How many greetings do you want?";
  cin >> countDown;
  while (countDown > 0) {
    cout <<"Hello ";
    countDown -=1;
  }
  cout << endl;
  cout << "That's all\n";
  return 0;
}


예제

#include <iostream>
using namespace std;

int main() {

  double balance;
  cin >> balance;
  int count = 0;

  cout << "This program tells you how long it takes\n"
    <<"to accumulate a debt of $100, starting with\n"
    <<"an initial balance of " << balance << " owed.\n"
    <<"The interest rate is 2% per month.\n";

  while (balance < 100.00){
    balance = balance + 0.02*balance;
    count++;
  }

  cout << "After " << count << " months,\n";
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);
  cout << "your balance due will be $" << balance << endl;
  
  return 0;

  }



Do while

#include <iostream>
using namespace std;

int main() {
  char ans;

  do {
    cout << "Hello\n";
    cout << "Do you want another greetings?\n"
      <<"Press y for yes, n for no,\n"
      <<"and then press return : ";
    cin >> ans;
  } 
    while (ans =='y' || ans =='Y'); 
    cout << "Good-Bye\n";

  return 0;

  }



댓글