💡Question 2
Display all prime numbers between two Intervals using a function.
Answer
#include <stdio.h>
int isPrime(int num) {
if (num <= 1)
return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return 0;
}
return 1;
}
void printPrimes(int start, int end) {
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int start, end;
printf("Enter the start of the interval: ");
scanf("%d", &start);
printf("Enter the end of the interval: ");
scanf("%d", &end);
printPrimes(start, end);
return 0;
}
💡Question 3
Find sum of natural numbers using a recursive function.
Answer
#include <stdio.h>
int sum_of_natural_numbers(int n) {
if (n == 0) {
return 0;
} else {
return n + sum_of_natural_numbers(n - 1);
}
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Please enter a positive integer.\n");
return 1;
}
printf("Sum of first %d natural numbers is: %d\n", num, sum_of_natural_numbers(num));
return 0;
}
💡Question 4
Calculate the power of a number using a recursive function.
Answer
#include <stdio.h>
double power(double base, int exponent) {
if (exponent == 0) {
return 1;
} else if (exponent > 0) {
return base * power(base, exponent - 1);
} else {
return 1 / power(base, -exponent);
}
}
int main() {
double base;
int exponent;
printf("Enter the base: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
printf("Result: %.2lf\n", power(base, exponent));
return 0;
}
💡Question 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.
Answer
#include <stdio.h>
double calculate_trip_cost(double distance) {
const double cost_per_km = 35.0;
return distance * cost_per_km;
}
int main() {
double distance;
printf("Enter the distance in kilometers: ");
scanf("%lf", &distance);
printf("Trip cost: %.2lf LKR\n", calculate_trip_cost(distance));
return 0;
}
💡Question 6
Write a function to convert the LKR currency into US dollars.
Answer
#include <stdio.h>
double lkr_to_usd(double lkr_amount, double exchange_rate) {
return lkr_amount / exchange_rate;
}
int main() {
double lkr_amount, exchange_rate;
printf("Enter the amount in LKR: ");
scanf("%lf", &lkr_amount);
printf("Enter the exchange rate (LKR to USD): ");
scanf("%lf", &exchange_rate);
printf("%.2lf LKR is equal to %.2lf USD\n", lkr_amount, lkr_to_usd(lkr_amount, exchange_rate));
return 0;
}
💡Question 7
Write a function to input source and destination and print the ticket for the route. Note: You can decide the payment calculator logic and passengers can ask more than one ticket at a time for different destinations.
Answer
#include <stdio.h>
#include <string.h>
typedef struct {
char source[20];
char destination[20];
float fare;
} Route;
float calculate_fare(char source[], char destination[]) {
Route routes[] = {
{"A", "B", 100.0},
{"A", "C", 200.0},
{"A", "D", 300.0},
{"B", "C", 150.0},
{"B", "D", 250.0},
{"C", "D", 200.0}
};
int num_routes = sizeof(routes) / sizeof(Route);
for (int i = 0; i < num_routes; ++i) {
if (strcmp(source, routes[i].source) == 0 && strcmp(destination, routes[i].destination) == 0) {
return routes[i].fare;
}
}
return -1;
}
void print_ticket(char source[], char destination[]) {
float fare = calculate_fare(source, destination);
if (fare == -1) {
printf("Invalid source or destination.\n");
} else {
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
printf("Fare: $%.2f\n", fare);
}
}
int main() {
int num_passengers;
printf("Enter the number of passengers: ");
scanf("%d", &num_passengers);
while (getchar() != '\n');
for (int i = 0; i < num_passengers; ++i) {
char source[20];
char destination[20];
printf("Enter source for passenger %d: ", i + 1);
fgets(source, sizeof(source), stdin);
source[strcspn(source, "\n")] = 0;
printf("Enter destination for passenger %d: ", i + 1);
fgets(destination, sizeof(destination), stdin);
destination[strcspn(destination, "\n")] = 0;
print_ticket(source, destination);
printf("\n");
}
return 0;
}
💡Question 11
Write a function to input electricity unit charges and calculate total electricity bill
according to the given condition:
a. For first 50 units Rs. 0.50/unit
b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
Answer
#include <stdio.h>
float calculate_bill(int units) {
float bill = 0;
if (units <= 50) {
bill = units * 0.50;
} else if (units <= 150) {
bill = 50 * 0.50 + (units - 50) * 0.75;
} else if (units <= 250) {
bill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;
} else {
bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;
}
return bill;
}
int main() {
int units;
printf("Enter the total electricity units consumed: ");
scanf("%d", &units);
if (units < 0) {
printf("Invalid input. Units consumed cannot be negative.\n");
return 1;
}
float total_bill = calculate_bill(units);
printf("Total electricity bill: Rs. %.2f\n", total_bill);
return 0;
}
Post a Comment