Thread: Counting 1-50 and pointing out odd and even numbers

  1. #1
    Registered User juliette's Avatar
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    2

    Counting 1-50 and pointing out odd and even numbers

    Hello!

    I've just started an introduction course in C programming at the University. I am currently struggling on the last exercise of an mandatory assignment.

    Its probably some basic stuff, but we were asked to make a program that counts from 1-50, which i have following code for;

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int i;
    
    for (i=0; i <= 50; i=i+1)
    
    
    {printf("%d\n", i);
    }
    This code works just fine, but the follow-up question is giving me a headache. We are asked to make the program to point out if the numbers are even or odd (with an "if" statement!).

    Example:
    1 is an odd number
    2 is an even number
    .... etc

    Hope you are able to help

    edit: Sorry about some misspellings, the assignment isnt given in english.
    Last edited by juliette; 09-13-2012 at 10:27 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    It's not an 'if' "function", but an 'if' "statement", also called a conditional; you don't need any function.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Assuming that getting to university means you know about basic mathematical operations like division and remainder (modulus), the interesting operator for you to go and read about is %
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User juliette's Avatar
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    2
    I think I have the right code to state which of the numbers that are even or odd, but my problem is to add ".. is an even number" in the original code.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Post what you have so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting random numbers using pointing arrays.
    By jFran in forum C Programming
    Replies: 8
    Last Post: 05-10-2011, 01:56 PM
  2. Counting Digits & Numbers
    By yacek in forum C Programming
    Replies: 4
    Last Post: 10-06-2010, 08:44 PM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. Counting Occurrance of Numbers
    By dat in forum C Programming
    Replies: 6
    Last Post: 11-03-2002, 02:11 PM
  5. counting even numbers
    By slamit93 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2002, 01:54 PM