Skip to content

Sum of 1s in binary of a denary number #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Print in ascending & descending recursive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<stdio.h>
/*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);
}
}

18 changes: 18 additions & 0 deletions Sum of 1s in binary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<stdio.h>

//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));
}
17 changes: 17 additions & 0 deletions sum of the digits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<stdio.h>

//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));
}