From 2614569cb38520265b2f5234922154c73c2e55ba Mon Sep 17 00:00:00 2001 From: Hasan Zahid <67003358+hasan-zahid-code@users.noreply.github.com> Date: Thu, 20 Jan 2022 14:56:45 +0500 Subject: [PATCH 1/5] Sum of 1s in binary of a denary number Sum of 1s in binary of a denary number through a recursive function --- binary.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 binary.c diff --git a/binary.c b/binary.c new file mode 100644 index 0000000..24005c0 --- /dev/null +++ b/binary.c @@ -0,0 +1,19 @@ +#include + +//Sum of 1s in binary of a denary number through a recursive function + +int sum(int x){ + if (x==0) return 0; + else { + int count=0; + //count+=10; + return x%2+sum(x/2); + } +} +int main(){ + int x; + printf("Enter a denary number: "); + scanf("%d",&x); + printf("sum is: %d",sum(x)); +} + From 9d4379267eb34437ffcd1e9e81fd3b7c09c771c7 Mon Sep 17 00:00:00 2001 From: Hasan Zahid <67003358+hasan-zahid-code@users.noreply.github.com> Date: Thu, 20 Jan 2022 14:59:11 +0500 Subject: [PATCH 2/5] Rename binary.c to Sum of 1s in binary.c --- binary.c => Sum of 1s in binary.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename binary.c => Sum of 1s in binary.c (100%) diff --git a/binary.c b/Sum of 1s in binary.c similarity index 100% rename from binary.c rename to Sum of 1s in binary.c From 03c9b0cdac3e097d3f45d6623432bd5b659e2617 Mon Sep 17 00:00:00 2001 From: Hasan Zahid <67003358+hasan-zahid-code@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:26:41 +0500 Subject: [PATCH 3/5] recursive function CountOnes Write a recursive function CountOnes that takes an unsigned integer and return the number of ones in binary representation of that given number. For an example: if 28 is given as an argument to this function it will return 3, as 2810 is 111002 --- Sum of 1s in binary.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Sum of 1s in binary.c b/Sum of 1s in binary.c index 24005c0..f06cd74 100644 --- a/Sum of 1s in binary.c +++ b/Sum of 1s in binary.c @@ -16,4 +16,3 @@ int main(){ scanf("%d",&x); printf("sum is: %d",sum(x)); } - From 1fe0ab360a6162b699904b16222b2e0f62a62062 Mon Sep 17 00:00:00 2001 From: Hasan Zahid <67003358+hasan-zahid-code@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:31:21 +0500 Subject: [PATCH 4/5] sum of the digits of integer --- sum of the digits.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sum of the digits.c diff --git a/sum of the digits.c b/sum of the digits.c new file mode 100644 index 0000000..9a396e9 --- /dev/null +++ b/sum of the digits.c @@ -0,0 +1,17 @@ +#include + +//Recursive function which takes an integer as a parameter and returns the sum of the digits of the number + +int sum(int x){ + if (x==0) return 0; + else { + return x%10+sum(x/10); + } +} +int main(){ + int x; + printf("Enter a number: "); + scanf("%d",&x); + printf("Sum is: %d",sum(x)); +} + From 9e59f2577bed1a2ada39ead3d5287633360c14ec Mon Sep 17 00:00:00 2001 From: Hasan Zahid <67003358+hasan-zahid-code@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:35:19 +0500 Subject: [PATCH 5/5] Print in ascending & descending recursive --- Print in ascending & descending recursive.c | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Print in ascending & descending recursive.c diff --git a/Print in ascending & descending recursive.c b/Print in ascending & descending recursive.c new file mode 100644 index 0000000..fbaaf75 --- /dev/null +++ b/Print in ascending & descending recursive.c @@ -0,0 +1,51 @@ +#include +/*Write a program that takes n numbers from the user and sort them in ascending order using a +function. Then ask the user whether to print in ascending order or descending order. Write a +recursive function for the print in both cases.*/ +int main(){ + int len,x,i,j; + printf("Enter Length of array: "); + scanf("%d",&len); + int arr[len]; + for(i=1;i<=len;i++){ + printf("Element %d: ",i); + scanf("%d",&x); + arr[i]=x; + } + sort(len,arr); + printf("\nAscending: "); + printAsc(1,len,arr); + printf("\nDescending: "); + printDec(1,len,arr); +} +int sort(int a,int arr[a]){ + int i,j,temp; + for(i=1;i<=a-1;i++){ + for(j=i+1;j<=a;j++){ + if(arr[i]>arr[j]){ + temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; + } + + } +} + +} +int printAsc(int start, int end, int arr[]){ + if(start==end+1) + return 0; + else{ + printf("\nElement %d: %d",start,arr[start]); + printAsc(start+1,end,arr); + } +} +int printDec(int start,int end, int arr[]){ + if(end==start-1) + return 0; + else{ + printf("\nElement %d: %d",end,arr[end]); + printDec(start,end-1,arr); + } +} +