You are on page 1of 15

Dasar Pemrograman

TA Gasal 2020

www.its.ac.id INSTITUT TEKNOLOGI SEPULUH NOPEMBER, Surabaya - Indonesia


03
Percabangan
Excessive Indentation
if...else statements vs. a series of if statements
1. double operand1, operand2;
2. char operator;
3. double result = 0.0;
4. printf( "%g %c %g = " , operand1 , operator , operand2 );
5. switch( operator ) {
6. case '+':
7. result = operand1 + operand2; break;
8. case '-':
9. result = operand1 - operand2; break;
10. case '*':
11. result = operand1 * operand2; break;
12. case '/':
13. if( operand2 == 0.0 )
14. printf( "*** ERROR *** division by %g is undefined.\n", operand2 );
15. else
16. result = operand1 / operand2;
17. break;
18. case '%':
19. result = (int) operand1 % (int) operand2;
20. break;
21. default:
22. printf( "*** ERROR *** unknown operator; operator must be + - * / or %%\n" );
23. break;
24. }
1. double operand1, operand2;
2. char operator;
3. double result = 0.0;
4. printf( "%g %c %g = " , operand1 , operator , operand2 );
5. if( operator == '+' )
6. result = operand1 + operand2;
7. else if( operator == '-' )
8. result = operand1 - operand2;
9. else if( operator == '*' )
10. result = operand1 * operand2;
11. else if( operator == '/'){
12. if( operand2 == 0.0 )
13. printf( "*** ERROR *** division by %g is undefined.\n", operand2 );
14. else result = operand1 / operand2;
15. } else if( operator == '%') result = (int) operand1 % (int) operand2;
16. else printf( "*** ERROR *** unknown operator; operator must be + - * / or %%\n" );
switch()… statement could not be
used
1. char* message;

2. if( degreesF > 100.0 ) message = "hot! Stay in the shade.";


3. else if( degreesF >= 80.0 ) message = "perfect weather for swimming.";
4. else if( degreesF >= 60.0 ) message = "very comfortable.";
5. else if( degreesF >= 40.0 ) message = "chilly.";
6. else if( degreesF >= 20.0 ) message = "freezing, but good skiing weather.";
7. else message= "way too cold to do much of anything!" ;

8. printf( "%g°F is %s\n" , degreesF , message );


PR-1
PR-2
1. A box of cookies can hold 24 cookies, and a container can hold 75 boxes of
cookies.
2. Write a program that prompts the user to enter the total number of cookies,
the number of cookies in a box, and the number of cookie boxes in a
container.
3. The program then outputs the number of boxes and the number of containers
to ship the cookies.
4. Note that each box must contain the specified number of cookies, and each
container must contain the specified number of boxes.
5. If the last box of cookies contains less than the number of specified cookies,
you can discard it and output the number of leftover cookies.
6. Similarly, if the last container contains less than the number of specified
boxes, you can discard it and output the number of leftover boxes.
PR-3
1. The cost of renting a room at a hotel is, say $100.00 per night.
2. For special occasions, such as a wedding or conference, the hotel offers a special discount
as follows:
i. If the number of rooms booked is at least 10, the discount is 10%; at least 20, the
discount is 20%; and at least 30, the discount is 30%.
ii. Also if rooms are booked for at least three days, then there is an additional 5%
discount.
3. Write a program that prompts the user to enter the cost of renting one room, the number of
rooms booked, the number of days the rooms are booked, and the sales tax (as a percent).
4. The program outputs the cost of renting one room, the discount on each room as a percent,
the number of rooms booked, the number of days the rooms are booked, the total cost of
the rooms, the sales tax, and the total billing amount.
04
Perulangan
1. int i = 0;
2. while (i <= 20){
3. cout << i << " ";
4. i = i + 5;
5. }

1. int calBurnedInADay, calBurnedInAWeek = 0;


2. int day = 1;
3. while (day <= 7){
4. cout << "Enter calories burned day " << day << ": ";
5. cin >> calBurnedInADay;
6. cout << endl;
7. calBurnedInAWeek = calBurnedInAWeek + calBurnedInADay;
8. day = day + 1;
9. }
10. cout << "Average number of calories burned each day: " << calBurnedInAWeek / 7 << endl;
1. int num, guess;
2. bool isGuessed;
3. srand(time(0));
4. num = rand() % 100;
5. isGuessed = false;
6. while (!isGuessed){
7. cout << "Enter an integer greater" << " than or equal to 0 and " << "less than 100: ";
8. cin >> guess;
9. cout << endl;
10. if (guess == num){
11. cout << "You guessed the correct " << "number." << endl;
12. isGuessed = true;
13. } else if (guess < num)
14. cout << "Your guess is lower than the " << "number.\n Guess again!" << endl;
15. else cout << "Your guess is higher than " << "the number.\n Guess again!" << endl;
16. }
1. for (i = 1; i <= 5; i++){
2. cout << "Hello!" << endl;
3. cout << "*" << endl;
4. }

1. for (i = 1; i <= 5; i++)


2. cout << "Hello!" << endl;
3. cout << "*" << endl;

1. for (i = 1; i <= 20; i = i + 2)


2. cout << " " << i;
3. cout << endl;
1. for (i = 1; ; i++)
2. cout << i << " ";
3. cout << endl;
When you borrow money to buy a house, a car, or for some other purpose,
you repay the loan by making periodic payments over a certain period of time.
Of course, the lending company will charge interest on the loan. PR-4
1. Every periodic payment consists of the interest on the loan and the payment toward the principal amount.
2. To be specific, suppose that you borrow $1,000 at an interest rate of 7.2% per year and the payments are
monthly.
3. Suppose that your monthly payment is $25. Now, the interest is 7.2% per year and the payments are monthly,
so the interest rate per month is 7.2/12 =0.6%.
4. The first month’s interest on $1,000 is 1000 X 0.006 = 6. Because the payment is $25 and the interest for the
first month is $6, the payment toward the principal amount is 25 - 6 = 19.
5. This means after making the first payment, the loan amount is 1,000 - 19 = 981. For the second payment, the
interest is calculated on $981. So the interest for the second month is 981 X 0.006 = 5.886, that is,
approximately $5.89.
6. This implies that the payment toward the principal is 25 - 5.89 =19.11 and the remaining balance after the
second payment is 981 - 19.11 = 961.89 . This process is repeated until the loan is paid.
7. Write a program that accepts as input the loan amount, the interest rate per year, and the monthly payment.
(Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2.)
8. The program then outputs the number of months it would take to repay the loan.
(Note that if the monthly payment is less than the first month’s interest, then after each payment, the loan amount
will increase. In this case, the program must warn the borrower that the monthly payment is too low, and with this
monthly payment, the loan amount could not be repaid.)

You might also like