Write a program to compute the factorial of a number using recursion. INPUT AND OUTPUT FORMAT: Input consists of an integer. Refer sample input and output for formatting specifications. SAMPLE INPUT & OUTPUT 5 The factorial of 5 is 120
Answers
NAMASTE...❤️❤️
1. C++
2. An input format describes how to interpret the contents of an input field as a number or a string. ... Every variable has two output formats, called its print format and write format. Print formats are used in most output contexts; write formats are used only by WRITE (see WRITE).
3. 1. Program in C programming language
#include <stdio.h>
long power (int, int);
int main()
{
int pow, a;
long result;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("Enter the value of n : ");
scanf("%d", &pow);
result = power(a, pow);
printf("The value of %d power %d is %ld", a, pow, result);
return 0;
}
long power (int a, int pow)
{
if (pow)
{
return (a * power(a, pow - 1));
}
return 1;
}
2. Program in C++ programming language
#include <iostream>
using namespace std;
int calculatePower(int, int);
int main()
{
int a, powerRaised, result;
cout << "Enter the value of a : ";
cin >> a;
cout << "Enter the value of n : ";
cin >> powerRaised;
result = calculatePower(a, powerRaised);
cout << "The value of " << a << " power " << powerRaised << " is " <<result;
return 0;
}
int calculatePower(int a, int powerRaised)
{
if (powerRaised != 0)
return (a*calculatePower(a, powerRaised-1));
else
return 1;
}
3. Program in Java programming language
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of a : ");
int a=sc.nextInt();
System.out.println("Enter the value of n : ");
int exponent=sc.nextInt();
System.out.println("The value of " + a + " power " + exponent + " is " + power(a,exponent));
}
static int power(int b,int e)
{
if(e==0)
return 1;
else
return (b*power(b,e-1));
}
}
4. Program in Python programming language
def power(a,n):
if(n==1):
return(a)
if(n!=1):
return (a*power(a,n-1))
a=int(input("Enter the value of a : "))
n=int(input("Enter the value of n : "))
print("The value of" , a , "power" , n , "is" , power(a,n))
Note=> Indentation or Spacing is necessary in Python.
✌️✌️✍️✍️✌️✌️✍️✍️✌️✌️✍️✍️✌️✌️