顯示具有 Lesson 標籤的文章。 顯示所有文章
顯示具有 Lesson 標籤的文章。 顯示所有文章

2021年3月30日 星期二

C++ Lesson 4 Assignment

 

學習目標:

1.      複習所有曾學習的C++指令,包括cin, cout, if..else, for loop及變量的設定

2.      應用以上的指令。

 

參考文件: 所需語法

 

Assignment 4-1

Calculate the area of a square

-          Let the user enter the length and then output the area of the square.

The output is as follow:



 

Assignment 4-2

Calculate the area of a circle

-          Let the user enter the radius and then output the area of the circle



 

Assignment 4-3

To judge whether a mark means passed or failed.

-          Let the user enter a mark. If the mark is larger than 60, output “passed. If not, output “failed”.





 

Assignment 4-4

To judge whether a number can be divided by 3.

-          Let the user enter a number, if the number can be divided by 3, output “ this number can be divided by 3. If not, output “this number cannot be divided by 3”.



Assignment 4-5

Enter a number and then output the same number of “*” in one line.



Assignment 4-6

Calculate the sum of all the numbers between two numbers.

-          Enter two number, output the sum of the all the numbers between these two numbers.


Assignment 4-7

Output a square made of “*”

-          Enter a number, and output a square with the same number of “* at each side.



Assignment 4-8

Output a right-angled triangle

-          Enter the base length of a right-angled triangle and then output the right-angled triangle



Assignment 4-9

Use loop function to output a 9x9 Table


Assignment 4-10

To judge a number whether it is a prime number

-          Enter a number and then judge whether it is a prime number.



Assignment 4-11

Output all the prime numbers between 1 to 100


Assignment 4-12
Let the user to enter a number, and then output all the prime numbers between 1 and the entered number.




2021年3月23日 星期二

C++ Lesson 3

 Learning Objectives

1.      Arithmetic expression

2.      Conditional statements

 

 

Exercise 3-1

#include <iostream>

using namespace std;

int main() {

  cout<<5+2;

  cout<<5-2; 

  cout<<5*2;

  cout<<5/2;

  cout<<5%2;

return 0;

}

 

<<Assignment 3-1>>



 

<<Assignment 3-2>>



 

Exercise 3-2

#include <iostream>

using namespace std;

int main() {

int a;

cout<<"enter a number smaller than 10:";

cin>>a;

if (a<=10){

    cout<<a<<" is smaller than 10";

}

return 0;

}

 

Exercise 3-3

#include <iostream>

using namespace std;

int main() {

int a;

cout<<"enter a number: ";

cin>>a;

if (a<=10){

    cout<<a<<" is smaller than 10";

}

else{

    cout<<a<<" is greater than 10";

}

return 0;

}

 

Exercise 3-4

#include <iostream>

using namespace std;

int main() {

int a;

cout<<"enter a number: ";

cin>>a;

if (a%3>0){

    cout<<a<<" cannot be divided by 3";

}

else{

    cout<<a<<" can be divided by 3";

}

 

return 0;

}

Exercise 3-5

#include <iostream>

using namespace std;

int main() {

int a;

cout<<"enter a number: ";

cin>>a;

if (a%3>0)

    cout<<a<<" cannot be divided by 3";

else

    cout<<a<<" can be divided by 3";

return 0;

}

 

<<Assignment 3-3>>

Enter a mark. If the mark is over 60, output “passed”. If the mark is below 60, output “failed”.




 

<<Assignment 3-4>>

Enter a number within 50, and distinguish whether it is a prime number(質數)

The output is as follow:



提示: 質數是除了1及自己之外,不能被其他數字整除的數字。

如何於C++產生亂數?

 如 何於 C++ 產生亂數 ?   以下是一個例子,輸出一個由 0-4 的亂數。留意,行號 2,3,8,9,10 及 11 是需要的。 備註 : 如要輸出 0-9 的話,把行 11 的 rand()%5 改成 rand()%10 便可以。   -----...