0% found this document useful (0 votes)
165 views25 pages

BCA Programming Lab Tasks

This document contains C programming assignments on functions and pointers for a Programming Lab subject. It includes questions to write functions to: 1. Calculate factorial, check for prime and palindrome numbers using a switch case menu. 2. Find maximum and minimum elements of an array using separate functions. 3. Check if a matrix is symmetric using a function. 4. Implement string handling functions like strlen, strcpy, strrev, strcat, strcmp. 5. Write recursive functions to calculate factorial of a number, print Fibonacci series, and calculate sum of digits of a number.

Uploaded by

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

BCA Programming Lab Tasks

This document contains C programming assignments on functions and pointers for a Programming Lab subject. It includes questions to write functions to: 1. Calculate factorial, check for prime and palindrome numbers using a switch case menu. 2. Find maximum and minimum elements of an array using separate functions. 3. Check if a matrix is symmetric using a function. 4. Implement string handling functions like strlen, strcpy, strrev, strcat, strcmp. 5. Write recursive functions to calculate factorial of a number, print Fibonacci series, and calculate sum of digits of a number.

Uploaded by

Ronit Deb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

University of Engineering & Management, Kolkata

PROGRAMMING LAB

Subject Code – BCA291

SESSION- 2020 ODD SEM

ASSIGNMENT – VI

Objective – C language: Functions and Pointers

6.1 Write a menu-driven program to calculate the factorial of a given


number check whether the number is prime or not check whether the
number is a palindrome or not. Use different functions for each of the
above tasks and call the functions from main ( ) using switch statement.

#include <stdio.h>
 
int fibo(int n) //recursive function, returns nth
element of series
{
if(n==1)       //1st term
return 0;
else if(n==2)  //2nd term
return 1;
 
return (fibo(n-1)+fibo(n-2));
}
 
int fact(int n) //recursive function, returning fact
{
int f;
 
if(n>0)
{
f=n*fact(n-1);
return f;
}
else
return 1;
}
 
int main()
{
 
int n,i; //to avoid redeclaration and its consequent
error
int ch=6; //ch is choice
 
int digit, num, rev=0, A=0; //for palindrome &
angstrom checking
 
do
{
printf("\n\n MENU ");
printf("\n 1. Fibonacci Series ");
printf("\n 2. Factorial of number ");
printf("\n 3. Multiplication table ");
printf("\n 4. Check whether number is Palindrome ");
printf("\n 5. Check whether number is Angstrom ");
printf("\n 6. EXIT ");
printf("\n Enter your choice: ");
scanf("%d",&ch); printf("\n");
 
switch(ch)
{
case 1:
 
printf(" Enter the no of elements of fibonacci series
to show:");
scanf("%d",&n);
 
printf("\n Fibonacci: \n");
for(i=1; i<=n; i++)
printf(" %d ", fibo(i) );
 
printf("\n");
break;
 
case 2:
 
printf(" Enter the number to find factorial of: ");
scanf("%d", &n);
 
printf("\n Factorial is: %d", fact(n));
break;
 
case 3:
 
printf(" Enter the number of whose
multiplication table ");
printf("is to be made (till 12) :");
scanf("%d",&n);
 
printf(" Table: ");
for(i=1; i<=12; i++)
printf("\n %dx%d=%d ",n,i,(n*i));
 
break;
 
case 4:
 
printf(" Enter number to check whether palindrome:
");
scanf("%d",&n);
 
num=n;
 
do
{
digit=num%10;
rev=(rev*10)+digit;
num/=10;
}
while(num>0);
 
if(rev==n)
printf("\n Number is Palindrome. ");
else
printf("\n Number is not Palindrome. ");
 
break;
 
case 5:
 
printf(" Enter number to check whether Angstrom: ");
scanf("%d",&n);
 
num=n;
 
do
{
digit=num%10;
A+=digit*digit*digit;
num/=10;
}
while(num>0);
 
if(A==n)
printf("\n Number is Angstrom. ");
else
printf("\n Number is not Angstrom. ");
 
break;
 
 
case 6:
printf(" Exiting... ");
break;
 
default:
printf("INVALID CHOICE");
}
 
}
while(ch!=6);
 
return 0;
}
 

OUTPUT:

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 1

Enter the no of elements of fibonacci series to


show:8

Fibonacci:
0 1 1 2 3 5 8 13

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 2

Enter the number to find factorial of: 4

Factorial is: 24

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 3

Enter the number of whose multiplication table is to


be made (till 12): 17
Table:
17x1=17
17x2=34
17x3=51
17x4=68
17x5=85
17x6=102
17x7=119
17x8=136
17x9=153
17x10=170
17x11=187
17x12=204

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 4

Enter number to check whether palindrome: 121

Number is Palindrome.

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 5

Enter number to check whether Angstrom: 371

Number is Angstrom.

MENU
1. Fibonacci Series
2. Factorial of number
3. Multiplication table
4. Check whether number is Palindrome
5. Check whether number is Angstrom
6. EXIT
Enter your choice: 6

Exiting...

6.2 Write a program to find the maximum and minimum elements of an


array using two different functions array_max ( ) and array_min ( ).
1 #include <stdio.h>
2 #include <conio.h>
3  
4 int sumofarray(int a[],int n)
5 {
6 int min,max,i;
7 min=max=a[0];
8     for(i=1; i<n; i++)
9     {
10          if(min>a[i])
11   min=a[i];  
12    if(max<a[i])
13     max=a[i];      
14     }
15     
16     printf("minimum of array is : %d",min);
17     printf("\nmaximum of array is : %d",max);
18 }
19 int main()
20 {
21     int a[1000],i,n,sum;
22   
23     printf("Enter size of the array : ");
24     scanf("%d", &n);
25
26     printf("Enter elements in array : ");
27     for(i=0; i<n; i++)
28     {
29         scanf("%d",&a[i]);
30     }
31     sumofarray(a,n);
32
 
33
    
34
35 }
Output:
Output
C
1 Enter size of the array: 5
2 Enter elements in array: 1
32
4 35
50
6 -1
7 minimum of an array is: -1
8 maximum of an array is: 35

6.3 Write a program to find whether a matrix is symmetric or not using a


function is_symmetric ( ).
#include <stdio.h>
#define SIZE 3

int main()
{
int A[SIZE][SIZE]; // Original matrix
int B[SIZE][SIZE]; // Transpose matrix

int row, col, isSymmetric;

/* Input elements in matrix A from user */


printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &A[row][col]);
}
}

/*
* Find transpose of matrix A
*/
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
/* Store each row of matrix A to each column
of matrix B */
B[row][col] = A[col][row];
}
}

