1. #include<stdio.h>
int PrimeNumber(int n);
int PrimeNumber(int n)
{
int j , x = 1;
for (j = 2; j <= n/2; j++)
{
if (n%j == 0)
{
x = 0;
break;
}
}
return x;
}
int main ()
{
int n1,n2,i,x;
printf("Enter the starting value :");
scanf("%d",&n1);
printf("Enter the ending value :");
scanf("%d",&n2);
printf("prime numbers between %d and %d :",n1,n2);
for (i = n1 + 1; i < n2; i++)
{
x = PrimeNumber(i);
if (x == 1)
{
printf("%3d ",i);
}
}
return 0;
}
2. #include<stdio.h>
int sum(int n);
int sum (int n)
{
if (n != 0)
{
return n + sum(n-1);
}
else
{
return n;
}
}
int main ()
{
int num;
printf("enter the positive number : ");
scanf("%i",&num);
printf("sum = %i",sum(num));
return 0;
}
3. #include<stdio.h>
#include<math.h>
float power(float n1 ,float n2);
float power(float n1,float n2)
{
if(n2 == 0)
return 1;
else
return n1 * power(n1,n2 - 1);
}
int main ()
{
float x;
float y;
printf("enter the number : ");
scanf("%f",&x);
printf("enter the exponent : ");
scanf("%f",&y);
printf("%.2f",power(x,y));
return 0;
}
4. #include <stdio.h>
float us_dollars(float lkr);
float us_dollars(float lkr)
{
float us_value;
printf("Enter the current Dollar value in lkr : ");
scanf("%f",&us_value);
return lkr / us_value;
}
int main()
{
float amount;
printf("Enter the LKR value: ");
scanf("%f", &amount);
printf("Value in US dollars: $%.2f\n", us_dollars(amount));
return 0;
}
5. #include <stdio.h>
float trip_cost(float km);
float trip_cost(float km)
{
return 35 * km;
}
int main()
{
float distance;
printf("Enter the distance in km: ");
scanf("%f", &distance);
printf("Trip cost is: %.2f\n", trip_cost(distance));
return 0;
}
0 Comments
Post a Comment