例2-9 累乘

累乘

程序核心——循环语句

程序

#include<stdio.h>
int main()
{
	int i,n;
	double product;
	
	printf("Enter n:");
	scanf("%d",&n); 
	product=1;
	for(i=1;i<=n;i++)
	{
		product=product*i;
	}
	
	printf("product=%.0f
",product);//指定输出不用小数 
	return 0;
 } 

结果

Enter n:4
product=24

--------------------------------
Process exited after 2.507 seconds with return value 0
请按任意键继续. . 

分析

重点:printf("product=%.0f ",product)中%.0f指定输出不用小数,%.nf(n为常数)为指定保 留多少位小数,而若为%nf时固定至少有n位整数如不足n位用空格补齐

原文地址:https://www.cnblogs.com/5236288kai/p/10531575.html