Tuesday, July 15, 2008

find the value of cos(x) using the series

/* Write a C program to find the value of cos(x) using the series *
* up to the given accuracy (without using user defined function) *
* Also print cos(x) using library function. */

#include stdio.h
#include conio.h
#include math.h
#include stdlib.h

void main()
{
int n, x1;
float acc, term, den, x, cosx=0, cosval;

clrscr();

printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);

x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);
cosval = cos(x);

printf("Enter the accuary for the result\n");
scanf("%f", &acc);
term = 1;
cosx = term;
n = 1;

do
{
den = 2*n*(2*n-1);
term = -term * x * x / den;
cosx = cosx + term;
n = n + 1;
} while(acc <= fabs(cosval - cosx)); printf("Sum of the cosine series = %f\n", cosx); printf("Using Library function cos(%d) = %f\n", x1,cos(x)); } /*End of main() */ /*------------------------------ Output Enter the value of x (in degrees) 30 Enter the accuary for the result 0.000001 Sum of the cosine series = 0.865991 Using Library function cos(30) = 0.865991 RUN 2 Enter the value of x (in degrees) 45 Enter the accuary for the result 0.0001 Sum of the cosine series = 0.707031 Using Library function cos(45) = 0.707035 ---------------------------------------------*/

This program is completly same as previous program. Same explanation. Insted we use the cosine taylor formula here


Hence we have the series



x2 x4 x6 x8


1 -
+ - + - ...


2! 4! 6! 8!



Notice that the series only contains even powers of x and even factorials. Even numbers can be represented by 2n. Also notice that this is an alternating series, hence the McLaurin series is




Find the value of sin(x) using the series

/* Write a C program to find the value of sin(x) using the series *
* up to the given accuracy (without using user defined function) *
* Also print sin(x) using library function. */

#include stdio.h
#include conio.h
#include math.h
#include stdlib.h

void main()
{
int n, x1;
float acc, term, den, x, sinx=0, sinval;

clrscr();

printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);

x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);
sinval = sin(x);

printf("Enter the accuary for the result\n");
scanf("%f", &acc);

term = x;
sinx = term;
n = 1;

do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;

} while(acc <= fabs(sinval - sinx));

printf("Sum of the sine series = %f\n", sinx);
printf("Using Library function sin(%d) = %f\n", x1,sin(x));

} /*End of main() */

/*------------------------------
Output
Enter the value of x (in degrees)
30
Enter the accuary for the result
0.000001
Sum of the sine series = 0.500059
Using Library function sin(30) = 0.500059

RUN 2
Enter the value of x (in degrees)
45
Enter the accuary for the result
0.0001
Sum of the sine series = 0.707215
Using Library function sin(45) = 0.707179
---------------------------------------------*/

This program uses the sine series formula. The absolute value is the number of terms used inorder to get the result. By increasing the number of times you are increasing the accuracy but at the cost of memory and execution time. So well balance has to considered in giving the absolute value.


You can increase the accuracy by exapanding the series with the odd powers of x. The program uses a varaible n to keep track of the number of terms in the fornula. When it meets the necessary limit, the value is printed. Another point to note is that first we are converting degrees into radinans using the formula x=(angle)*(PI / 180)


Check whether it is a palindrome

/* Write a C program to reverse a given integer number and check *
* whether it is a palindrome. Output the given numbers with suitable*
* message */

#include stdio.h
#include conio.h

void main()
{
int num, temp, digit, rev = 0;

clrscr();

printf("Enter an integer\n");
scanf("%d", &num);

temp = num; /* original number is stored at temp */

while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", rev);

if(temp == rev )
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
}
/*------------------------
Output
RUN 1
Enter an integer
12321
Given number is = 12321
Its reverse is = 12321
Number is a palindrome

RUN 2
Enter an integer
3456
Given number is = 3456
Its reverse is = 6543
Number is not a palindrome
-----------------------------------*/

The main point to note is the logic in the program. 121 can be written as 100+20+1.

that is 1*100+2*10+1. This principal is used. So first the number is saved on other variable. Then divided by 10. the remainders are added in the same above way to get the number. Now this number is compared to duplicate copy of original number to check its same or not. If its same we will print it as a palindrome or else the otherwise

Sunday, July 13, 2008

sum of odd and even numbers from 1 to N

/* Write a C program to find the sum of odd numbers and *
* sum of even numbers from 1 to N. Output the computed *
* sums on two different lines with suitable headings */

#include stdio.h
#include conio.h

void main()
{
int i, N, oddSum = 0, evenSum = 0;

clrscr();

printf("Enter the value of N\n");
scanf ("%d", &N);

for (i=1; i <=N; i++)
{
if (i % 2 == 0)
evenSum = evenSum + i;
else
oddSum = oddSum + i;
}

printf ("Sum of all odd numbers = %d\n", oddSum);
printf ("Sum of all even numbers = %d\n", evenSum);
}
/*-----------------------------
Output
RUN1

Enter the value of N
10
Sum of all odd numbers = 25
Sum of all even numbers = 30

RUN2
Enter the value of N
50
Sum of all odd numbers = 625
Sum of all even numbers = 650

------------------------------*/

This is a straight example. Uses the concepts of even and odd numbers and sum of n numbers together

find the GCD and LCM of two integers

/* Write a C program to find the GCD and LCM of two integers *
* output the results along with the given integers. Use Euclids' algorithm*/

#include stdio.h
#include conio.h

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);
} /* End of main() */
/*------------------------
Output
RUN 1
Enter two numbers
5
15
GCD of 5 and 15 = 5
LCM of 5 and 15 = 15
------------------------------*/

This uses a Euclid's GCD (and LCM) algorithm. The discription is found here.

http://www.geocities.com/SiliconValley/Garage/3323/aat/a_eucl.html. Its better explained there. There is nothing much to tell about syntax.

print first N FIBONACCI numbers

/*Write a C program to generate and print first N FIBONACCI numbers*/

#include stdio.h

void main()
{
int fib1=0, fib2=1, fib3, N, count=0;

printf("Enter the value of N\n");
scanf("%d", &N);

printf("First %d FIBONACCI numbers are ...\n", N);
printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2; /* fib1 and fib2 are already used */

while( count < N)
{
fib3 = fib1 + fib2;
count ++;
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
} /* End of main() */

/*--------------------------
Enter the value of N
10
First 5 FIBONACCI numbers are ...
0
1
1
2
3
5
8
13
21
34
-------------------------------*/

This program is to print the fibanocci series. 0 1 1 2 3 5 8 13

The points to note in this program are two variables are preinstianted as the logic of it says that the sum of previous two numbers is the present number. Here there is a good point to note. The COUNT value is initially assigned to 0. but later changed to 2 as after execution, we only need 8 values excluding the initial value instanciated

Find the sum of 'N' natural numbers

/* Write a C program to find the sum of 'N' natural numbers*/

#include stdio.h
#include conio.h

void main()
{
int i, N, sum = 0;

clrscr();

printf("Enter an integer number\n");
scanf ("%d", &N);

for (i=1; i <= N; i++)
{
sum = sum + i;
}

printf ("Sum of first %d natural numbers = %d\n", N, sum);
}

/*----------------------------------------
Output
RUN1

Enter an integer number
10
Sum of first 10 natural numbers = 55


RUN2

Enter an integer number
50
Sum of first 50 natural numbers = 1275
------------------------------------------*/

Another straight forward program which uses a for loop to increment the number. The sum value is pre instiantiated to zero and we use a for loop to increment it. The output is displayed. The other way to execute this program is using the fornula for addition of n natural numbers