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?
Freshmen IT student pls help me.. T_T
Started by Jerialou, Feb 12 2008 03:05 AM
13 replies to this topic
#1
Posted 12 February 2008 - 03:05 AM
|
|
|
#2
Posted 12 February 2008 - 09:08 AM
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?
2) Do you know how to sum all numbers from 0 to N?
#3
Posted 12 February 2008 - 01:37 PM
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.
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
Posted 12 February 2008 - 06:49 PM
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
Posted 13 February 2008 - 05:33 AM
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..
*******
*****
do you think this is right?
or maybe im mistaken.. >.<
help me pls... :(
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
Posted 13 February 2008 - 08:51 AM
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:
Also, a++ increments your counter by 1, not by 2 as dargueta suggested.
for (a=1;a<=n;a++)
{
b = b+a;
}
Also, a++ increments your counter by 1, not by 2 as dargueta suggested.
#7
Posted 13 February 2008 - 08:17 PM
You'll need two separate for loops, by the way.
#8
Posted 26 February 2008 - 10:10 AM
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);
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com
#9
Posted 26 February 2008 - 10:47 AM
@broncoslb: Please use the so-called code-tags, when you're posting some code.
#10
Posted 26 February 2008 - 06:07 PM
Your code looks right, though.
#11
Posted 27 February 2008 - 09:55 AM
for(int i = 1; i <= n; ) {
oddSum += i++;
evenSum += i++;
}
why write two loops?
#12
Posted 27 February 2008 - 10:00 AM
If n is an odd number then your evenSum would be off by +1.
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com


Sign In
Create Account

Back to top









