Click here to Skip to main content
15,885,994 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in C programming ,control statements chapter.

What I have tried:

C++
/*I have done*/
int num,even=0,odd=0;

do{printf("enter number")
scanf("%d",&num);
if(num÷2==0)
even+=1;
else odd+=1;
printf("even number %d",even);
printf("odd number %d",odd);
Posted
Updated 20-Mar-18 6:12am
v2
Comments
Deepak pandey 20-Mar-18 11:25am    
This is just a logic is it correct ??

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
/*I have done*/
int num,even=0,odd=0;
do{
  printf("enter number")  // ; is missing here
  scanf("%d",&num);
  if(num÷2==0)
    even+=1;
  else 
    odd+=1;
// } is missing here
// end of loop is missing here
printf("even number %d",even);
printf("odd number %d",odd);

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Note that your code will count -1 as an input value.
 
Share this answer
 
This should do the trick:

C++
int num = 0, even = 0, odd = 0;

do
{
    printf( "enter number (-1 exits):" );
    scanf( "%d", &num );
    if ( num != -1 )
    {    
        if ( num & 0x01 ) odd++;
        else              even++;
    };
    
} while ( num != -1 );
    
printf( "even number %d", even );
printf( "odd number %d",  odd );


I Hope that helps you.
-Doug
 
Share this answer
 
v2
Comments
W Balboos, GHB 20-Mar-18 13:08pm    
Your answer's close enough to mine that I won't submit one. Here's the change.
int counter[2]; 


Then, for all numbers you could simply have:
counter[num&0x01]++;

Just a nuance, but I thought you might find it interesting.
Doug Joseph 20-Mar-18 14:06pm    
Dude... great call! I like that even better. :)
KarstenK 21-Mar-18 4:32am    
You should do his homework. He should learn coding. ;-)
At first you need some tutorial about the do-while loop.

The resulting code could look like this:
C++
int count = 0;
do {
count++;
if( ... )
  break;//quick leave loop
}while( num!= -1;
 
Share this answer
 
Just change
num÷2==0
to
num%2==0
, then you'll be good.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900