KEMBAR78
7 Function | PDF | Teaching Methods & Materials
0% found this document useful (0 votes)
77 views5 pages

7 Function

The document lists 40 functions related to programming in C. It provides descriptions and examples of functions to demonstrate passing values between functions, calling functions multiple times, finding absolute values, comparing values, calculating maximums and more. Many functions involve mathematical operations on integers and floating-point numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views5 pages

7 Function

The document lists 40 functions related to programming in C. It provides descriptions and examples of functions to demonstrate passing values between functions, calling functions multiple times, finding absolute values, comparing values, calculating maximums and more. Many functions involve mathematical operations on integers and floating-point numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Function

1 1. Write a program to demonstrate sending and receiving values between functions.


1 2. Write a program to demonstrate that one function can be called multiple times.
2 3. Write a function which returns the absolute value of an integer that is passed as a
parameter.
2 4. Write a function which returns the smaller of its two parameters.
2 5. Write a function that returns the maximum of three floating-point numbers.
2 6. Write a function which prints square of given number.
2 7. Write a function that converts inches to centimetre.
2 8. Write a function that prints perimeter and area of right triangle.
2 9. Write a function which receives a float and an int from main(), finds the product of these
two and returns the product which is printed through main( ).
2 10. Write a function atoi(s) to convert string of digits to a number.
2 11. Write function distance that calculates the distance between two points (x1, y1) and (x2,
y2). All numbers and return values should be of type double.
3 12. Any year is entered through the keyboard. Write a function to determine whether the year
is a leap year or not.
3 13. Write a function to test for a valid triangle.
3 14. Write a function that inputs a student’s average and returns 4 if a student's average is 90–
100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is 60–69, and 0 if
the average is lower than 60.
3 15. The area of triangle can be calculated by heron's formula, which defines the semi perimeter
s, as half of the sum of sides of the triangle. The area of triangle then given by the formula:
Area= √(s(s-a)(s-b)(s-c))
Where a, b and c are the sides of the triangle. Write a function which, when passed the side
of a triangle, return the area using Heron's formula. This function should first make sure that
triangle is valid; if it is not the function can return zero or negative value as an error
indicator.
3 16. Write a function to compute the distance between two points and use it to develop another
function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2),
and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point
(x, y) lines inside the triangle ABC, otherwise a value 0.
3 17. Define a function called hypotenuse that calculates the length of the hypotenuse of a right
triangle when the other two sides are given. Use this function in a program to determine the
length of the hypotenuse for each of the following triangles. The function should take two
arguments of type double and return the hypotenuse as a double. Test your program with
the side values specified below:
Triangle Side 1 Side 2

C Programs 1
Function
1 3.0 4.0
2 5.0 12.0
3 8.0 15.0
3 18. Write a function multiple that determines for a pair of integers whether the second integer
is a multiple of the first. The function should take two integer arguments and return 1 (true)
if the second is a multiple of the first and 0 (false) otherwise. Use this function in a program
that inputs a series of pairs of integers.
3 19. An application of function floor is rounding a value to the nearest integer. The statement y =
floor(x + .5); will round the number x to the nearest integer and assign the result to y. Write
a program that reads several numbers and uses the preceding statement to round each of
these numbers to the nearest integer. For each number processed, print both the original
number and the rounded number.
4 20. Given three variables x, y, z write a function to circularly shift their values to right. In other
words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8
and x = 10. Call the function with variables a, b, c to circularly shift values.
4 21. Write a function that receives marks received by a student in 3 subjects using loop and
returns the average and percentage of these marks. Call this function from main () and print
the results in main().
4 22. Write a function that receives 5 integers using loop and returns the sum, average and
standard deviation of these numbers. Call this function from main() and print the results in
main().
4 23. Write a function to calculate the factorial value of any integer entered through the
keyboard.
4 24. Write a function power (a, b) to calculate the value of a raised to b.
4 25. Write a program to print the table of squares and cubes of 1 to 10.
4 26. Write a function which generates the random number.
4 27. Write a program that simulates coin tossing. For each toss of the coin the program should
print Heads or Tails. Let the program toss the coin 100 times, and count the number of
times each side of the coin appears. Print the results. The program should call a separate
function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the
program realistically simulates the coin tossing, then each side of the coin should appear
approximately half the time for a total of approximately 50 heads and 50 tails.]
4 28. A parking garage charges a $2.00 minimum fee to park for up to three hours and an
additional $0.50 per hour for each hour or part thereof over three hours. The maximum
charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24
hours at a time. Write a program that will calculate and print the parking charges for each of
three customers who parked their cars in this garage yesterday. You should enter the hours

