Compute the Value of X ^ N
#include
#include
#include
long int power(int x, int n);
void main()
{
long int x, n, xpown;
clrscr();
printf("Enter the values of X and N \n");
scanf("%ld %ld", &x, &n);
xpown = power(x, n);
printf("X to the power N = %ld\n", xpown);
}
long int power(int x, int n)
{
if (n == 1)
return(x);
else if (n % 2 == 0)
return (pow(power(x, n/2), 2));
else
return (x * power(x, n - 1));
}
Output
Enter the values of X and N
2 5
X to the power N = 32
Print the Factorial of a given
Number
#include
#include
void main()
{
int i, fact = 1, num;
clrscr();
printf("Enter the number \n");
scanf("%d", &num);
if (num <= 0)
fact = 1;
else
{
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
}
printf("Factorial of %d = %5d\n", num, fact);
getch();
}
Output
Enter the number
10
Factorial of 10 = 3628800
Calculate the value of nCr
#include
#include
int fact(int z);
void main()
{
int n, r, ncr;
clrscr();
printf("\n Enter the value for N and R \n");
scanf("%d%d", &n, &r);
ncr = fact(n) / (fact(r) * fact(n - r));
printf("\n The value of ncr is: %d", ncr);
}
int fact(int z)
{
int f = 1, i;
if (z == 0)
{
return(f);
}
else
{
for (i = 1; i <= z; i++)
{
f = f * i;
}
}
return(f);
}
Output
Enter the value for N and R
5 2
The value of ncr is: 10
Find & Display
Multiplication table
#include
#include
int main()
{
int number, i = 1;
clrscr();
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
getch();
}
Output
Enter the Number:6
Multiplication table of 6:
--------------------------
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
No comments:
Post a Comment