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); + } +} + diff --git a/Sum of 1s in binary.c b/Sum of 1s in binary.c new file mode 100644 index 0000000..f06cd74 --- /dev/null +++ b/Sum of 1s in binary.c @@ -0,0 +1,18 @@ +#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)); +} 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)); +} +