1. 编程求 1!+2!+....+n!,要求从键盘输入n 使用JAVA实现

用c语言编程实现求1!+2!+3!+。。。+n!的值,其中n值由键盘输入~

#include//64位机器最多只能取到n = 31,再大数据会溢出int main(){int n = 0;long long int result = 0, tmp = 1;printf("Please input n to calculate 1! + 2! + ...+ n!:
");scanf_s("%d", &n);if (n < 1){printf("Input wrong.
");return 0;}for (int i = 1; i <= n; ++i){tmp *= i;result += tmp;}printf("The result is %lld
", result);return 0;}

import java.util.Scanner;public class test{ public static void main(String[] args){ Scanner s=new Scanner(System.in); System.out.println("请输入n: "); int n=s.nextInt(); int den=0; double sum=0.0; for(int i=1;i<=n;i++){ den+=i; sum+=1.0/den; } System.out.println("The result is : "+sum); }}

import java.util.*;
public class temp2{
public static void main(String[] args){
int k=1;
long sum=0,s;
Scanner in=new Scanner(System.in); //使用Scanner类定义对象
System.out.println("请输入一个整数:");
k=in.nextInt();
for(int j=1;j<=k;j++){
s=1;
for(int i=1;i<=j;i++){s*=i;} //计算阶乘
sum=sum+s;
}
System.out.println("sum="+sum);
}
}

package com.zibet.xj.test;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Test t = new Test();
t.test1();
}

private void test1() {
System.out.println("请输入您要操作的n值(n>=1):");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n < 1) {
System.out.println("输入值过小于1请重新输入:");
test1();
}
double res = 0;
for (int i = 1; i <= n; i++) {
double d = 1;
for (int j = 1; j <= i; j++) {
d = d * j;
}
res = res + d;
}
String m = "1!";
for (int i = 2; i <= n; i++) {
m = m + "+" + i + "!";
}
m = m + "=";
if (n > 0)
System.out.println(m + "的结果是:" + res);
}
}

import java.util.Scanner;
public class T{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入n:");
int n=input.nextInt();
int m=0,p=1;
for(int i=1;i<n+1;i++)
{
m=m+p*i;
p=p*i;
}
System.out.println(m);
}
}
不懂可以问我

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网