Unit III - Program
Unit III - Program
#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
For example,
Let the value of x be 30.
OUTPUT:
Enter the value for x: 10
Enter the value for n: 2
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();
}