0% found this document useful (0 votes)
9 views

Function

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Function

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a C program that does the following: (Call By Reference


(Arrays and Functions))
a) Declare a float array of size 5. Read the elements of the array from the
keyboard using a function named ReadFloats.
b) The program should include a function named printHistogram that
reads numbers from the array and maps the information in the form of
dollar signs—each number is printed, then a bar consisting of that many
dollar signs is printed beside the number. Ex: n = {2.5, 4.0, 1.5, 3.0, 2.0};

#include <stdio.h>
void printHistogram(float arr[], int
size) {

void ReadFloats(float arr[], int size) for (int i = 0; i < size; i++) {
{
printf("%.1f ", arr[i]);
for (int i = 0; i < size; i++) {
for (int j = 0; j < (int)arr[i]; j+
printf("Enter element %d: ", i + +) {
1);
2. printf("$");
scanf("%f", &arr[i]);
}
}
printf("\n");
}
}
}

int main() {
float arr[5];
ReadFloats(arr, 5);
printHistogram(arr, 5);
return 0;
}

Write a C program that computes and prints the nth Fibonacci number
using recursion functions. (Recursion Function)

The Fibonacci numbers are the numbers in the following


integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, 144, ……..
#include <stdio.h> int main() {
int n;
int fibonacci(int n) { printf("Enter the position of the Fibonacci number: ");
if (n <= 1) { scanf("%d", &n);
return n; printf("Fibonacci number at position %d is %d\n", n,
} else { fibonacci(n));

return fibonacci(n - 1) + return 0;


fibonacci(n - 2); }
}
}

3. Write a C program to create a unit converter using functions. The


program should convert between different units of measurement
(e.g., inches to centimeters, pounds to kilograms, etc.). (Call By
Value Function)

#include <stdio.h> float poundsToKilograms(float pounds)


{
return pounds * 0.453592;
float inchesToCentimeters(float
inches) { }
void calculator() {
int choice;
float value;
printf("Unit Converter:\n");
printf("1. Inches to Centimeters\n"); int main() {
printf("2. Pounds to Kilograms\n"); calculator();
printf("Enter your choice: "); return 0;
scanf("%d", &choice); }

if (choice == 1) {
printf("Enter value in inches: ");
scanf("%f", &value);
printf("%.2f inches is %.2f centimeters\
n", value, inchesToCentimeters(value));
} else if (choice == 2) {
printf("Enter value in pounds: ");
scanf("%f", &value);
printf("%.2f pounds is %.2f kilograms\
n", value, poundsToKilograms(value));
} else {
printf("Invalid choice.\n"); }}

You might also like