C Programming Practice - Function Based Programs with Algorithms
1. Sum using 4 different user-defined function patterns
Algorithm:
Algorithm:
1. Start
2. Define four different function patterns:
- No arguments, no return
- With arguments, no return
- No arguments, with return
- With arguments and return
3. In each, take two numbers and compute their sum accordingly
4. Call each function from main and display the result
5. End
#include <stdio.h>
void sum1() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
}
void sum2(int a, int b) {
printf("Sum = %d\n", a + b);
}
int sum3() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
return a + b;
}
int sum4(int a, int b) {
return a + b;
}
int main() {
int a, b;
sum1();
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum2(a, b);
printf("Sum = %d\n", sum3());
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", sum4(a, b));
return 0;
}
C Programming Practice - Function Based Programs with Algorithms
2. Swap two numbers using call by value
Algorithm:
Algorithm:
1. Start
2. Define a function swap(a, b) that attempts to swap two numbers
3. Inside swap, use a temporary variable to exchange the values of a and b
4. In main, read two numbers and call the swap function
5. Display the values before and after calling swap
6. Show that values don't actually swap due to call by value
7. End
#include <stdio.h>
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside swap function: a = %d, b = %d\n", a, b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
swap(a, b);
printf("In main function: a = %d, b = %d\n", a, b);
return 0;
}
3. Area of circle using static function inside another function
Algorithm:
Algorithm:
1. Start
2. Define a static function area(r) that calculates and returns PI * r * r
3. Define a function dis() that reads radius and calls area()
4. In main, call dis() three times in a loop
5. End
#include <stdio.h>
#define PI 3.1416
static float area(float r) {
return PI * r * r;
}
void dis() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area = %.2f\n", area(r));
C Programming Practice - Function Based Programs with Algorithms
}
int main() {
for (int i = 0; i < 3; i++) {
dis();
}
return 0;
}
4. Recursive functions for GCD and Fibonacci
Algorithm:
Algorithm for GCD:
1. Start
2. Define recursive gcd(a, b)
3. If b == 0, return a
4. Else, return gcd(b, a % b)
Algorithm for Fibonacci:
1. Start
2. Define recursive function fibonacci(n)
3. If n is 0 or 1, return n
4. Else, return fibonacci(n-1) + fibonacci(n-2)
5. Loop through 0 to n and print each term
6. End
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int fibonacci(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int a, b, n, i;
printf("Enter two numbers for GCD: ");
scanf("%d %d", &a, &b);
printf("GCD = %d\n", gcd(a, b));
printf("Enter number of terms in Fibonacci series: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
C Programming Practice - Function Based Programs with Algorithms
printf("\n");
return 0;
}
5. Macro to compute S = A + 3*B
Algorithm:
Algorithm:
1. Start
2. Define a macro SUM(A, B) as A + 3*B
3. In main, read values for A and B
4. Use macro to compute result and display it
5. End
#include <stdio.h>
#define SUM(A, B) (A + 3 * B)
int main() {
int a, b;
printf("Enter values of A and B: ");
scanf("%d %d", &a, &b);
printf("S = %d\n", SUM(a, b));
return 0;
}