Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i am trying to print factors in even number. can somone tell me what i did wrong here, cuz i cant print even factors

0 votes
asked Mar 20, 2018 by annonymus
#include <stdio.h>

#include<stdlib.h>

int EVEN(int);

int main()

{

int i;

int c, n;

  printf("random number in [10,100]\n");

  for (c = 1; c <= 1; c++) {

    n = rand() % 100 + 1;

    printf("%d\n", n);

  }

    

    printf("Factors of %d are: ", n);

    for(i=1; i <= n; ++i)

    {

        if (n%i == 0)

        {

            printf("%d ",i);

        }

    }

    return 0;

}

    int EVEN(int n)

{

if (n%2 == 0){

      printf("even factors are:");

  }

}

4 Answers

0 votes
answered Mar 20, 2018 by anonymous
need to scanf and atribute the randon number to the "int" you want
commented Mar 20, 2018 by annonymus
i don't get it atribut what random number to the int????
0 votes
answered Mar 20, 2018 by anonymous
https://onlinegdb.com/SJ5573AKz

You should have used EVEN() function to check whether number is even or not.
0 votes
answered Mar 21, 2018 by Patryk Klimawicz (140 points)
I don't know if I understood problem, but here is some code :)

#include <stdio.h>
#include<stdlib.h>
//#include <time.h>
// int EVEN(int);

int main() {
// srand(time(NULL));
int n;

  printf("Random number in [10,100]\n");

  for (int c = 1; c <= 1; c++) {
    n = rand() % 100 + 1;   // this is always the same variable... if you want to make it diffrent
    printf("%d\n", n);      // every time you run a program add #include <time.h> and in main function      srand(time(NULL)
  }                         // rand()%100+1 gives you number in [1, 100] not [10,100]. Better is rand()%91+10;

    

    printf("Factors of %d are: ", n);

    for(int i=1; i <= n; ++i) {

        if (n%i == 0 && i%2==0) {   // here you can check if i is a factor and also if i is even
            printf("%d ", i);       // so one extra function is not necessery :)
        }
    }

    return 0;
}
/*
int EVEN(int n) {
    if (n%2 == 0) {
        printf("Even factors are:");
    }
}*/
0 votes
answered Mar 21, 2018 by anonymous
edited Mar 21, 2018

in your code you're looking for all numbers that CAN be a part of the factors sequence. Not the factors temselves.

This code will work:

    for (int i = 2; i <= n; )
        if (n % i == 0)
            {
                printf ("%d ", i);
                n /= i;
                i = 2;
            }
        else
            i++;

Also, if you need to get random numbers any time you start the program, you should include <time.h> and add 

srand(time(NULL));

in the beginning of the main() function;

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...