C Function Examples with Runtime Input
1. add() - Add two numbers
#include <stdio.h>
#include <conio.h>
int add(int a, int b) {
return a + b;
}
void main() {
clrscr(); {
int x = 5, y = 3;
printf("Sum = %d", add(x, y));
getch();
}
2. sub() - Subtract two numbers
#include <stdio.h>
#include <conio.h>
int sub(int a, int b) {
return a - b;
}
void main() {
clrscr(); {
int x = 10, y = 4;
printf("Difference = %d", sub(x, y));
getch();
}
3. mul() - Multiply two numbers
#include <stdio.h>
#include <conio.h>
int mul(int a, int b) {
return a * b;
}
void main() {
clrscr(); {
int x = 6, y = 7;
printf("Product = %d", mul(x, y));
getch();
}
4. divide() - Divide two numbers
#include <stdio.h>
#include <conio.h>
float divide(float a, float b) {
return a / b;
}
void main() {
clrscr(); {
float x = 10, y = 2;
printf("Quotient = %.2f", divide(x, y));
getch();
}
5. modulus() - Find remainder
#include <stdio.h>
#include <conio.h>
int modulus(int a, int b) {
return a % b;
}
void main() {
clrscr(); {
int x = 10, y = 3;
printf("Remainder = %d", modulus(x, y));
getch();
}
6. maximum() - Find maximum of two numbers
#include <stdio.h>
#include <conio.h>
int maximum(int a, int b) {
return (a > b) ? a : b;
}
void main() {
clrscr(); {
int x = 8, y = 6;
printf("Maximum = %d", maximum(x, y));
getch();
}
7. minimum() - Find minimum of two numbers
#include <stdio.h>
#include <conio.h>
int minimum(int a, int b) {
return (a < b) ? a : b;
}
void main() {
clrscr(); {
int x = 8, y = 6;
printf("Minimum = %d", minimum(x, y));
getch();
}
8. square() - Square of a number
#include <stdio.h>
#include <conio.h>
int square(int a) {
return a * a;
}
void main() {
clrscr(); {
int x = 5;
printf("Square = %d", square(x));
getch();
}
9. cube() - Cube of a number
#include <stdio.h>
#include <conio.h>
int cube(int a) {
return a * a * a;
}
void main() {
clrscr(); {
int x = 3;
printf("Cube = %d", cube(x));
getch();
}
10. factorial() - Factorial of a number
#include <stdio.h>
#include <conio.h>
int factorial(int n) {
int f = 1;
for(int i = 1; i <= n; i++) {
f *= i;
}
return f;
}
void main() {
clrscr(); {
int x = 5;
printf("Factorial = %d", factorial(x));
getch();
}
11. isEven() - Check if number is even
#include <stdio.h>
#include <conio.h>
int isEven(int a) {
return a % 2 == 0;
}
void main() {
clrscr(); {
int x = 4;
if(isEven(x))
printf("%d is Even", x);
else
printf("%d is Odd", x);
getch();
}
12. isOdd() - Check if number is odd
#include <stdio.h>
#include <conio.h>
int isOdd(int a) {
return a % 2 != 0;
}
void main() {
clrscr(); {
int x = 7;
if(isOdd(x))
printf("%d is Odd", x);
else
printf("%d is Even", x);
getch();
}
13. isPrime() - Check if number is prime
#include <stdio.h>
#include <conio.h>
int isPrime(int n) {
if(n <= 1) return 0;
for(int i = 2; i < n; i++) {
if(n % i == 0)
return 0;
}
return 1;
}
void main() {
clrscr(); {
int x = 7;
if(isPrime(x))
printf("%d is Prime", x);
else
printf("%d is Not Prime", x);
getch();
}
14. sumDigits() - Sum of digits of a number
#include <stdio.h>
#include <conio.h>
int sumDigits(int n) {
int sum = 0;
while(n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
void main() {
clrscr(); {
int x = 1234;
printf("Sum of digits = %d", sumDigits(x));
getch();
}
15. reverse() - Reverse a number
#include <stdio.h>
#include <conio.h>
int reverse(int n) {
int rev = 0;
while(n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
void main() {
clrscr(); {
int x = 123;
printf("Reverse = %d", reverse(x));
getch();
}
16. swap() - Swap two numbers
#include <stdio.h>
#include <conio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void main() {
clrscr(); {
int x = 5, y = 10;
swap(&x, &y);
printf("x = %d, y = %d", x, y);
getch();
}
17. power() - Calculate a^b
#include <stdio.h>
#include <conio.h>
int power(int a, int b) {
int result = 1;
for(int i = 1; i <= b; i++)
result *= a;
return result;
}
void main() {
clrscr(); {
int x = 2, y = 3;
printf("%d^%d = %d", x, y, power(x, y));
getch();
}
18. table() - Print multiplication table
#include <stdio.h>
#include <conio.h>
void table(int n) {
for(int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n*i);
}
}
void main() {
clrscr(); {
int x = 5;
table(x);
getch();
}
19. fibonacci() - Print Fibonacci series
#include <stdio.h>
#include <conio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
printf("%d %d ", a, b);
for(int i = 3; i <= n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
void main() {
clrscr(); {
int terms = 10;
fibonacci(terms);
getch();
}
20. gcd() - Greatest common divisor
#include <stdio.h>
#include <conio.h>
int gcd(int a, int b) {
while(b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void main() {
clrscr(); {
int x = 48, y = 18;
printf("GCD = %d", gcd(x, y));
getch();
}