10 Function Examples
10 Function Examples
Examples with
Pseudocode,
Flowcharts, and
Code
#Pseudocode
1. Function `sum` takes two integers `a` and `b` as input.
2. Add `a` and `b`.
3. Return the result.
#Flowchart
1. Start
2. Input `a`, `b`
3. `result = a + b`
4. Return `result`
5. End
#Code
#include <stdio.h>
int main() {
int num1 = 5, num2 = 3;
printf("Sum: %d\n", sum(num1, num2));
return 0;
}
#Pseudocode
1. Function `factorial` takes an integer `n` as input.
2. If `n` is 0, return 1.
3. Initialize `result` to 1.
4. Loop from 1 to `n`, multiply `result` by loop counter.
5. Return `result`.
#Flowchart
1. Start
2. Input `n`
3. If `n == 0` return 1
4. Initialize `result = 1`
5. For `i` from 1 to `n`, `result = result * i`
6. Return `result`
7. End
#Code
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
int main() {
int number = 5;
printf("Factorial: %d\n", factorial(number));
return 0;
}
#Pseudocode
1. Function `isPrime` takes an integer `n` as input.
2. If `n <= 1`, return `false`.
3. Loop from 2 to `sqrt(n)`, if `n` is divisible by any number, return
`false`.
4. Return `true`.
#Flowchart
1. Start
2. Input `n`
3. If `n <= 1` return `false`
4. For `i` from 2 to `sqrt(n)`, if `n % i == 0` return `false`
5. Return `true`
6. End
#Code
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int number = 7;
if (isPrime(number))
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);
return 0;
}
#Pseudocode
1. Function `fibonacci` takes an integer `n` as input.
2. Initialize `a = 0`, `b = 1`.
3. If `n == 0`, return `a`.
4. If `n == 1`, return `b`.
5. Loop from 2 to `n`, update `a` and `b`.
6. Return `b`.
#Flowchart
1. Start
2. Input `n`
3. Initialize `a = 0`, `b = 1`
4. If `n == 0` return `a`
5. If `n == 1` return `b`
6. For `i` from 2 to `n`, `next = a + b`, `a = b`, `b = next`
7. Return `b`
8. End
#Code
#include <stdio.h>
int fibonacci(int n) {
int a = 0, b = 1, next;
if (n == 0)
return a;
if (n == 1)
return b;
for (int i = 2; i <= n; i++) {
next = a + b;
a = b;
b = next;
}
return b;
}
int main() {
int number = 10;
printf("Fibonacci: %d\n", fibonacci(number));
return 0;
}
#Pseudocode
1. Function `reverseString` takes a string `str` as input.
2. Initialize two pointers, `start` at the beginning and `end` at the end
of the string.
3. Swap the characters at `start` and `end`, move the pointers towards the
center.
4. Repeat until the pointers meet or cross.
####
#Flowchart
1. Start
2. Input `str`
3. Initialize `start = 0`, `end = strlen(str) - 1`
4. While `start < end`:
- Swap `str[start]` and `str[end]`
- Increment `start`
- Decrement `end`
5. Return `str`
6. End
#Code
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
#Pseudocode
1. Function `findMax` takes an array `arr` and its length `n` as input.
2. Initialize `max` to the first element of `arr`.
3. Loop through the array from the second element to the end.
4. If the current element is greater than `max`, update `max`.
5. Return `max`.
#Flowchart
1. Start
2. Input `arr` and `n`
3. Initialize `max = arr[0]`
4. For `i` from 1 to `n-1`:
- If `arr[i] > max`, update `max = arr[i]`
5. Return `max`
6. End
#Code
#include <stdio.h>
#Pseudocode
1. Function `isPalindrome` takes a string `str` as input.
2. Initialize two pointers, `start` at the beginning and `end` at the end
of the string.
3. While `start < end`:
- If `str[start]` is not equal to `str[end]`, return `false`.
- Increment `start`.
- Decrement `end`.
4. Return `true`.
#Flowchart
1. Start
2. Input `str`
3. Initialize `start = 0`, `end = strlen(str) - 1`
4. While `start < end`:
- If `str[start] != str[end]`, return `false`
- Increment `start`
- Decrement `end`
5. Return `true`
6. End
#Code
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#Pseudocode
1. Function `power` takes two integers `base` and `exp` as input.
2. Initialize `result` to 1.
3. Loop from 1 to `exp`.
4. Multiply `result` by `base` in each iteration.
5. Return `result`.
#Flowchart
1. Start
2. Input `base` and `exp`
3. Initialize `result = 1`
4. For `i` from 1 to `exp`, `result = result * base`
5. Return `result`
6. End
#Code
#include <stdio.h>
int main() {
int base = 2, exp = 3;
printf("Power: %d\n", power(base, exp));
return 0;
}
Example 9: Count Vowels in a String
#Pseudocode
1. Function `countVowels` takes a string `str` as input.
2. Initialize `count` to 0.
3. Loop through each character in the string.
4. If the character is a vowel (a, e, i, o, u), increment `count`.
5. Return `count`.
#Flowchart
1. Start
2. Input `str`
3. Initialize `count = 0`
4. For each character in `str`:
- If character is a vowel, increment `count`
5. Return `count`
6. End
#Code
#include <stdio.h>
int main() {
char str[] = "Hello World";
printf("Number of Vowels: %d\n", countVowels(str));
return 0;
}
Example 10: Find GCD of Two Numbers
#Pseudocode
1. Function `gcd` takes two integers `a` and `b` as input.
2. While `b` is not zero:
- Store `b` in `temp`.
- Update `b` to `a % b`.
- Update `a` to `temp`.
3. Return `a`.
#Flowchart
1. Start
2. Input `a` and `b`
3. While `b != 0`:
- `temp = b`
- `b = a % b`
- `a = temp`
4. Return `a`
5. End
#Code
#include <stdio.h>
int main() {
int num1 = 56, num2 = 98;
printf("GCD: %d\n", gcd(num1, num2));
return 0;
}