/*
* Check whether matrix A is equal to its transpose or
not
*/
isSymmetric = 1;
for(row=0; row<SIZE && isSymmetric; row++)
{
for(col=0; col<SIZE; col++)
{
/* If matrix A is not equal to its transpose
*/
if(A[row][col] != B[row][col])
{
isSymmetric = 0;
break;
}
}
}

/*
* If the given matrix is symmetric.
*/
if(isSymmetric == 1)
{
printf("\nThe given matrix is Symmetric matrix:
\n");

for(row=0; row<SIZE; row++)


{
for(col=0; col<SIZE; col++)
{
printf("%d ", A[row][col]);
}
printf("\n");
}
}
else
{
printf("\nThe given matrix is not Symmetric
matrix.");
}

return 0;
}
Output
Enter elements in matrix of size 3x3:
1 2 3
2 4 5
3 5 8

The given matrix is Symmetric matrix:


1 2 3
2 4 5
3 5 8

6.4 Write five user defined functions which perform the tasks of strlen ( ),
strcmp ( ), strcpy ( ), strrev ( ), strcat ( ). Write a main ( ) to test these
functions.
/* strlen.c */

#include <stdio.h>
#include <string.h>

int main()
{
char *t = "XXX";
printf( "Length of <%s> is %d.\n", t, strlen( t
));
}

This prints:

Length of <XXX> is 3.
/* strcpy.c */

#include <stdio.h>
#include <string.h>

int main()
{
char s1[100],
s2[100];
strcpy( s1, "xxxxxx 1" );
strcpy( s2, "zzzzzz 2" );

puts( "Original strings: " );


puts( "" );
puts( s1 );
puts( s2 );
puts( "" );

strcpy( s2, s1 );

puts( "New strings: " );


puts( "" );
puts( s1 );
puts( s2 );
}

This will print:

Original strings:

xxxxxx 1
zzzzzz 2

New strings:

xxxxxx 1
xxxxxx 1
strncpy( s2, s1, 5 );

—then the results change to:


New strings:

xxxxxx 1
xxxxxz 2
/* strcat.c */

#include <stdio.h>
#include <string.h>

int main()
{
char s1[50],
s2[50];
strcpy( s1, "Tweedledee " );
strcpy( s2, "Tweedledum" );
strcat( s1, s2 );
puts( s1 );
}

This prints:

Tweedledee Tweedledum
/* strcmp.c */

#include <stdio.h>
#include <string.h>

#define ANSWER "blue"

int main()
{
char t[100];
puts( "What is the secret color?" );
gets( t );
while ( strcmp( t, ANSWER ) != 0 )
{
puts( "Wrong, try again." );
gets( t );
}
puts( "Right!" );
}
/* strchr.c */
#include <stdio.h>

#include <string.h>

int main()
{
char *t = "MEAS:VOLT:DC?";
char *p;
p = t;
puts( p );
while(( p = strchr( p, ':' )) != NULL )
{
puts( ++p );
}
}

This prints:

MEAS:VOLT:DC?
VOLT:DC?
DC?

6.5 Write a recursive function to print the factorial of a number.

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n,
multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output

Enter a positive integer: 6


Factorial of 6 = 720

6.6 Write a recursive function to print the first n Fibonacci numbers.


//Fibonacci Series using Recursion
#include<stdio.h>
int fib(int n)
{
   if (n <= 1)
      return n;
   return fib(n-1) + fib(n-2);
}
  
int main ()
{
  int n = 9;
  printf("%d", fib(n));
  getchar();
  return 0;
}

Output
34

6.7 Write a recursive function to calculate the sum of all digits of a number
entered by the user.
// Recursive C program to find sum of digits 
// of a number
#include <stdio.h>
  
// Function to check sum of digit using recursion
int sum_of_digit(int n)
{
    if (n == 0)
       return 0;
    return (n % 10 + sum_of_digit(n / 10));
}
  
// Driven Program to check above
int main()
{
    int num = 12345;
    int result = sum_of_digit(num);
    printf("Sum of digits in %d is %d\n", num, result);
    return 0;
}

Output:
Sum of digits in 12345 is 15

6.8 Write a recursive function to evaluate nCr = n! / r! * (n – r)!


// CPP program To calculate The Value Of nCr
#include <bits/stdc++.h>
using namespace std;
  
int fact(int n);
  
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
  
// Returns factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Driver code
int main()
{
    int n = 5, r = 3;
    cout << nCr(n, r);
    return 0;
}
Input : n = 5, r = 2
Output : 30
The value of 5C2 is 10

Input : n = 3, r = 1
Output : 3

6.9 Write a C program to swap two number using call by reference.


**
* C program to swap two number using call by reference
*/

#include <stdio.h>

/* Swap function declaration */


void swap(int * num1, int * num2);

int main()
{
int num1, num2;

/* Input numbers */
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);

/* Print original values of num1 and num2 */


printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

/* Pass the addresses of num1 and num2 */


swap(&num1, &num2);

/* Print the swapped values of num1 and num2 */


printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

return 0;
}
/**
* Function to swap two numbers
*/
void swap(int * num1, int * num2)
{
int temp;

// Copy the value of num1 to some temp variable


temp = *num1;

// Copy the value of num2 to num1


*num1= *num2;

// Copy the value of num1 stored in temp to num2


*num2= temp;

printf("After swapping in swap function n");


printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}

