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及自己之外,不能被其他數字整除的數字。





沒有留言:
張貼留言