23CHI077

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

Roll No: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

Practical No. 7-b


Aim: Consider a currency system in which there are notes of seven denominations,
namely Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 20, Rs. 50 and Rs. 100. A sum of Rs. N is entered as
an input. Write a function to compute the smallest number of notes that will combine to
give Rs. N.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>
int function(int n){
int z = n / 100; int y = n % 100;
z += y / 50; y = y % 50;
z += y / 20; y %= 20;
z += y / 10; y %= 10;
z += y / 5; y %= 5;
z += y / 2; y %= 2;
z += y;
return z;
}
void main(){
printf("Please enter the amount of money\n");
int n = 0;
scanf("%d", &n);
printf("%d", function(n));
}
Input: 100
Output: 1

Practical No. 7-c


Aim: Write a program to compute F(n) such that F(n) = 0, if n = 0, F(n) = 1, if n = 1,
otherwise F(n) = F(n - 1) + F(n - 2).
Methodology :
#include<stdio.h>
#include<math.h>

#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

Practical No. 7-d


Aim:
Aman has 10 balls that have different numbers on it and Shoaib has 6 balls. They both
arrange balls in all different possible ways. What is the ratio of number of arrangements
made by Aman to that made by Shoaib? Use recursive function to calculate

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(){

printf("%f", (float)function(10) / (float)function(6));


}

Input: no input
Output: 5040.0000

Practical No. 7-e


Aim: Perform Q 6c using user defined function.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>
void function(char arr[]){
int brr[26];
for(int i = 0; i < 26; i++) brr[i] = 0;
for(int i = 0; i < strlen(arr); i++){
int d = arr[i];
if(d < 91){
brr[d - 65]++;
}
else{
brr[d - 97]++;
}
}
int k = 0;
for(int i = 0; i < 26; i++){
if(brr[i] > 1) k++;
}
if(k == 0)printf(" None of the character are repeating");
else {
for(int i = 0; i < 26; i++)
if(brr[i] > 1)printf("%c is repeating %d times.\n", i + 97, brr[i]);
}

}
void main(){
char arr[30];
printf("Please enter any Name\n");
gets(arr);
function(arr);
}

Input: karan
Output: a is repeated 2 times

You might also like