Jump to content

Freshmen IT student pls help me.. T_T

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Jerialou

Jerialou

    Newbie

  • Members
  • Pip
  • 3 posts

I was just annoying the problem that our teacher give it to me...
can someone help me?

this is the problem..how to get the sum of all even and odd numbers from 0 to N...:confused:

im so confusing..what loop am i using...and how to do it...
and what command to separate the odd and even?
because the given that been said to the prob is 0 to n... it means...
the number is unlimited...

our teacher give a clue to me...
the output would be like this
*ex.*
Enter the number:10<ex no.>

the sum of even number:30
the sum of odd number:25
***
:confused::confused: what will i do?
can anyone help me?



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
1) Do you know how to tell if a number is even or odd?
2) Do you know how to sum all numbers from 0 to N?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
n/2(a+l)

where n is the number of items, a is the first item and l is the last item. This probably isn't how they expect you to solve it though.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Use for loops, one starting at 0 and the other starting at 1. Increment them by two instead of one, and just add the sums.

#5
Jerialou

Jerialou

    Newbie

  • Members
  • Pip
  • 3 posts
thank you..dargueta,

i did it now...but...the even numbers only appeared..
>.<
but somethings missing..
can you help me?

here's my code..i made up..

*******

#include <stdio.h>
#include <conio.h>

int main()
{
int n,a,b,c;

printf("enter number:");
scanf("%d",&n);

for (a=1;a<=n;a++){

b=a+a+n;
}
printf("The Sum of the even number:%d\n",b);
getch ();
}

*****

do you think this is right?
or maybe im mistaken.. >.<
help me pls... :(

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Based on your response: You don't know how to sum a sequence of numbers. Your code to add the numbers 1 to N should look like this:

for (a=1;a<=n;a++) 
{
  b = b+a;
}

Also, a++ increments your counter by 1, not by 2 as dargueta suggested.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
You'll need two separate for loops, by the way.

#8
broncoslb

broncoslb

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
int n;
int oddSum = 0;
int evenSum = 0;
printf("Enter the n value: ");
scanf("%d",&n);

// To sum the odd numbers
for(i=1; i<=n; i+2)
    oddSum+=i;

// Same for even but start i=0

printf("Odd sum was %d\nEven sum was %d",oddSum,evenSum);


#9
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
@broncoslb: Please use the so-called code-tags, when you're posting some code.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Your code looks right, though.

#11
amischiefr

amischiefr

    Newbie

  • Members
  • Pip
  • 9 posts

for(int i = 1; i <= n; ) {

     oddSum += i++;

     evenSum += i++;

}

why write two loops?

#12
broncoslb

broncoslb

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
If n is an odd number then your evenSum would be off by +1.