0% found this document useful (0 votes)
9 views

Unit III - Program

Uploaded by

jankar123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit III - Program

Uploaded by

jankar123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

//SWAP THE VALUES OF TWO VARIABLES

#include<stdio.h>
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);
int main()
{
int a=1, b=2, c=3, d=4;
printf(“\n In Main(), a=%d and b=%d”, a, b);
swap_call_by_val(a, b);
printf(“\n In Main(), a=%d and b=%d”, a, b);
printf(“\n In Main(), c=%d and d=%d”, c, d);
swap_call_by_ref(&c, &d); //address of the variable is passed
printf(“\n In Main(), c=%d and d=%d”, c, d);
}
void swap_call_by_val(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(“\n In Function Call By Value a=%d and b=%d”, a, b);
}
void swap_call_by_ref(int *c, int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;
printf(“\n In Function Call By Reference c=%d and d=%d”, c, d);
}

OUTPUT:
In Main(), a=1 and b=2
In Function Call By Value a=2 and b=1
In Main(), a=1 and b=2
In Main(), c=3 and d=4
In Function Call By Reference c=4 and d=3
In Main(), c=4 and d=3
//FIBONACCI SERIES USING RECURSIVE FUNCTION
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
printf(“Enter how many numbers to calculate:”)
scanf("%d",&n);
printf("Fibonacci series\n");
for (c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int a)
{
if ( a == 0 )
return 0;
else if ( a == 1 )
return 1;
else
return ( Fibonacci(a-1) + Fibonacci(a-2) );
}
//COMPUTATION OF SINE SERIES
Sine Series:
Sine Series is a series which is used to find the value of Sin(x).
Where, x is the angle in degree which is converted to Radian.
The formula used to express the Sin(x) as Sine Series is

Expanding the above notation, the formula of Sine Series is

For example,
Let the value of x be 30.

So, Radian value for 30 degree is 0.52359.

So, the value of Sin (30) is 0.5.


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum, t;
clrscr();
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
getch();
}

OUTPUT:
Enter the value for x: 10
Enter the value for n: 2

The value of sin(0.174533) = 0.1736


//SCIENTIFIC CALCULATOR USING BUILD IN FUNCTIONS
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
float sine(float x);
float cosine(float x);
float tangent(float x);
float sineh(float x);
float cosineh(float x);
float tangenth(float x);
float logten(float x);
float squareroot(float x);
float exponent(float x);
float power(float x,float y);
int main()
{
int n;
float x,y,answer;
printf("What do you want to do?\n");
printf("1.sin 2.cos 3. tan 4. sinh 5.cosh 6.tanh 7.1og10 8. square root. 9.exponent
10.power.");
scanf ("%d",&n);
if (n<9 && n>0)
{
printf("\n What is x? ");
scanf("%f",&x);
switch (n)
{
case 1:
answer = sine(x);
break;
case 2:
answer = cosine(x);
break;
case 3:
answer = tangent(x);
break;
case 4:
answer = sineh(x);
break;
case 5:
answer = cosineh(x);
break;
case 6:
answer = tangenth(x);
break;
case 7:
answer = logten(x);
break;
case 8:
answer = squareroot(x);
break;
case 9:
answer = exponent(x);
break;
}
}
if (n==10)
{
printf("What is x and y?\n");
scanf("%f%f",&x,&y);
answer = power(x,y);
}
if (n>0 && n<11)
printf("%f",answer);
else
printf("Wrong input.\n");
return 0;
}
float sine(float x)
{
return (sin (x*PI/180));
}
float cosine(float x)
{
return (cos (x*PI/180));
}
float tangent(float x)
{
return (tan(x*PI/180));
}
float sineh(float x)
{
return (sinh(x));
}
float cosineh(float x)
{
return (sinh(x));
}
float tangenth(float x)
{
return (sinh(x));
}
float logten(float x)
{
return (log10(x));
}
float squareroot(float x)
{
return (sqrt(x));
}
float exponent(float x)
{
return(exp(x));
}
float power(float x, float y)
{
return (pow(x,y));
}
//BINARY SEARCH USING RECURSIVE
#include <stdio.h>
int found=0;
int main()
{
int arr[10], i, n, num, beg, end, pos;
printf(“Enter the size of an array:”);
scanf(“%d”, &n);
printf(“Enter the Elements of the array in ascending order:”);
for(i=0;i<n;i++)
{
scanf(“%d”, &arr[i]);
}
printf(“Enter the number to be searched”);
scanf(“%d”, &num);
beg=0;
end=n-1;

pos=bsearch(arr, n, num, beg, end);


if(found==0)
{
printf(“Number not Found”);
}

else
{
printf(“Number is found at position: %d”, pos+1);
}

return 0;
}
int bsearch(int arr[], int n, int num, int beg, int end)
{
int mid, pos=0;
if(beg<=end)
{
mid=(beg+end)/2;
if(num==arr[mid])
{
found=1;
pos=mid;
}

elseif(num<arr[mid])
{
return bsearch(arr, n, num, beg, mid-1);
}

else
{
return bsearch(arr, n, num, mid+1, end);
}
else
{
if(found==1)
return pos;
}
}
}

//SORTING OF NAMES
#include<stdio.h>
#include<string.h>
void main()
{
char names[10][15], temp[25];
int i, j, n;
clrscr();
printf(“Enter the Number of Names:”);
scanf(“%d”, &n);
printf(“\nEnter the Names:”);
for(i=0;i<n;i++)
Scanf(“%s”, names[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
If(strcmp(names[i], names[j])>0)
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf(“Names in the sorted order:\n”);
for(i=0;i<n;i++)
printf(“\n%s”,names[i]);
getch();
}

You might also like