0% found this document useful (0 votes)
2 views5 pages

Assignment Three

The document contains multiple C programming examples demonstrating different concepts such as recursion, function prototypes, and sorting algorithms. It includes a recursive function to calculate the factorial of a number, a simple addition function, and a bubble sort implementation for sorting an array. Additionally, it features the Sieve of Eratosthenes algorithm for generating prime numbers up to a specified limit.

Uploaded by

bkip5675
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Assignment Three

The document contains multiple C programming examples demonstrating different concepts such as recursion, function prototypes, and sorting algorithms. It includes a recursive function to calculate the factorial of a number, a simple addition function, and a bubble sort implementation for sorting an array. Additionally, it features the Sieve of Eratosthenes algorithm for generating prime numbers up to a specified limit.

Uploaded by

bkip5675
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment three

1 A recursive function is a function that calls itself to solve a problem. It breaks a large problem
into smaller instances of the same problem and solves each one step-by-step until it reaches a
simple case, called the base case, which can be solved directly.

#include <stdio.h>

// Recursive function to find factorial

int factorial(int n) {

if (n == 0) // Base case

return 1;

else

return n * factorial(n - 1); // Recursive call

int main() {

int num = 5;

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

#include <stdio.h>

/* ---------- function prototype ---------- */

int add(int a, int b);

/* -------------- main program ------------- */

int main(void)
{

int num1, num2, sum;

/* input from user */

printf("Enter first number : ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

/* call the function and store result */

sum = add(num1, num2);

/* output the result */

printf("Sum = %d\n", sum);

return 0;

/* ---------- function definition ----------- */

int add(int a, int b)

return a + b; /* perform the addition and send back the result */

}
3

#include <stdio.h>

/* -------- function prototypes -------- */

void readArray(int numbers[], int length);

void bubbleSortAscending(int numbers[], int length);

void printArray(const int numbers[], int length);

/* --------------- main ---------------- */

int main(void)

int length;

/* Ask user for the number of elements */

printf("How many numbers do you want to sort? ");

scanf("%d", &length);

/* Create an array of the required size */

int numbers[length];

/* Read elements, sort them, and display the result */

readArray(numbers, length);

bubbleSortAscending(numbers, length);

printf("\nSorted list (ascending): ");

printArray(numbers, length);
return 0;

/* ---------- function definitions ---------- */

/* Read 'length' integers into the array 'numbers' */

void readArray(int numbers[], int length)

printf("Enter %d integers:\n", length);

for (in

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

void sieveOfEratosthenes(int n);

int main(void)

int n;

printf("Generate primes up to: ");

if (scanf("%d", &n) != 1 || n < 2) {

printf("Please enter an integer ≥ 2.\n");

return 1;

}
sieveOfEratosthenes(n);

return 0;

/* ----------- Sieve implementation ----------- */

void sieveOfEratosthenes(int n)

/* 1. Allocate a boolean array and assume every number is prime */

bool *isPrime = calloc(n + 1, sizeof(bool));

if (!isPrime) {

perror("Not enough memory");

exit(EXIT_FAILURE);

for (int i = 2; i <= n; ++i)

isPrime[i] = true;

/* 2. Mark multiples of each*

You might also like