In this article, we are gonna to discuss questions to expert Functions
Before doing the tutorial 12, watch this videos to get an idea about Functions, user-defined functions and types
Here you can find the tutorial 12 questions pdf to practice Arrays ;
https://www.thecn.com/uploads/64779d/64779d76eef4888a55091e98/s3/NjMzNWEyM2JhOGE1NWUxZjk5NWIzNTcxLzY0Nzc5ZDcyOTQ3OTQ5NGZkMzA5MGEwYy9JQ1QxNDAyX1R1dG9yaWFsXzEyLnBkZg/34fa53f23af0a6c698eb9c2abecc4bbd.pdf
1.Type in and run the all programs presented in lecture note 12. Briefly describe the theory/
concept you learned from these programs separately.
- Describing the C functions.
- Practicing the declaration, parameter passing, prototyping, and calling functions.
- Justifying parameter passing methods.
- Practicing creating user-defined C header files.
- Justifying the variable types and scope rules.
- Describing the concept of recursion.
- Applying taught concepts for writing programs.
2. Display all prime numbers between two Intervals using a function.
Answer
#include <stdio.h>
int checkPrimeNumber(int number){
int i, f = 1;
for (i = 2; i <= number / 2; ++i)
{
if (number % i == 0)
{
f = 0;
break;
}
}
return f;
}
int main()
{
int num1, num2, j, f;
printf("Enter the start number:");
scanf("%d",&num1);
printf("Enter the end number:");
scanf("%d",&num2);
printf("Prime numbers between %d and %d are: ",
num1, num2);
for (j = num1; j < num2; ++j)
{
f = checkPrimeNumber(j);
if (f == 1)
{
printf("%d ", j);
}
}
return 0;
}
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;
}
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;
}
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>
float trip_cost(float distance);
float trip_cost(float distance) {
float trip_fees;
trip_fees = 35 * distance;
return trip_fees;
}
int main() {
float distance, trip_fees;
printf("Enter the distance in km: ");
scanf("%f", &distance);
trip_fees = trip_cost(distance);
printf("Trip cost is: %.2f\n", trip_fees);
return 0;
}
6.Write a function to convert the LKR currency into US dollars.
Answer
#include <stdio.h>
float us_dollars(float lkr);
float us_dollars(float lkr) {
float us_value;
us_value = lkr / 312.94;
return us_value;
}
int main() {
float lkr, us_value;
printf("Enter the LKR value: ");
scanf("%f", &lkr);
us_value = us_dollars(lkr);
printf("Value in US dollars: %.2f\n", us_value);
return 0;
}
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>
double calculateTicketPrice(char source[], char destination[])
{
double distance = 0.0;
double ticketPrice = distance * 100.0;
return ticketPrice;
}
void printTicket()
{
char source[100], destination[100];
printf("Enter source: ");
scanf("%s", source);
printf("Enter destination: ");
scanf("%s", destination);
double ticketPrice = calculateTicketPrice(source, destination);
printf("\n********** Ticket **********\n");
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
printf("Ticket Price: %.2lf LKR\n", ticketPrice);
printf("*****************************\n\n");
}
int main() {
int numTickets;
printf("How many tickets would you like to purchase? ");
scanf("%d", &numTickets);
if (numTickets <= 0)
{
printf("Invalid number of tickets.\n");
}
else
{
for (int i = 0; i < numTickets; i++)
{
printf("\n Ticket %d:\n", i + 1);
printTicket();
}
}
return 0;
}
8.Write a program for both main line and internal process of the flowcharts
Answer
#include<stdio.h>
void leaderEarnings();
int main(void){
int i=0;
while(i<20)
{
leaderEarnings();
i++;
}
return 0;
}
void leaderEarnings(){
char empId[10];
int NoOfItemsSold;
float unitPrice, leaderEarning=0, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold;
commission=leaderEarning*0.5;
printf("\n%s is earn %.2f\n\n", empId, commission);
return;
}
9.Modify and re-write the program written in Question 8 to print the total amount paid as the weekly sales commission for all sales leaders.
Answer
#include<stdio.h>
double leaderEarnings();
int main(void){
int i=0;
float totalCommission=0;
while(i<20){
totalCommission+=leaderEarnings();
i++;
}
printf("Total Commission of all sales leaders %.2f\n", totalCommission);
return 0;
}
double leaderEarnings(){
char empId[10];
int NoOfItemsSold;
float unitPrice, leaderEarning=0, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold;
commission=leaderEarning*0.5;
printf("\n%s is earn %.2f\n\n", empId, commission);
return commission;
}
10.Suppose that the management of this organization has decided to allow sales leaders to sell more than one different item as they wish. Modify and re-write the program written in Question 9 to satisfy this requirement. Note: 50% of total sales earnings is paid as the sales commission for a sales leader.
Answer
#include<stdio.h>
double leaderEarnings();
int main(void){
int i=0;
float totalCommission=0;
while(i<20){
totalCommission+=leaderEarnings();
i++;
}
printf("Total Commission of all sales leaders %.2f\n", totalCommission);
return 0;
}
double leaderEarnings(){
char empId[10];
int NoOfItemsSold, i, n;
float unitPrice, leaderEarning, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("How many items does sale? ");
scanf("%i", &n);
for(i=0;i<n;++i){
leaderEarning=0;
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold*0.5;
commission+=leaderEarning;
}
printf("\n%s is earn %.2f\n\n", empId, commission);
return commission;
}
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 calculateElectricityBill(int units);
float calculateElectricityBill(int units)
{
float bill = 0.0;
if (units <= 50)
bill=0.5*units;
else if (units<=150)
bill=0.5*50+(units-50)*0.75;
else if (units<=250)
bill=0.5*50+0.75*100+(units-150)*1.20;
else{
bill=0.5*50+0.75*100+1.20*100+(units-250)*1.50;
bill += units * 1.50;
}
return bill;
}
int main()
{
int units;
float bill;
printf("Enter the number of units: ");
scanf("%d", &units);
bill = calculateElectricityBill(units);
printf("Total electricity bill: Rs. %.2f\n", bill);
return 0;
}