Input

Input num1: 10
Input num2: 20

Output

Values after swapping:


Num1 = 20
Num2 = 10

6.10 Write three functions, using pointers, to concatenate two strings, to


compare two strings and to reverse a string, respectively. Test these functions in
a complete program.

1. #include<stdio.h>

2.

3. int compare_string(char*, char*);


4.

5. main()

6. {

7. char first[100], second[100], result;

8.

9. printf("Enter first string\n");

10. gets(first);

11.

12. printf("Enter second string\n");

13. gets(second);

14.

15. result = compare_string(first, second);

16.

17. if ( result == 0 )

18. printf("Both strings are same.\n");

19. else

20. printf("Entered strings are not equal.\n");

21.

22. return 0;

23. }

24.

25. int compare_string(char *first, char *second)

26. {
27. while(*first==*second)

28. {

29. if ( *first == '\0' || *second == '\0' )

30. break;

31.

32. first++;

33. second++;

34. }

35. if( *first == '\0' && *second == '\0' )

36. return 0;

37. else

38. return -1;

39. }

#include <stdio.h>
int main() {
char s1[100] = "programming ", s2[] = "is awesome";
int i, j;

// length of s1 is stored in i
for (i = 0; s1[i] != '\0'; ++i) {
printf("i = %d\n", i);
}

// concatenating each character of s2 to s1


for (j = 0; s2[j] != '\0'; ++j, ++i) {
s1[i] = s2[j];
}

// terminating s1 string
s1[i] = '\0';

printf("After concatenation: ");


puts(s1);

return 0;
}

