CodeEval Easy Level Challenge Odd Numbers

CodeEval Easy Level Challenge Odd Numbers:

Click here to go to this problem.

Description:

For this problem there is no input. We just print odd numbers from 1 to 100. Here I have solved this problem in 4 programming languages c, c++, java, python. Have a look to learn the syntax difference between these.

There are many ways to solve this one. One way to solve can be starting at 1 and keep increment by 2 until we reach 99. Another can be checking if the number is divisible by 2.


 C:
/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

#include <stdio.h>
int main(int argc, const char * argv[]) {
    register unsigned int i = 1;
    for(; i<=99; i+=2) printf("%u\n",i);
    return 0;
}

C++:
/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

#include <iostream>
int main(int argc, const char * argv[]) {
    register unsigned int i = 1;
    for(; i<=99; ++i){
        if(i&1) std::cout << i << "\n";
    }
    return 0;
}

Java:
/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

import java.io.*;
public class Main {
    public static void main (String[] args) throws IOException {
        for(int i=1; i<100; ++i){
            if(i % 2 == 1) System.out.println(i);
        }
    }
}

Python (Python 3):
#
# Author: Quickgrid ( Asif Ahmed )
# Site: https://quickgrid.wordpress.com
#

for i in range(1,100):
    if(i%2):
        print(i)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s