Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is Determine odd and even number from Multiplication table of 1 to 10 in c programming-2d array. You can't count any number second time. Every number will be unique.
Example : Odd will be 14. But in this program it shows 15.

N.B : I tried to get help from different programming community but they cant help me.
If it necessary to rewrite code then plz hlp me to figure out or rewrite it please.

What I have tried:

<pre>#include <stdio.h>

int main() 
{
    int a[10][10];
    int i,j;
    int even = 0,odd = 0;

    // print all digit in a array.
    for(i = 0;i < 10;i++) {
        for(j = 0;j < 10;j++) {
            a[i][j] = (i + 1) * (j + 1);
            printf("%3d ",a[i][j]);
        }
        printf("\n");
    }

    printf("\n\n");

    // print Remaining number and count odd-even.
    for(i = 0;i < 10;i++) {
        for(j = i;j < 10;j++) {
           if(a[i][j] % 2 == 0) {
            even++;
           }

           else{
            odd++;
           }
            printf("%3d ",a[i][j]);
        }
        printf("\n");
    } 

    printf("\n\n");
    printf("Total even : %d \t Total odd : %d \n",even,odd);


    return 0;
}
Posted
Updated 27-Jun-22 21:06pm
Comments
0x01AA 27-Jun-22 16:30pm    
Quote: "You can't count any number second time".
Then you need to register what numbers you allready counted to avoid counting them twice.
Lara Thomas 28-Jun-22 10:17am    
How will I register it?

Your second loop starts with j = i rather than j = 0, so it won't look at the entire grid. If you're trying to optimize for i * j == j * i, you have to increment twice unless i == j.
 
Share this answer
 
Comments
Lara Thomas 28-Jun-22 10:15am    
Sir, so how I will write the loop to get the solution ?
could you write the part of code here?
Greg Utas 28-Jun-22 10:21am    
In your second loop:
if(a[i][j] % 2 == 0)
{
   ++even;
   if(i != j) ++even;
}
else
{
   ++odd;
   if(i != j) ++odd;
}
A different way to approach this is not to write any code at all. The product of two even numbers, or an even and an odd number, is an even number. The product of two odd numbers is an odd number. The multiplications are 1...10 x 1..10, so there are 5 odd numbers in each range. You should therefore expect 25 odd numbers and 75 even numbers.
Patrice T 28-Jun-22 10:31am    
How do you handle 7 * 7 = 49 ?
Greg Utas 28-Jun-22 10:33am    
It should increment odd once because i == j.
Patrice T 28-Jun-22 14:19pm    
Ok.
How do you take "You can't count any number second time. Every number will be unique." into account ?
I fear you count 6 four times.
1 * 6 = 2 * 3 = 3 * 2 = 6 * 1 = 6
Quote:
You can't count any number second time. Every number will be unique.

How many times do you count 8 or 36 ?
8 = 1 * 8 = 2 * 4
36 = 4 * 9 = 6 * 6
[Update]
Quote:
Sir, so how I will write the loop to get the solution ?
could you write the part of code here?

You need to remember if a number is first time seen or already seen, counting how many times a number is seen) .
 
Share this answer
 
v3
Comments
Lara Thomas 28-Jun-22 10:15am    
Sir, so how I will write the loop to get the solution ?
could you write the part of code here?

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