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

10 Function Examples

The document provides examples of 10 C functions with pseudocode, flowcharts, and code implementations. The functions cover topics like sum of two numbers, factorial, checking prime numbers, Fibonacci sequence, reversing a string, finding the maximum element in an array, checking palindromes, calculating power, counting vowels in a string, and finding the greatest common divisor.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

10 Function Examples

The document provides examples of 10 C functions with pseudocode, flowcharts, and code implementations. The functions cover topics like sum of two numbers, factorial, checking prime numbers, Fibonacci sequence, reversing a string, finding the maximum element in an array, checking palindromes, calculating power, counting vowels in a string, and finding the greatest common divisor.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SECOND TERM: PROGRAMMING 10 C Function

Examples with
Pseudocode,
Flowcharts, and
Code

Shahrul Najib bin Abdullah


KTE Forest Heights Seremban
Example 1: Sum of Two Numbers

#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 sum(int a, int b) {


return a + b;
}

int main() {
int num1 = 5, num2 = 3;
printf("Sum: %d\n", sum(num1, num2));
return 0;
}

Example 2: Factorial of a Number

#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;
}

Example 3: Check Prime Number

#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;
}

Example 4: Fibonacci Sequence

#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;
}

Example 5: Reverse a String

#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>

void reverseString(char *str) {


int start = 0, end = strlen(str) - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}

int main() {
char str[] = "Hello";
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}

Example 6: Find Maximum Element in an Array

#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>

int findMax(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
int arr[] = {1, 3, 4, 2, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Maximum Element: %d\n", findMax(arr, n));
return 0;
}

Example 7: Check Palindrome

#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>

bool isPalindrome(char *str) {


int start = 0, end = strlen(str) - 1;
while (start < end) {
if (str[start] != str[end])
return false;
start++;
end--;
}
return true;
}
int main() {
char str[] = "radar";
if (isPalindrome(str))
printf("%s is a palindrome.\n", str);
else
printf("%s is not a palindrome.\n", str);
return 0;
}

Example 8: Calculate Power of a Number

#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 power(int base, int exp) {


int result = 1;
for (int i = 1; i <= exp; i++) {
result *= base;
}
return result;
}

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 countVowels(char *str) {


int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
char c = str[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
count++;
}
}
return count;
}

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 gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int num1 = 56, num2 = 98;
printf("GCD: %d\n", gcd(num1, num2));
return 0;
}

You might also like