6.11 Write two functions to find the maximum and minimum elements of a
matrix, using array of pointers.
/* structure is used to return two values from minMax() */
#include<stdio.h>
struct pair 
{
  int min;
  int max;
};  
  
struct pair getMinMax(int arr[], int n)
{
  struct pair minmax;     
  int i;
    
  /*If there is only one element then return it as min and
max both*/
  if (n == 1)
  {
     minmax.max = arr[0];
     minmax.min = arr[0];     
     return minmax;
  }    
  
  /* If there are more than one elements, then initialize
min 
      and max*/
  if (arr[0] > arr[1])  
  {
      minmax.max = arr[0];
      minmax.min = arr[1];
  }  
  else
  {
      minmax.max = arr[1];
      minmax.min = arr[0];
  }    
  
  for (i = 2; i<n; i++)
  {
    if (arr[i] >  minmax.max)      
      minmax.max = arr[i];
    
    else if (arr[i] <  minmax.min)      
      minmax.min = arr[i];
  }
    
  return minmax;
}
  
/* Driver program to test above function */
int main()
{
  int arr[] = {1000, 11, 445, 1, 330, 3000};
  int arr_size = 6;
  struct pair minmax = getMinMax (arr, arr_size);
  printf("nMinimum element is %d", minmax.min);
  printf("nMaximum element is %d", minmax.max);
  getchar();
}  

Output:
Minimum element is 1
Maximum element is 3000

6.12 Write a function to multiply two matrices using array of pointers.


**
* C program to multiply two matrix using pointers
*/

#include <stdio.h>

#define ROW 3
#define COL 3
/* Function declarations */
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL], int
res[][COL]);

int main()
{
int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL];

/*
* Input elements in matrices.
*/
printf("Enter elements in first matrix of size %dx
%d\n", ROW, COL);
matrixInput(mat1);

printf("Enter elements in second matrix of size %dx


%d\n", ROW, COL);
matrixInput(mat2);

// Call function to multiply both matrices


matrixMultiply(mat1, mat2, product);

// Print product of both matrix


printf("Product of both matrices is : \n");
matrixPrint(product);

return 0;
}

/**
* Function to input elements in matrix from user.
*
* @mat Two-dimensional array to store user input.
*/
void matrixInput(int mat[][COL])
{
int row, col;

for (row = 0; row < ROW; row++)


{
for (col = 0; col < COL; col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}

/**
* Function to print elements in a two-dimensional array.
*
* @mat Two-dimensional array to print.
*/
void matrixPrint(int mat[][COL])
{
int row, col;

for (row = 0; row < ROW; row++)


{
for (col = 0; col < COL; col++)
{
printf("%d ", *(*(mat + row) + col));
}

printf("\n");
}
}
/**
* Function to multiply two matrices.
*
* @mat1 First matrix
* @mat2 Second matrix
* @res Resultant matrix to store product of both
matrices.
*/
void matrixMultiply(int mat1[][COL], int mat2[][COL], int
res[][COL])
{
int row, col, i;
int sum;

for (row = 0; row < ROW; row++)


{
for (col = 0; col < COL; col++)
{
sum = 0;

/*
* Find sum of product of each elements of
* rows of first matrix and columns of second
* matrix.
*/
for (i = 0; i < COL; i++)
{
sum += (*(*(mat1 + row) + i)) * (*(*(mat2
+ i) + col));
}

/*
* Store sum of product of row of first matrix
* and column of second matrix to resultant
matrix.
*/
*(*(res + row) + col) = sum;
}
}
}
Output
Enter elements in first matrix of size 3x3
10 20 30
40 50 60
70 80 90
Enter elements in second matrix of size 3x3
1 2 3
4 5 6
7 8 9
Product of both matrices is :
300 360 420
660 810 960
1020 1260 1500

You might also like