Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
int main()
{
    int t,n,i,d;

    scanf("%d",&t);//number of inputs

    for(i=0; i<t; i++)
    {
        scanf("%d",&n);//inputs
    }

    ///finding d which indicates the divisors of the numbers
    for(i=0; i<n; i++)
    {
        d = n%i;
    }

    ///checking if the divisors are odd or even

    if(d==0 && i%2!=0)//d = n%i
    {
        printf("YES");
    }
    else
    {
        printf("No");
    }
    return 0;
}


What I have tried:

I am trying to check if some given numbers have odd divisors or not..if the number has an odd divisor it will print Yes
otherwise it will print no..

I wrote the avobe program but it is not Working..Can you please elaborate why this is not working and what is the actual solution of it in C ..
Thank you :)
Posted
Updated 1-Apr-21 9:12am
v3
Comments
Rick York 1-Apr-21 17:53pm    
Mr. K5054's solution is essentially what one does to find prime factors except two is always checked but you don't need to do that.

You seem to be a little confused about how for loops work. Consider this one:
C
for(i=0; i<t; i++)
{
    scanf("%d",&n);//inputs
}
This is a loop that will read in t values, each time replacing the value of n. The program does not progress beyond this point until all values have been read in. So if the user enters 3 for t, then numbers 6, 20 and 64, the next statement in your program only ever sees the last value, in this case 64. What you want is to nest your for loops e.g.
C
for(i=0; i < t; i++)
{
    scanf("%d", &n);  /* now we have an "n" value to work with */

    int test_divisor;
    for(test_divisor = 1; test_divisor < n; test_divisor++)
    {
       if (  /* test_divisor is a divisor of n */ )
          /* do something */
    }
}

Almost all compilers these days support C99 for loop declaration of the index, so you should use that if at all possible, e.g.
C
for(int i = 0; i < 10; i++)
Also think about how you find your possible divisors. Do you need to start at 0 and go all the way to n? If you're only interested in odd divisors, maybe you could start at 3 and then go up by 2, testing as you go. Maybe you don't need to go all the way to n, either. I'll leave you to think about that a bit.
 
Share this answer
 
Quote:
HELP to find odd divisors of some given numbers

Many errors of logic here.
C++
for(i=0; i<t; i++)
{   // This loop is reading all inputs
    scanf("%d",&n);//inputs
}
// but only the last input reach this point
///finding d which indicates the divisors of the numbers
for(i=0; i<n; i++) // I am not sure testing 0 help on any way
{
    d = n%i; // here you do all calculation and discard all but the last one
}
// At this point, d=1 and i=n

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this 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