Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Count number of Ones in Array

0 votes
asked Apr 10 by Saba Noor (120 points)
I need to count number of Ones of a 32 bit array in C.

1 Answer

+2 votes
answered Apr 11 by anu kanth sudharsanam (180 points)
#include <stdio.h>

int main()
{
    char a[32];
    int count=0,i;
    
    printf("Enter your string");
    scanf("%[^\n]s",a);
    for(i=0;i<32;i++)
    {
        if(a[i]=='1')
        {
            count++;
            
        }
    }
    
    printf("number of ones in an array is %d",count);

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...