1 to 100 Odd Numbers in Array

1 to 100 odd numbers in array

The Range class comes with a very cool feature for that purpose:

1.9.3-p286 :005 > (1..10).step(2).to_a
=> [1, 3, 5, 7, 9]

How can I create an array of only odd numbers between 1 and n - Java?

You can do this is linear time complexity and without using an ArrayList.

Your final output will always have n/2 elements so your array size can be fixed at the same. In the next step you can simply populate the values in your array.

FYR code:

int[] arr = new int[((n+1)/2)];
for(int i = 0, e = 1; i < arr.length; e += 2, i++) {
arr[i] = e;
}

How can I display odd numbers 1 to 100 with 5 numbers per line in C?

Your problem is not a C problem. You even should not approach a keyboard until you can make clear in your brain what you have to do.

Let us write it down in plain English:

  • you want to print numbers from 1 to 100
  • you want only odd numbers (so even ones are to be ignored)
  • you want a new line after every fifth number

Now in pseudo code:

loop for i from 0 to 100
if i % 2 == 0 continue loop
print number
increment counter
if counter == 5
print newline
reset counter to 0
end of if block
end of loop block

For the above algo to work you must just initialize the counter variable to 0 before starting the main loop.

And now it is time to go back to your keyboard and translate that in C language, but it is not the hard part:

#include <stdio.h>

int main() {
int i, counter = 0;

for (i = 0; i < 100; i++) {
if (i % 2 == 0) continue;
// as highest number will be 99 "%3d" would ensure at least 1 space
// but " %2d" makes the intention more evident
printf("% 2d", i);
if (++counter >= 5) {
printf("\n");
counter = 0;
}
}
return 0;
}

Other implementations are possible, this one is just a working one. What you should have learned here: a programmer must be sure of its algo before thinking about how to code it...

Fill array with odd numbers

You are getting 0 for each even index because the value at that index is never set.

This line:

int[] t = new int[n];

declares an array of int of length n where each element is initialized to 0. Now consider your code:

for (i = 0; i < t.length; i++) {
if (i % 2 != 0) {
t[i] += i;
}
}

You are saying: when the index of my array is odd, let's set it to this index, otherwise, do nothing (and keep 0). This explains the result you are getting.

What you want to do is not to check whether the index of the array is odd: you want to set an odd value to each index of the array. Consider this code instead:

for (i = 0; i < t.length; i++) {
t[i] = 2 * i + 1;
}

For each index, this sets the value of the array to an odd number (2n+1 is always odd).

(Note that I wrote = instead of +=: it gives the intent better and does not rely on the fact that the array was initialized with 0)

An array to assign even and odd numbers

I am try to point some mistakes of the code by using the comment .

And I think this code also has logical problem is that you want to put a list of numbers to odd-array and even-array,but the initial vaule of the even and odd array is all zero.

When you divide the list of number into tow array,you are not overwrite all of the initial value,that will cause there are some zero value be printed at last but they are not belong to odd neither even.

    #include <stdio.h>
#include <math.h>

int main()
{
int a[100],odd[100],even[100],i,j,n;
//you should first assign a vaule to n and then use n
//and be careful for the value of the n,
//it should not larger than the size of the array,or it will overflow.
for(i=0;i<=n;i++)
{
a[i]=0;
odd[i]=0;
even[i]=0;
}
//put below two lines to the front of the above for-loop
printf("Enter the number of elements which you want to check as even or odd");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
a[i]=even[i];
else
a[i]=odd[i];
}
for(i=0;i<n;i++)
//you type a wrong 'printf' satement,here is the right way
//printf("The set of even numbers is %d",even[i]);
printf("The set of even numbers is %d"),even[i];
for(i=0;i<n;i++)
printf("The set of even numbers is %d"),odd[i];//you type a wrong 'printf' satement
return 0;
}

how to implement odd numbers in a new array?

Just create new array and add the odd numbers

use strict;
use warnings;

my @zahlen = (1..100);
my @ungerade;
my $i = 0;

foreach my $zahlen (@zahlen){
if ($zahlen % 2){
$ungerade[$i] = $zahlen;
$i = $i + 1;
}

}

print "@ungerade"


Related Topics



Leave a reply



Submit