Monday, October 27, 2014

Find the GCD and LCM of Two Integers

Find the GCD and LCM of Two Integers

#include 
#include 
void main()
{
    int num1, num2, gcd, lcm, remainder, numerator, denominator;
    clrscr();
    printf("Enter two numbers\n");
    scanf("%d %d", &num1, &num2);
    if (num1 > num2)
    {
        numerator = num1;
        denominator = num2;
    }
    else
    {
        numerator = num2;
        denominator = num1;
    }
    remainder = num1 % num2;
    while (remainder != 0)
    {
        numerator   = denominator;
        denominator = remainder;
        remainder   = numerator % denominator;
    }
    gcd = denominator;
    lcm = num1 * num2 / gcd;
    printf("GCD of %d and %d = %d\n", num1, num2, gcd);
    printf("LCM of %d and %d = %d\n", num1, num2, lcm);         getch();
}

Output
 
Enter two numbers
30
40
GCD of 30 and 40 = 30
LCM of 30 and 40 = 40

Calculate the Value of sin(x)

#include 
#include 
#include 
#include 
 
void main()
{
    int n, x1;
    float accuracy, term, denominator, x, sinx, sinval;
    clrscr();
    printf("Enter the value of x (in degrees) \n");
    scanf("%f", &x);
    x1 = x;
    x = x * (3.142 / 180.0);
    sinval = sin(x);
    printf("Enter the accuracy for the result \n");
    scanf("%f", &accuracy);
    term = x;
    sinx = term;
    n = 1;
    do
    {
        denominator = 2 * n * (2 * n + 1);
        term = -term * x * x / denominator;
        sinx = sinx + term;
        n = n + 1;
    } while (accuracy <= fabs(sinval - sinx));
    printf("Sum of the sine series = %f \n", sinx);
    printf("Using Library function sin(%d) = %f\n", x1, sin(x));
    getch();
}

Output
 
Enter the value of x (in degrees)
60
Enter the accuracy for the result
0.86602540378443864676372317075294
Sum of the sine series         = 0.855862
Using Library function sin(60) = 0.866093
 
Enter the value of x (in degrees)
45
Enter the accuracy for the result
0.70710678118654752440084436210485
Sum of the sine series         = 0.704723
Using Library function sin(45) = 0.707179

 

Calculate the Value of cos(x)

#include 
#include 
#include 
#include 
 
void main()
{
    int n, x1;
    float accuracy, term, denominator, x, cosx, cosval;
    clrscr();
    printf("Enter the value of x (in degrees) \n");
    scanf("%f", &x);
    x1 = x;
    x = x * (3.142 / 180.0);
    cosval = cos(x);
    printf("Enter the accuracy for the result \n");
    scanf("%f", &accuracy);
    term = 1;
    cosx = term;
    n = 1;
    do
    {
        denominator = 2 * n * (2 * n - 1);
        term = -term * x * x / denominator;
        cosx = cosx + term;
        n = n + 1;
    } while (accuracy <= fabs(cosval - cosx));
    printf("Sum of the cosine series = %f\n", cosx);
    printf("Using Library function cos(%d) = %f\n", x1, cos(x)); 
    getch();
}

Output
 
Enter the value of x (in degrees)
60
Enter the accuracy for the result
0.86602
Sum of the cosine series       = 0.451546
Using Library function cos(60) = 0.499882
 
Enter the value of x (in degrees)
45
Enter the accuracy for the result
0.7071
Sum of the cosine series       = 0.691495
Using Library function cos(45) = 0.707035
Check if a given Number is Prime number
#include 
#include 
#include 
 
void main()
{
    int num, j, flag;
    clrscr();
    printf("Enter a number \n");
    scanf("%d", &num);
 
    if (num <= 1)
    {
        printf("%d is not a prime numbers \n", num);
        exit(1);
    }
    flag = 0;
    for (j = 2; j <= num / 2; j++)
    {
        if ((num % j) == 0)
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("%d is a prime number \n", num);
     else
        printf("%d is not a prime number \n", num);
        getch();
}

Output
 
Enter a number
23
23 is a prime number
 
Enter a number
15
15 is not a prime number
Find Prime Numbers in a given Range
#include 
#include 
#include 
void main()
{
    int num1, num2, i, j, flag, temp, count = 0;
    clrscr();
    printf("Enter the value of num1 and num2 \n");
    scanf("%d %d", &num1, &num2);
    if (num2 < 2)
    {
        printf("There are no primes upto %d\n", num2);
        exit(0);
    }
    printf("Prime numbers are \n");
    temp = num1;
    if ( num1 % 2 == 0)
    {
        num1++;
    }
    for (i = num1; i <= num2; i = i + 2)
    {
        flag = 0;
        for (j = 2; j <= i / 2; j++)
        {
            if ((i % j) == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            printf("%d\n", i);
            count++;
        }
    }
    printf("Number of primes between %d & %d = %d\n", temp, num2, count); 
getch();
}

Output
 
Enter the value of num1 and num2
70 85
Prime numbers are
71
73
79
83

Number of primes between 70 and 85 = 4

No comments:

Post a Comment