Tanti Technology

My photo
Bangalore, karnataka, India
Multi-platform UNIX systems consultant and administrator in mutualized and virtualized environments I have 4.5+ years experience in AIX system Administration field. This site will be helpful for system administrator in their day to day activities.Your comments on posts are welcome.This blog is all about IBM AIX Unix flavour. This blog will be used by System admins who will be using AIX in their work life. It can also be used for those newbies who want to get certifications in AIX Administration. This blog will be updated frequently to help the system admins and other new learners. DISCLAIMER: Please note that blog owner takes no responsibility of any kind for any type of data loss or damage by trying any of the command/method mentioned in this blog. You may use the commands/method/scripts on your own responsibility. If you find something useful, a comment would be appreciated to let other viewers also know that the solution/method work(ed) for you.

Monday, 4 July 2011

C Program-4

1. /* 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

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 ------------------------------*/ ***************************************************************** 2. /* 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
main()
{
int num, rev = 0, found = 0, temp, digit;
clrscr();
printf("Enter an integer\n");
scanf("%d", &num);

temp = num;
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
-----------------------------------*/

****************************************************************

3. /* 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
#include
#include

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

printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);
x1 = x;
/* 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 ---------------------------------------------*/ ******************************************************************************** 4. /* 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
#include
#include

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

printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);
x1 = x;
/* 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 ---------------------------------------------*/ **************************************************************************** 5. /* Write a C program to check whether a given number is prime or not * * and output the given number with suitable message */ #include
#include
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);
}
/*------------------------
Output
RUN 1
Enter a number
34
34 is not a prime number

RUN 2
Enter a number
29
29 is a prime number
-----------------------------*/

No comments:

Post a Comment