Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've made a code for calculating odd and even number from 1 to X integer and my program showing the result. But I need to add one thing in my code that how to calculate how many odd/even numbers are in the list.

As Example:Sample Output

1 2 3 4 5 6 7 8 9 10
The Even number are: 2
The Even number are: 4
The Even number are: 6
The Even number are: 8
The Even number are: 10
There are 5 even numbers <- need to add it in my code

The odd number are: 1
The odd number are: 3
The odd number are: 5
The odd number are: 7
The odd number are: 9
There are 5 odd numbers

What I have tried:

C++
/*My trying code is below:*/

#include <stdio.h>

int main() 
{
  int i, j; 
  int X=10;

  for(i=1;i<=X;i++)
  {
  printf("%d\t", i);
  }
  printf("\n");
 for(i=1;i<=X;i++)
 {
    if((i%2)==0)
 {
  printf(" The Even number are: %d\n", i);
 }
    
}

 printf("\n");
 
 for(i=1;i<=X;i++)
  {
  if((i%2)!=0)
  {
  printf(" The odd number are: %d\n", i);
    }
     }
  return 0;
}
Posted
Updated 19-Aug-18 1:54am
v2
Comments
OriginalGriff 19-Aug-18 7:50am    
And?
What does it do that you didn't expect, or not do that you did?
Are there any error messages? If so, where, and what?
Where are you stuck?
What help do you need?
Richard MacCutchan 20-Aug-18 4:41am    
Add a counter (or two counters) to your program, and add 1 to them for each number you find.
Member 13954054 20-Aug-18 4:48am    
Dear Richard,
Thanks for your reply. But I can't code to make a counters in my code. Could you please help me? In my code you see that I use two loops if and for. By using these 2 loops i want see how many odd and even numbers in list.

As example in output:
1 2 3 4 5 6
Even: 2 4 6
total even: 3
odd: 1 3 5
total odd: 3

Regards
Manash
Richard MacCutchan 20-Aug-18 5:03am    
Seriously? You cannot create a variable, initialise it to zero and add 1 to it each time you find an even number? I suggest you go back and study your course notes.
Member 13954054 20-Aug-18 5:36am    
Dear,
See my code below but I fail again. I couldn't understand where should I intial the variable that means in if loop or others?

#include <stdio.h>

int main()
{
int i, j;
int X=10;
printf(" The numbers are: \n");
for(i=1;i<=X;i++)
{
printf("%d\t", i);
}
printf("\n\n");
printf(" The Even number are:");
for(i=1;i<=X;i++)
{
if((i%2)==0)
{
printf(" %d ", i);
int j=0;
j=j+1;
printf("%d", j);
}

}

printf("\n\n");
printf(" The odd number are: ");
for(i=1;i<=X;i++)
{
if((i%2)!=0)
{
printf(" %d ", i);
}
}
return 0;
}

output:

The numbers are:
1 2 3 4 5 6 7 8 9 10

The Even number are: 2 1 4 1 6 1 8 1 10 1

The odd number are: 1 3 5 7 9

1 solution

Quote:
But I need to add one thing in my code that how to calculate how many odd/even numbers are in the list.

How can you write correct code to list odd and even numbers and be unable to count them?

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
/*My trying code is below:*/

#include <stdio.h>

int main()
{
  int i, j;
  int X=10;

  for(i=1;i<=X;i++)
  {
    printf("%d\t", i);
  }
  printf("\n");
  for(i=1;i<=X;i++)
  {
    if((i%2)==0)
    {
      printf(" The Even number are: %d\n", i);
    }

  }

  printf("\n");

  for(i=1;i<=X;i++)
  {
    if((i%2)!=0)
    {
      printf(" The odd number are: %d\n", i);
    }
  }
  return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
v2
Comments
Nelek 19-Aug-18 15:49pm    
[Quote]
How can you write correct code to list odd and even numbers and be unable to count them?
[/Quote]
Probably by copying it from the internet? Getting someone else to do his homework? Or were you just asking a "rethoric question"? ;)
Patrice T 19-Aug-18 17:14pm    
I was suspecting something like that, and the lack of reaction gives the answer. :)

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