Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

C Lab (CA3104)

Name: MUSKAN BIHAR


Registration ID: 2021PGCACA076
Course: MCA Ist Semester
Date: 24-12-2021

Instructor- DR.Chandrashekhar Azad sir.


Assignment no. 8

1.Write a program to add, subtract, multiply and divide two integers using user-defined type
function with return type.

2. Write a C program to find power of any number using recursion.

3. Write a program to search a user given number in an array and print the location of the
number if it is found.

4. Print the following pattern

1
121
12321
1234321
123454321

5. Write a program that reads a 5×5 array of integers and then prints the row sum and the
column sum :

Enter row 1: 8 3 9 0 10
Enter row 2 : 3 5 17 1 1
Enter row 3: 2 8 6 23 1
Enter row 4: 15 7 3 2 9
Enter row 5: 6 14 2 6 0

Row total: 30 27 40 36 28
Column total :34 37 37 32 21

6. Write a program to swap two integers using call by value and call by reference methods of
passing arguments to a function.
PROBLEM 1:-

Write a program to add, subtract, multiply and divide two integers using user-defined type
function with return type.

SOLUTION:-

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two number ");
scanf("%d %d",&a,&b);
printf("The sum is = %d",sum(a,b));
printf("\nThe substraction is = %d",substract(a,b));
printf("\nThe multiplcation is = %d",multiplcation(a,b));
printf("\nThe division is = %d",divide(a,b));
}
int sum(int a ,int b)
{
return a+b;
}
int substract(int a ,int b)
{
return a-b;
}
int multiplcation(int a,int b)
{
return a*b;
}
int divide(int a,int b)
{
return (int)a/b;
}
OUTPUT:-
PROBLEM 2:-

Write a C program to find power of any number using recursion.

SOLUTION :-

#include<stdio.h>
int main()
{
int b,expo;
printf("Enter the base and exponent ");
scanf("%d%d",&b,&expo);
printf("The answer is = %d",power(b,expo));
}
int power(int a,int b)
{
if(b==0)
return 1;
return a*power(a,b-1);
}

OUTPUT:-

PROBLEM 3:-

Write a program to search a user given number in an array and print the location of the number
if it is found.

SOLUTION:-

#include<stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++)
{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at index %d", toSearch, i);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

return 0;
}

OUTPUT:-
PROBLEM 4:-

Print the following pattern


1
121
12321
1234321
123454321

SOLUTION :-

#include<stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=j-2;j>=1;j--)
printf("%d ",j);
printf("\n");
}
return 0;
}

OUTPUT:-
PROBLEM 5:-

Write a program that reads a 5×5 array of integers and then prints the row sum and the column
sum :
Enter row 1: 8 3 9 0 10
Enter row 2 : 3 5 17 1 1
Enter row 3: 2 8 6 23 1
Enter row 4: 15 7 3 2 9
Enter row 5: 6 14 2 6 0

Row total: 30 27 40 36 28
Column total :34 37 37 32 21

SOLUTION:-

#include<stdio.h>
#define ROWS 5
#define COLS 5
int main(void)
{
int a[ROWS][COLS], row_totals[ROWS] = {0}, col_totals[COLS] = {0};

for (int i = 0; i < ROWS; i++) {


printf("Enter row %d: ", i + 1);
scanf("%d%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]);
}

for (int i = 0; i < ROWS; i++) {


for (int j = 0; j < COLS; j++) {
row_totals[i] += a[i][j];
col_totals[j] += a[i][j];
}
}

printf("\nRow totals:");
for (int i = 0; i < ROWS; i++) {
printf(" %d", row_totals[i]);
}

printf("\nColumn totals:");
for (int i = 0; i < COLS; i++) {
printf(" %d", col_totals[i]);
}
printf("\n");

return 0;
}

OUTPUT:-

PROBLEM 6:-

Write a program to swap two integers using call by value and call by reference methods of
passing arguments to a function.

SOLUTION:-

Call By Value:-

#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a
and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual
parameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20,
b = 10
}

Call By Reference:-

#include <stdio.h>
void swap(int *, int *);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a
and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual
parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a =
20, b = 10
}
OUTPUT:-

Call By Value:-

Call By Reference:-

You might also like