Find out how many 1 and 0 in a given number.

Write a C program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number in C.

Program in C

Code:

/*Write a C program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number Using C*/

#include<stdio.h>
int main ()
{
    int num,c0=0,c1=0,r;
    printf("Enter a number:");
    scanf("%d",&num);
    while (num)
{
  r = num % 10;
  num = num / 10;
  if(r==1)
  {
      c1++;
  }
  if(r==0)
  {
      c0++;
  }
}
printf("The total number of zero's are %d \n",c0);
printf("The total number of one's are %d \n",c1);
}

Input/Output:
Enter a number:
1010101
The total number of zero's are 3
The total number of one's are 4

Write a C++ program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number in C++.

Program in C++

Code:

/*Write a C++ program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number using C++*/

#include<iostream>
using namespace std;
int main ()
{
    int num,c0=0,c1=0,r;
    cout<<"Enter a number:";
    cin>>num;
    while (num)
{
  r = num % 10;
  num = num / 10;
  if(r==1)
  {
      c1++;
  }
  if(r==0)
  {
      c0++;
  }
}
    cout<<"The total number of zero's are "<<c0;
cout<<"\nThe total number of one's are "<<c1;
}

Input/Output:
Enter a number:
10001111
The total number of zero's are 3
The total number of one's are 5

Write a JAVA program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number in Java.

Program in Java

Code:

/*Write a JAVA program to find out all How many 1 and 0 in a given number. or Write a program to  find out all How many 1 and 0 in a given number using Java*/

import java.util.Scanner;
public class NumberOfZeroAndOne {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
int c0=0,c1=0,r;
    System.out.println("Enter a number:");
    num=cs.nextInt();
    while (num!=0)
{
  r = num % 10;
  num = num / 10;
  if(r==1)
  {
      c1++;
  }
  if(r==0)
  {
      c0++;
  }
}
System.out.println("The total number of zero's are "+c0);
System.out.println("The total number of one's are "+c1);

cs.close();
}
}

Input/Output:
Enter a number:
100001
The total number of zero's are 4
The total number of one's are 2

Write a PYTHON to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given number in Python

Program in Python

Code:

'''Write a Python program to find out all How many 1 and 0 in a given number. or Write a program to find out all How many 1 and 0 in a given the number using Python '''

print("Enter a number:")
num=int(input())
c1=0
c0=0
while int(num):
    r=num%10
    num=int(num/10)
    if r==1:
        c1=c1+1
    if r==0:
        c0=c0+1
print("The total number of zero's are ",c0)
print("The total number of one's are ",c1)

Input/Output:
Enter a number:
10001
The total number of zero's are  3
The total number of one's are  2





Post a Comment

0 Comments