C++程序设计基础——代码练习题及答案

本文介绍了C++编程的基础练习,包括判断闰年、比较数字、转换星期、求和、数字反转等算法,还涉及了枚举类型、函数调用、递归、面向对象等概念。此外,探讨了C++的存储类型、内联函数、重载函数以及系统函数的应用,并讲解了函数参数传递的值传递和引用传递的区别。
摘要由CSDN通过智能技术生成

文章目录

简单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 (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值