Thread: Simple problem just can't seem to get it

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Simple problem just can't seem to get it

    How do you get the sum of all numbers that are less than a max value and either even or odd? I know that I am not supposed to ask to have assignments done for me but this is not the whole program i just need the basic idea of how to get all of the even or odd numbers up to that certain number. I am sorry if this question isn't clear.
    ~Sven

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    As this is homework, I wrote an example in c++ rather then c

    Code:
    #include <iostream>
    
    int main()
    {
    	int MAX = 7;
    	bool evens = false;
    	int sum = 0;
    	int i = 1;
    	if (evens)
    		i++;
    	for (; i <= MAX; i=i+2)
    		sum += i;
    	std::cout << sum;
    }
    Notes
    How do you get the sum of all numbers that are less than a max value and either even or odd?
    The sum is infinite, likewise the sum of all integers is also infinite. You are interested in whole numbers.

    And I encourage you to not do this using brute force (the way I showed). There is a much more elegant way involving the averages of a set of numbers [Guass did this]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( x < y )
        if odd
            add to this sum
        else
            add to that sum
    If I understand what you're saying that is.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM