简单C++程序
#include<iostream>
using namespace std;
int main() {
cout << "Hello!" << endl;
cout << "Welcome to C++!" << endl;
return 0;
}
输入一个年份,判断是否是闰年
#include<iostream>
using namespace std;
int main() {
int year;
bool isLeapYear;
cout << "输入一个年份:" << endl;
cin >> year;
isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
if (isLeapYear)
cout << year << " is a leap year" << endl;
else
{
cout << year << " is not a leap year" << endl;
}
return 0;
}
比较两个数的大小
#include<iostream>
using namespace std;
int main() {
int x, y;
cout << "Enter x and y:";
cin >> x >> y;
if (x != y)
if (x > y)
cout << "x>y" << endl;
else
{
cout << "x<y" << endl;
}
else
{
cout << "x=y" << endl;
}
return 0;
}
输入一个0~6的整数,转换成星期输出
#include<iostream>
using namespace std;
int main() {
int day;
cin >> day;
switch (day)
{
case 0:
cout << "Sunday" << endl;
break;
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
default:
cout << "Day out of range Sunday...Saturday" << endl;
break;
}
return 0;
}
求1~10的之和
#include<iostream>
using namespace std;
int main() {
int i = 1,sum = 0;
while (i <= 10) {
sum += i;
i++;
}
cout << "sum=" << sum << endl;
return 0;
}
输入一个整数,将各位数字反转后输出
#include<iostream>
using namespace std;
int main() {
int n, right_digit, newnum = 0;
cout << "Enter the number:";
cin >> n;
cout << "The number in reverse order is ";
do {
right_digit = n % 10;
cout << right_digit;
n /= 10;
} while (n != 0);
cout << endl;
return 0;
}
用do…while语句编程,求自然数1~10之和
#include<iostream>
using namespace std;
int main() {
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i < 10);
cout << "sum=" << sum << endl;
return 0;
}
输入一个整数,求出它的所有因子
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter a postive integer:";
cin >> n;
cout << "Number " << n << " Factors ";
for (int k = 1; k <= n; k++) {
if (n % k == 0) {
cout << k << " ";
}
}
cout << endl;
return 0;
}
编写输出图案

#include<iostream>
using namespace std;
int main() {
const int N = 4;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= 30; j++) {
cout << ' ' ;
}
for (int j = 1; j <= 8 - 2 * i; j++) {
cout << ' ' ;
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << '*';
}
cout << endl;
}
for (int i = 1; i <= N - 1; i++) {
for (int j = 1; j <= 30; j++) {
cout << ' ';
}
for (int j = 1; j <= 7 - 2 * i; j++) {
cout << '*';
}
cout << endl;
}
return 0;
}
求100~200之间不能被3整除的数
#include<iostream>
using namespace std;
int main() {
for (int n = 100; n <= 200; n++) {
if (n % 3 != 0) {
cout << n << endl;
}
}
return 0;
}
读入一系列整数,统计出正整数个数i和负整数个数j,读入0则结束
#include<iostream>
using namespace std;
int main() {
int i = 0, j = 0, n;
cout << "Enter some integers please (enter 0 to quit):" << endl;
cin >> n;
while (n != 0) {
if (n > 0) {
i += 1;
}
if (n < 0) {
j += 1;
}
cin >> n;
}
cout << "Count of positive integers:" << i << endl;
cout << "Count of negative integers:" << j << endl;
return 0;
}
设某次体育比赛只有4种可能,所以可以声明一个枚举类型,用一个枚举类型的变量来存放比赛比赛结果
#include<iostream>
using namespace std;
enum GameResult{WIN,LOSE,WIE,CANCEL};
int main() {
GameResult result;
enum GameResult omit = CANCEL;
for (int count = WIN; count <= CANCEL; count++) {
result = GameResult(count);
if (result == omit) {
cout << "The game was cancelled" << endl;
}
else {
cout << "The game was played";
if (result == WIN) {
cout << "and we won!";
}
if (result == LOSE) {
cout << "and we lost.";
}
cout << endl;
}
}
return 0;
}
|