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 便可以。   -----...