how to use an array to input random.......?

any one please

write a program that use an array to input random numbers from users, then traverse the same array checking if number is even or odd, if it is even, ad the number to the sum of even numbers.

if it is odd, add the number to the sum of odd numbers.

Update:

i write such a program, but the output is garbage value, i think i can not assign value to array, the value which user enter.

kindly correct the program, if anyone know, thank you

#include<stdio.h>

#include<conio.h>

main()

{

int size, i, sumeven=0, sumodd=0;

printf("enter the size of array \n",&size);

scanf("%d",&size);

int array[size];

printf(" enter the values of elements\n");

for(i=0; i<size; i++)

{

scanf("%d", &array[i]);

}

for(i=0; i<size; i++)

{

if(i%2==0)

sumeven = sumeven + array[i];

else

sumodd = sumodd + array[i];

}

printf("the sum of even values = %d\n",&sumeven);

2 Answers

Relevance
  • 8 years ago
    Favorite Answer

    I will do the hard part for you

    This is how you put random numbers, from 1 to 100 inside an array

    ------------------------------------------

    #include <ctime>

    #include <cstdlib>

    .

    .

    srand (time(NULL));

    // place random numbers into the array

    for(int i = 0; i < NUM_INTS; ++i)

    {

         arry[i] =rand()%100+1;

    }

    -----------------------------

    This is how you check for even numbers

    --------------------------------

    If(number % 2==0)

    {

         // NUMBER IS EVEN

    }

    else

    {

         // NUMBER IS ODD

    }

    ---------------------------------------

    Hope this helps

  • 6 years ago

    You can use Scanner by writing:

    import java.util.Scanner;

    you then have to name your scanner like:

    Scanner in= new Scanner(System.in);// in is the name of your scanner

    Then you can declare and initialize user input by using:

    .nextInt, .nextDouble, or .next

Still have questions? Get your answers by asking now.