CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 5
Function Block of code to perform a specific task A large problem can be solved using different functions User defined functions can take different parameters and return a result, which can be used in the main function Example: pow(base, exponent)takes 2 parameters as input and returns a result. It is a built-in function from math.h, not user-defined.
Function Syntax returnType functionName(type1 argument1, { // Body of the function }
Function Calling
Practice Problem-1 Write a program that reads in an integer value for n and then sums the integers from n to 2*n if n is nonnegative, or from 2 * n to n if n is negative. Use function for the summation of series.
Practice Problem-2 Write a program that prints all the prime numbers from 2 to 1000. Use function to check whether a number is prime or not.
Practice Problem-3 Write a program that takes an integer n from the input and, If n is negative: make it positive, add 5 and print it. If n is divisible by 2: print the next 10 numbers. If n is divisible by 3: print the number of digits of n. Hint: you will need a loop. Use one function for each if case.
Practice Problem-4 Write a program that takes an integer n from the input and print if the number is a palindrome. Use one function.
Practice Problem-5 Write a program that takes an integer n from the input and print if the number is a palindrome. Use one function. Use recursion.