1. 计算矩形面积

    读入两个整数,表示长和宽,输出面积。

#include<bits/stdc++.h>
using namespace std;

int main() {
	int a, b;    //定义两个 int整数类型 的变量 a 和 b 
	cin>>a>>b;   //读入a 和 b 
	cout<<a*b;   //输出面积 
	return 0;
}
  1. 计算球的体积

读入半径,输出体积。

#include<bits/stdc++.h>
using namespace std;

int main() {
	double r, pi=3.1415926535;    //定义 double双精度浮点数类型的变量 r 和 pi 
	cin>>r;   //读入r 
	double v;
	v = 4.0/3*pi*r*r*r;  //赋值语句计算体积  
	                     //要写4.0/3,不能写4/3 因为4/3的结果是1. 
	cout<<fixed<<setprecision(6)<<v;   //输出体积 保留小数点后6位 
	return 0;
}
  1. 判断奇数还是偶数

读入一个整数,判断它的奇偶

#include<bits/stdc++.h>
using namespace std;

int main() {
	int n; 
	cin>>n;
	if (n%2==0) {  //n%2计算n除以2的余数
    //n%2==0 判断它是否和0相等
		cout<<n<<"是偶数";
	} else {   //else 表示否则
		cout<<n<<"是奇数";
	}
	return 0;
}
  1. 输出1~~100,这100个数。

第一种方法

	cout<<1<<endl;
	cout<<2<<endl;
	cout<<3<<endl;
	cout<<4<<endl;
	cout<<5<<endl;

第二种方法:想办法通过复制粘贴同样的代码, 输出。 因为每次输出的数,都会增加1,输出一个变量,让变量每次增加1。

int n=1;
cout<<n<<endl; n=n+1;
cout<<n<<endl; n=n+1;
cout<<n<<endl; n=n+1;
cout<<n<<endl; n=n+1;
cout<<n<<endl; n=n+1;
cout<<n<<endl; n=n+1;
...

方法三: while语句 当条件成立时重复执行相同的代码

#include<bits/stdc++.h>
using namespace std;

int main() {
	int n=1;
	while (n<=8) {
		cout<<n<<endl; 
		n=n+1;
	}
	return 0;
}

练习1: 输出1~100里的奇数

练习2: 输出1~100里的偶数

例子4. 简易乘法表

#include<bits/stdc++.h>
using namespace std;
int main() {
	int m=1;
	int n=1;
	while (n<=9) {
		cout<<m<<"*"<<n<<"="<<m*n<<endl; 
		n=n+1;
	}
	return 0;
}

练习3: 输出乘法表

练习4: 读入一个整数,判断是否是质数