Monday, October 27, 2014

Find Power of a Number using Recursion
#include 
# include 
long power (int, int);
 
int main()
{
    int pow, num;
    long result;
    clrscr();
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Enter it's power: ");
    scanf("%d", &pow);
    result = power(num, pow);
    printf("%d^%d is %ld", num, pow, result);
    return 0;
}
 
long power (int num, int pow)
{
    if (pow)
    {
        return (num * power(num, pow - 1));
    }
    return 1;
}

Output
 
Enter a number: 456
Enter it's power: 3
456^3 is 94818816
Find the Factorial of a Number using Recursion
#include 
#include  
int factorial(int);
 
int main()
{
    int num;
    int result;
    clrscr();
    printf("Enter a number to find it's Factorial: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Factorial of negative number not possible\n");
    }
    else
    {
        result = factorial(num);
        printf("The Factorial of %d is %d.\n", num, result);
    }
    return 0;
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));
    }
}

Output
 
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.
Find LCM of a Number using Recursion
#include 
# include  
int lcm(int, int);
int main()
{
    int a, b, result;
    int prime[100];
    clrscr();
    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);
    result = lcm(a, b);
    printf("The LCM of %d and %d is %d\n", a, b, result);
    return 0;
}
 int lcm(int a, int b)
{ 
    static int common = 1;
 
    if (common % a == 0 && common % b == 0)
    {
        return common;
    }
    common++;
    lcm(a, b);
    return common;
getch();
}
Output
 
Enter two numbers: 456
12
The LCM of 456 and 12 is 456
 
Enter two numbers: 45 75
The LCM of 45 and 75 is 225
Find GCD of given Numbers using Recursion
#include 
#include 
int gcd(int, int);
 
int main()
{
    int a, b, result;
    clrscr();
    printf("Enter the two numbers to find their GCD: ");
    scanf("%d%d", &a, &b);
    result = gcd(a, b);
    printf("The GCD of %d and %d is %d.\n", a, b, result);
}
 
int gcd(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return gcd(a - b, b);
        }
        else
        {
            return gcd(a, b - a);
        }
    }
    return a;
}
getch();
}

Output
 
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.
Find HCF of a given Number without using Recursion
1.  #include 
2.   
3.  int hcf(int, int);
4.   
5.  int main()
6.  {
7.      int a, b, result;
8.   
9.      printf("Enter the two numbers to find their HCF: ");
10.    scanf("%d%d", &a, &b);
11.    result = hcf(a, b);
12.    printf("The HCF of %d and %d is %d.\n", a, b, result);
13. 
14.    return 0;
15.}
16. 
17.int hcf(int a, int b)
18.{
19.    while (a != b)
20.    {
21.        if (a > b)
22.        {
23.            a = a - b;
24.        }
25.        else
26.        {
27.            b = b - a;
28.        }
29.    }
30.    return a;
31.}

Output
 
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.
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
Find the sum of series 1^2 + 2^2 + …. + n^2.
#include 
#include 
int main()
{
    int number, i;
    int sum = 0;
    clrscr();
    printf("Enter maximum values of series number: ");
    scanf("%d", &number);
    sum = (number * (number + 1) * (2 * number + 1 )) / 6;
    printf("Sum of the above given series : ");
    for (i = 1; i <= number; i++)
    {
        if (i != number)
            printf("%d^2 + ", i);
        else
            printf("%d^2 = %d ", i, sum);
    }
    return 0; 
    getch();
}

 
Output
 
Enter maximum values of series number: 4
Sum of the above given series : 1^2 + 2^2 + 3^2 + 4^2 = 30
Find the Sum of A.P Series
#include 
#include 
#include  
int main()
{
     int a, d, n, i, tn;
     int sum = 0;
     clrscr();
     printf("Enter the first term value of the A.P. series: ");
     scanf("%d", &a);
     printf("Enter the total numbers in the A.P. series: ");
     scanf("%d", &n);
     printf("Enter the common difference of A.P. series: ");
     scanf("%d", &d);
     sum = (n * (2 * a + (n - 1)* d ))/ 2;
     tn = a + (n - 1) * d;
     printf("Sum of the A.P series is: ");
     for (i = a; i <= tn; i = i + d )
     {
          if (i != tn)
               printf("%d + ", i);
          else
               printf("%d = %d ", i, sum);
     }
     return 0;
     getch();
}

Output
 
Enter the first term value of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 2

Sum of the A.P series is: 1 + 3 + 5 + 7 + 9 = 25

No comments:

Post a Comment