Count the number of odd and even digits

Problem statement:- Program to Find the number of odd and even digits in the given number.

Sample Input/Output:-

Sample Input First: 879413                  Sample Output First: 2  4

Sample Input Second: 547863             Sample Output Second3  3

Data requirement:-

   Input Data:- num

  Output Data:-
even, odd

Program in C

Here is the source code of the C Program to Find the number of odd and even digits in the given number.

Code:

#include<stdio.h>
int main()
{
    int num;
    printf("Enter the number:");
    scanf("%d",&num);
    int odd=0,even=0;
    while(num!=0)
    {
        int rem=num%10;
        if(rem%2==1)
            odd++;
        else
            even++;
        num/=10;
    }
    printf("Number of even digits = %d\n",even);
    printf("Number of odd digits = %d",odd);
}

Input/Output:
Enter the number:879413
Number of even digits = 2
Number of odd digits = 4

Program in C++

Here is the source code of the C++ Program to count the number of odd and even digits in an integer.

Code:

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter the number:";
    cin>>num;
    int odd=0,even=0;
    while(num!=0)
    {
        int rem=num%10;
        if(rem%2==1)
            odd++;
        else
            even++;
        num/=10;
    }
    cout<<"Number of even digits = "<<even;
    cout<<"Number of odd digits = "<<odd;
}

Input/Output:
Enter the number:547863
Number of even digits = 3
Number of odd digits = 3

Program in Java

Here is the source code of the Java Program to Find the number of odd and even digits in the given number.

Code:

import java.util.Scanner;
public class NumberOfOddAndEvenDigits {

public static void main(String[] args) {
    Scanner cs=new Scanner(System.in);
    int num;
    System.out.println("Enter the number:");
    num=cs.nextInt();
    int odd=0,even=0;
    while(num!=0)
    {
        int rem=num%10;
        if(rem%2==1)
            odd++;
        else
            even++;
        num/=10;
    }
    System.out.println("Number of even digits = "+even);
    System.out.println("Number of odd digits = "+odd);
    cs.close();
}
}

Input/Output:
Enter the number:
535254456
Number of even digits = 4
Number of odd digits = 5

Program in Python

Here is the source code of the Python Program to count the number of odd and even digits in an integer.

Code:

print("Enter the number:")
num=int(input())
odd=0
even=0
while(num!=0):
    rem=num%10
    if(rem%2==1):
        odd+=1
    else:
        even+=1
    num//=10
print("Number of even digits = ",even)
print("Number of odd digits = ",odd) 

Input/Output:
Enter the number:
5432
Number of even digits =  2
Number of odd digits =  2







Post a Comment

0 Comments