Week 7
a) Write a C program to find the factorial of a number using functions.
b) Write a C program to check whether a number is prime using a function.
c) Write a Program to swap the values of two variables using i) Call by Value ii) Call by Reference
d) Write a C program to sort an array using functions.
e) Write a C program to perform matrix operations using functions.
a) Write a C program to find the factorial of a number using functions.
#include <stdio.h>
// Function Prototype
long factorial(int n);
// Function Definition
long factorial(int n)
{
long fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
long result = factorial(num); // Function Call
printf("Factorial of %d = %ld\n", num, result);
}
Program: Prime Check Using Factor Count (with Functions)
#include <stdio.h>
// Function Prototype
int isPrime(int n);
// Function Definition (Using Factor Count)
int isPrime(int n)
{
int count = 0;
if (n <= 1)
return 0; // Not prime
// Count the number of factors
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
count++;
}
// Prime number has exactly 2 factors: 1 and itself
if (count == 2)
return 1;
else
return 0;
}
main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
}
c) Write a Program to swap the values of two variables using i) Call by Value ii) Call by
Reference
i) Program using Call by Value
(Does NOT swap the original values)
#include <stdio.h>
void swapByValue(int x, int y) ;
main()
{
int a = 10, b = 20;
printf("Before swap (Call by Value): a = %d, b = %d\n", a, b);
swapByValue(a, b);
printf("After swap (Call by Value): a = %d, b = %d\n", a, b);
void swapByValue(int x, int y)
{
int temp = x;
x = y;
y = temp;
printf("Inside swapByValue: x = %d, y = %d\n", x, y);
}
Output
Before swap: a = 10, b = 20
Inside swapByValue: x = 20, y = 10
After swap: a = 10, b = 20
Values do NOT swap in main(), because only copies are passed.
ii) Program using Call by Reference
(Swaps the original values using pointers)
#include <stdio.h>
void swapByReference(int *x, int *y);
main()
{
int a = 10, b = 20;
printf("Before swap (Call by Reference): a = %d, b = %d\n", a, b);
swapByReference(&a, &b);
printf("After swap (Call by Reference): a = %d, b = %d\n", a, b);
}
void swapByReference(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
d) Write a C program to sort an array using functions.
#include <stdio.h>
void sortArray(int a[], int n);
void sortArray(int a[], int n)
{
int i, j, temp;
// Simple Bubble Sort
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-i-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Function Call
sortArray(arr, n);
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
e) Write a C program to perform matrix operations using functions.
#include <stdio.h>
void readMatrix(int a[10][10], int r, int c);
void displayMatrix(int a[10][10], int r, int c);
void addMatrix(int a[10][10], int b[10][10], int c[10][10], int r, int col);
void subMatrix(int a[10][10], int b[10][10], int c[10][10], int r, int col);
void mulMatrix(int a[10][10], int b[10][10], int c[10][10], int r1, int c1, int c2);
// Function to read matrix
void readMatrix(int a[10][10], int r, int c)
{
int i, j;
printf("Enter elements:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
}
// Function to display matrix
void displayMatrix(int a[10][10], int r, int c)
{
int i, j;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
// Matrix Addition
void addMatrix(int a[10][10], int b[10][10], int c[10][10], int r, int col)
{
int i, j;
for(i = 0; i < r; i++)
{
for(j = 0; j < col; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
// Matrix Subtraction
void subMatrix(int a[10][10], int b[10][10], int c[10][10], int r, int col)
{
int i, j;
for(i = 0; i < r; i++)
{
for(j = 0; j < col; j++)
{
c[i][j] = a[i][j] - b[i][j];
}
}
}
// Matrix Multiplication
void mulMatrix(int a[10][10], int b[10][10], int c[10][10], int r1, int c1, int c2)
{
int i, j, k;
for(i = 0; i < r1; i++)
{
for(j = 0; j < c2; j++)
{
c[i][j] = 0;
for(k = 0; k < c1; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
main()
{
int a[10][10], b[10][10], c[10][10];
int r1, c1, r2, c2, choice;
printf("Enter rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of Matrix B: ");
scanf("%d %d", &r2, &c2);
printf("\nEnter Matrix A:\n");
readMatrix(a, r1, c1);
printf("\nEnter Matrix B:\n");
readMatrix(b, r2, c2);
printf("\n--- Matrix Operations ---\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if(r1 == r2 && c1 == c2)
{
addMatrix(a, b, c, r1, c1);
printf("\nResult of Addition:\n");
displayMatrix(c, r1, c1);
}
else
printf("Addition not possible!\n");
break;
case 2:
if(r1 == r2 && c1 == c2)
{
subMatrix(a, b, c, r1, c1);
printf("\nResult of Subtraction:\n");
displayMatrix(c, r1, c1);
}
else
printf("Subtraction not possible!\n");
break;
case 3:
if(c1 == r2)
{
mulMatrix(a, b, c, r1, c1, c2);
printf("\nResult of Multiplication:\n");
displayMatrix(c, r1, c2);
}
else
printf("Multiplication not possible!\n");
break;
default:
printf("Invalid choice!\n");
}