Write a C Program to compute 1/n!.
Problem statement:- Program to compute 1/n!
Data requirement:-
Input Data:- n
Output Data:-result
Additional Data :-i,fact
Program in C
Here is the source code of the C Program to compute 1/n!.
#include<stdio.h>//printf,scanf definition
int main()
{
int n,i,fact=1;
double result;
//Get the n value
printf("Enter the n value:");
scanf("%d",&n);
//Compute the n factorial
/*fact*=1
fact*=2
.
.
.
fact*=n
*/
for(i=1;i<=n;i++)
{
fact*=i;
}
//Compute the 1/n!
result=1.0/fact;
//Display the value of 1/n!
printf("1/n!=%lf\n",result);
}
Input/Output:int main()
{
int n,i,fact=1;
double result;
//Get the n value
printf("Enter the n value:");
scanf("%d",&n);
//Compute the n factorial
/*fact*=1
fact*=2
.
.
.
fact*=n
*/
for(i=1;i<=n;i++)
{
fact*=i;
}
//Compute the 1/n!
result=1.0/fact;
//Display the value of 1/n!
printf("1/n!=%lf\n",result);
}
Enter the n value:5
1/n!=0.008333
Write a C++ Program to compute 1/n!.
Program in C++Here is the source code of the C++ Program to compute 1/n!.
#include<iostream>
using namespace std;
int main()
{
int n,i,fact=1;
double result;
cout<<"Enter the n value:";
cin>>n;
for(i=1;i<=n;i++)
{
fact*=i;
}
result=1.0/fact;
cout<<"1/n!= "<<result;
}
Input/Output:using namespace std;
int main()
{
int n,i,fact=1;
double result;
cout<<"Enter the n value:";
cin>>n;
for(i=1;i<=n;i++)
{
fact*=i;
}
result=1.0/fact;
cout<<"1/n!= "<<result;
}
Enter the n value:7
1/n!= 0.000198413
Write a Java Program to compute 1/n!.
Program in JavaHere is the source code of the Java Program to compute 1/n!.
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i,fact=1;
double result;
System.out.println("Enter the n value:");
n=cs.nextInt();
for(i=1;i<=n;i++)
{
fact*=i;
}
result=1.0/fact;
System.out.println("1/n! = "+result);
cs.close();
}
}
Input/Output:public class P1 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i,fact=1;
double result;
System.out.println("Enter the n value:");
n=cs.nextInt();
for(i=1;i<=n;i++)
{
fact*=i;
}
result=1.0/fact;
System.out.println("1/n! = "+result);
cs.close();
}
}
Enter the n value:
4
1/n! = 0.041666666666666664
Write a Python Program to compute 1/n!.
Program in PythonHere is the source code of the Python Program to compute 1/n!.
n=int(input("Enter the n value:"))
fact=1
for i in range(1,n+1):
fact*=i
result=1.0/fact
print("1/n!= ",result)
Input/Output:fact=1
for i in range(1,n+1):
fact*=i
result=1.0/fact
print("1/n!= ",result)
Enter the n value:5
1/n!=0.008333
0 Comments
Please do not Enter any spam link in the comment box