Turn Desktop View Off
You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program to find HCF (GCD) between two numbers.
by Dinesh Thakur Category: Control Structures

This program segment given below uses a straight-forward approach to count the number of odd and even digits in a given integer number(num).

Initially, variables nodd and neven, the counts for odd and even digits, respectively, are initialized to zero. Then a while loop is used to count the odd and even digits. In each iteration of this loop, the least significant digit of number num is first determined in variable digit,then the appropriate counter is incremented using an if-else statement and the least significant digit is removed from number num. The loop is executed as long as the value of num is greater than zero, i. e., all digits in num are not processed. Finally, the values of nodd and never are printed.

#include <stdio.h>

void main()

{    

       int nodd,neven,num,digit ; 

       clrscr();

       printf("Count number of odd and even digits in a given integer number ");

       scanf("%d",&num);

                nodd = neven =0; /* count of odd and even digits */

                while (num> 0)

                    {

                        digit = num % 10; /* separate LS digit from number */

                        if (digit % 2 == 1)

                           nodd++;

                        else neven++;

                               num /= 10; /* remove LS digit from num */

                    }

                        printf("Odd digits : %d Even digits: %d\n", nodd, neven);

                        getch();

}

       C Program Count Number of odd and even digits in a given integer number



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article