C Programs 2
Function
parked for each customer. Your program should print the results in a neat tabular format,
and should calculate and print the total of yesterday's receipts. The program should use the
function calculateCharges to determine the charge for each customer. Your outputs should
appear in the following format:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50
4 29. Write a general-purpose function to convert any given year into its roman equivalent. The
roman equivalents of decimal numbers: 1 – i, 100- c, 5- v ,500- d, 10- x, 1000 – m ,50- l
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
4 30. The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Begins with the terms 0 and 1 and has the property that each succeeding term is the sum of
the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the n th
Fibonacci number.
4 31. Write a function to compute the greatest common divisor given by Euclid’s algorithm,
exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1 1980 – 1 * 1617 = 363
1617 / 363 = 4 1617 – 4 * 363 = 165
363 / 165 = 2 363 – 2 * 165 = 33
5 / 33 = 5 165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.
4 32. Write a function to check whether given number is prime or not.
4 33. A positive integer is entered through the keyboard. Write a function to obtain the prime
factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime
factors of 35 are 5 and 7.
4 34. Write a function to check whether given number is perfect or not.
4 35. Write a function which prints first digit of an integer.
4 36. A 5-digit positive integer is entered through the keyboard, write a function to calculate sum
of digits of the 5-digit number
4 37. Write a function which adds the two least significant digits of an integer.
4 38. Write a function that takes an integer value and returns the number with its digits reversed.
For example, given the number 7631, the function should return 1367.

C Programs 3
Function
4 39. A positive integer is entered through the keyboard, write a function to find the binary
equivalent of this number without using recursion
4 40. Implement the following integer functions:
a) Function Celsius returns the Celsius equivalent of a Fahrenheit temperature.
b) Function Fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.
c) Use these functions to write a program that prints charts showing the Fahrenheit
equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of
all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular
format that minimizes the number of lines of output while remaining readable.
4 41. Prepare a payroll earnings statement for the sales force at the arctic ice company. All
Arctic’s employees are on a straight commission basis of 12.5% of the sales. Each month
they also receive a bonus separately that varies depends on the profit for the month and
length of their service. The sales manager calculates the bonus separately and enters it with
sales person total sales for the month. Your program is also calculate the withholding taxes
and retirement for the month based on following rates:
1. federal withholding: 25%
2. state withholding: 10%
3. retirement plan: 8%
use the test data shown in following table:
Sales person Sales Bonus
1 53,500 425
2 41,300 300
3 56,800 350
4 36,200 175
4 42. Write a program that, given a beginning balance in your saving account, calculates the
balance at the end of 1 year. The interest is 5.3% compounded quarterly. Show the interest
earned and balance at the quarter. Present the data in tabular columns with appropriate
headings. Use the separate function to compute the interest and print the balance.
4 43. Write a function that displays a solid square of asterisks whose side is specified in integer
parameter side. For example, if side is 4 , the function displays:
****
****
****
****
4 44. Write a function to search a given number to an array.

C Programs 4
Function
4 45. Write a program to arrange array in ascending order using function.
4 46. Write a program to reverse a string with the use of function.
4 47. Write a function stringindex(s, t) to find index of string t into string s. If s=” i am using unix”
and t=am then function returns 3.
4 48. Write a function strend(s, t) to find whether small string occurs at the end of large string s.
4 49. Write a function that prints your name as shown below. Write a call as it would be coded in
a calling function, such as main.
******************************
* your name here *
* *
******************************
Recursion
5 50. Write a program to find factorial using recursion.
5 51. Write a program to find power using recursion.
5 52. Write a program to generate Fibonacci numbers using recursive function.
5 53. Write a program to print string in reverse using recursive function.
5 54. Write a recursive function to find GCD.

C Programs 5

You might also like