1. Find the minimum and maximum of sequence of 10 numbers stored in array.
Answer
#include<stdio.h>
int main()
{
int array[10] = {5,7,20,45,66,74,22,58,41,47};
int min,max,i;
min = array[0]; /* Assigning a initial value for min */
max = array[0]; /* Assigning a initial value for max */
for( i = 0; i < 10 ; i++)
{
if (min > array[i])
{
min = array[i];
}
if (max < array[i])
{
max = array[i];
}
}
printf("Minimum value of array[] is %i\n", min);
printf("Maximum value of array[] is %i", max);
return 0;
}
2.Find the total and average of a sequence of 10 numbers stored in array.
Answer
#include<stdio.h>
int main()
{
int sequence[10] = {2,4,6,8,10,12,14,16,18,20};
float average = 0.0;
int i,total = 0;
for ( i = 0; i < 10 ; i++)
{
total += sequence[i];
}
average = total / 10.0;
printf (" Total is %i and the Average is %.2f ", total, average);
return 0;
}
3.Find sum of natural numbers using a recursive function
CODE
#include<stdio.h>
int sum(unsigned int);
int sum(unsigned int n)
{
if ( n == 0 )
{
return 0;
}
return n + sum(n - 1);
}
int main()
{
printf("Value of sum(n) is %i", sum(10));
return 0;
}
4. Calculate the power of a number using a recursive function.
CODE
#include<stdio.h>
int power(int,int);
int power(int a, int b)
{
if ( b == 0 )
{
return 1;
}
return a * power(a,b-1);
}
int main()
{
printf("Value of power() is %i ", power(5,2));
return 0;
}
5. Write a function to return the trip cost which calculated using the given distance in kilometers. Note: Take 35 LKR as travelling cost per kilometer.
CODE
#include<stdio.h>
double cost(double);
double cost( double km)
{
double total = 0, unit_cost = 35;
total = km * unit_cost;
return total;
}
int main()
{
printf("cost is %f ", cost(10));
return 0;
}
0 Comments