23CHI077
23CHI077
23CHI077
NAME:PAGHADAR PREM
Course Code and Name: 2CS101, Computer Science
Practical No. 7-a
Aim : Find union and intersection of two input integer arrays using user defined function.
The function should return the resultant array to the main function.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>
void function(int a[], int b[], int n,int m){
printf("Union of two array is:\n");
for(int i = 0; i < n; i++){
printf("%d ", a[i]);
}
for(int i = 0; i < m; i++){
int kk = 0;
for(int j = 0; j < n; j++){
if(b[i] == a[j]){
kk = 1;
break;
}
}
if(kk == 0) printf("%d ", b[i]);
}
printf("\nIntersection of two array is:\n");
if(n < m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i] == b[j]) {printf("%d ", a[i]); break;}
}
}
}
else {
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(b[i] == a[j]) {printf("%d ", b[i]); break;}
}
}
}
}
void main(){
printf("Please enter the no of element of two array\n");
int n = 0, m = 0;
scanf("%d %d", &n, &m);
int arr[n], brr[m];
printf("Please enter elements of array 1\n");
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
printf("Please enter elements of array 2\n");
for(int i = 0; i < m; i++){
scanf("%d", &brr[i]);
}
function(arr, brr, n , m);
}
Input: each array has 2 elements, elements of array 1 are 1,2 and elements
of array 2 are 2,3
Output: Union of 2 arrays is: 1,2,3
Intersection of two arrays is 2
#include<string.h>
int function(int n){
int z = 0;
if(n == 0) z = 0;
else if(n == 1) z =1;
else{
z = function(n - 1) + function(n - 2);
}
return z;
}
void main(){
int n = 0;
printf("Please enter any number\n");
scanf("%d", &n);
printf("%d", function(n));
Input: 9
Output: 34
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>
int function(int n){
int z = 0;
if(n == 0) z = 1;
else{
z = n * function(n - 1);
}
return z;
}
void main(){
Input: no input
Output: 5040.0000
}
void main(){
char arr[30];
printf("Please enter any Name\n");
gets(arr);
function(arr);
}
Input: karan
Output: a is repeated 2 times