1. 26
Set 2 Q1 Program to calculate the standard deviation of an array of values using
function
Program
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float calsd(float array[],int num);
int main()
{
int i,n;
float *a;
printf("Enter number of element in the array ");
scanf("%d", &n);
a= (float*) malloc(n*sizeof(float));
printf("Enter the elements in the arrayn ");
for(i=0; i < n; i++)
scanf("%f", &a[i]);
printf("nStandard Deviation = %.3f", calsd(a,n));
free (a);
return 0;
}
float calsd(float array[],int num)
{
float sum = 0.0, mean, stddev = 0.0;
int i;
for(i=0; i<num; i++)
{
sum =sum + array[i];
}
mean = sum/num;
2. 27
for(i=0; i<num; ++i)
{
stddev = stddev + pow(array[i] - mean, 2);
stddev = sqrt(stddev/num);
}
return stddev;
}
Output
Enter number of element in the array 10
Enter the elements in the array
2
3
4
1
5
6
9
8
7
5
Standard Deviation = 0.266
3. 28
Set 2 Q2 Program to find the factorial of a number using recursive function
Program
#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ldn",n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
Output
Enter a positive integer: 4
Factorial of 4 = 24
4. 29
Set 2 Q3 Recursive function to find the sum of odd numbers between a given
range
Program
#include <stdio.h>
int sumodd(int n,int m);
int main()
{
int n,m;
printf("Enter the start and end range ");
scanf("%d %d", &n,&m);
if (n%2==0)
n=n+1;
printf("Sum of odd numbers between %d and %d is %dn",n,m,
sumodd(n,m));
return 0;
}
int sumodd(int n, int m)
{
if (n<=m)
{
return n+sumodd(n+2,m);
}
else
return 0;
}
Output
Enter the start and end range 3
9
Sum of odd numbers between 3 and 9 is 24
5. 30
Set 2 Q4 Program to find the GCD of two numbers using recursive function
Program
#include <stdio.h>
int gcd(int a, int b);
int main()
{
int a, b, g;
printf("Enter the two numbers: ");
scanf("%d %d", &a, &b);
g = gcd(a, b);
printf("The GCD of %d and %d is %d n", a, b, g);
return 0;
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
Output
Enter the two numbers: 100
50
The GCD of 100 and 50 is 50
6. 31
Set 2 Q5 Program to reverse a text using recursive function
Program
#include <stdio.h>
#include <string.h>
void rev(char*, int, int);
int main()
{
char a[100];
printf("Enter the string: n");
gets(a);
rev(a, 0, strlen(a)-1);
printf("%sn",a);
return 0;
}
void rev(char *x, int start, int end)
{
char c;
if (start >= end)
return;
c = *(x+start);
*(x+start) = *(x+end);
*(x+end) = c;
rev(x, ++start, --end);
}
Output
Enter the string:
hello how are you
uoy era woh olleh
7. 32
Set 2 Q6 Program to print the nth
term of a Fibonacci series using recursive
function
Program
#include <stdio.h>
int fibonacci(int a);
int main()
{
int n;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &n);
if (n < 0)
printf("Fibonacci of negative number is not possiblen");
else
printf("The %d number in fibonacci series is %d n", n, fibonacci(n));
return 0;
}
int fibonacci(int num)
{
if (num == 0)
return 0;
else if (num == 1)
return 1;
else
return(fibonacci(num - 1) + fibonacci(num - 2));
}
Output
Enter the nth number in fibonacci series: 5
The 5 number in fibonacci series is 5