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