第二章 printf语句与判断结构

一、printf 输出格式

注意:使用printf 时最好添加头文件 #include 。

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	printf("Hello World!");
	return 0;
}

1.Int、float、double、char等类型的输出格式:

(1)Int:%d
(2)Float: %f, 默认保留6位小数
(3)Double: %lf, 默认保留6位小数
(4)Char: %c, 回车也是一个字符,用’\n’表示

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
   
int a=3;
float b = 3.12345678;
double c = 3.12345678;
char d = 'y';
printf("%d\n", a);
printf("%f\n", b);
printf("%lf\n", c);
printf("%c\n", d);
return 0;
}

2.所有输出的变量均可包含在一个字符串中:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
   
int a=3;
float b = 3.12345678;
double c = 3.12345678;
char d = 'y';
printf("int a = %d, float b = %f\n, double c = %lf, char d = %c\n",a,b,c,d);
return 0;
}

练习:输入一个字符,用这个字符输出一个菱形:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
   
char c;
cin >> c;
printf("  %c \n", c);
printf(" %c%c%c\n", c,c,c);
printf("%c%c%c%c\n", c,c,c,c);
printf(" %c%c%c\n", c,c,c);
printf("  %c \n", c);
return 0;
}

练习:输入一个整数,表示时间,单位是秒。输出一个字符串,用”时:分:秒”的形式表示这个时间。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
   
	int t;	
	cin>>t;	
	int hours = t / 3600;
	int minutes = t % 3600 / 60;
	int seconds = t % 60;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李老师信息奥赛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值