Docsity

applied programming subject sheets, Exercises of Programming Languages

Toggle sidebar
bg1
Arab Academy for Science, Technology & Maritime Transport
College of Engineering and Technology
CC212 Applied Programming (Computer Staff)
Week 4 (Functions)
Class Work
1. a) Write a function that gets two numbers (as parameters) and returns their
maximum value.
b) Write a program that reads 5 numbers from the user and uses the above
function to find their maximum.
2. a) Write a function that gets a number (as parameter) and returns its double.
b) Write a program that reads two numbers from the user (x, y) and evaluates
the equation: 2 x + 4 y (Use the above function)
3. a) Write a function that gets two numbers (x, y - as parameters) and returns
the value of: x^y.
b) Write a program that reads 3 values from the used (a, b, c) and uses the
above function to evaluate the equation: (a + b) ^c.
4. Write a C function that calculates the factorial of a number.
5. Solve the following sequence:
Z=1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
Using the factorial function solved in problem 4.
Home Work
1. a) Write a function that gets a number (as a parameter) and returns โ€œ1โ€ if
the number is odd and โ€œ0โ€ if the number is even.
b) Write a program that uses the above function to count the number of even
values in an array of 10 elements.
2. a) Write a function that gets a number (as parameters) and prints all its
divisors (assume not more than 10).
pf2

Partial preview of the text

Download applied programming subject sheets and more Exercises Programming Languages in PDF only on Docsity!

Arab Academy for Science, Technology & Maritime Transport

College of Engineering and Technology

CC21 2 Applied Programming (Computer Staff)

Week 4 ( Functions )

Class Work

  1. a) Write a function that gets two numbers (as parameters) and returns their maximum value. b) Write a program that reads 5 numbers from the user and uses the above function to find their maximum.
  2. a) Write a function that gets a number (as parameter) and returns its double. b) Write a program that reads two numbers from the user (x, y) and evaluates the equation: 2 x + 4 y (Use the above function)
  3. a) Write a function that gets two numbers (x, y - as parameters) and returns the value of: x^y. b) Write a program that reads 3 values from the used (a, b, c) and uses the above function to evaluate the equation: (a + b) ^c.
  4. Write a C function that calculates the factorial of a number.
  5. Solve the following sequence: Z=1/1! + 2/2! + 3/3! + 4/4! + ... N/N! Using the factorial function solved in problem 4.

Home Work

  1. a) Write a function that gets a number (as a parameter) and returns โ€œ1โ€ if the number is odd and โ€œ0โ€ if the number is even. b) Write a program that uses the above function to count the number of even values in an array of 10 elements.
  2. a) Write a function that gets a number (as parameters) and prints all its divisors (assume not more than 10).
  1. a) Write a function that gets a number and returns its absolute value. b) Write a program that reads 2 arrays from the user (each contains 3 elements) and evaluates the sum of absolute differences of its elements. SAD = Sum (Absolute (ai โ€“ bi)) , SAD = โˆ‘ โ”‚ ai โ€“ bi โ”‚
  2. Write a C function that checks if a number is Prime. Return 1 if itโ€™s prime and 0 if not.
  3. Write a C Program to Check Whether a Number can be expressed as Sum of Two Prime Numbers. Hint : Use the check prime function done in problem 4. Sample Output: Enter a positive integer: 34 34 = 3 + 31 34 = 5 + 29 34 = 11 + 23 34 = 17 + 17
  4. Write a C function that checks if a 3 - digit number is Armstrong Number A number is Armstrong if sum of power of its digit is equal to the number. For example, 153 is an Armstrong number because 1 53 = 1^3 + 5^3 + 3^3. Test Data: 153 Expected Output: 153 is an Armstrong number.