c++ looping problem

HI guys i need some help about looping..

its about creating a program that accepts number N and display the sum of all even numbers and sum of all odd numbers from 1 to N

any form of loops will do tnx..

Last edited on
So what have you tried?

We're here to help, but we're not going to do your homework for you.
this is wat i have so far..
and when i try to compile it its says "source file not compiled"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    int i=0;
    int n;
    cin>>n;
    while (i<=n)
    {
          
          if (n%2==0)
          {
                i=i+1;
                cout<<i<<endl;
                i++;
                }
                if (n%2!=0)
                {
                i=i+2;
                cout<<i<<endl;
                i++;
                }
                }
Your indentation is all messy looking. Show the full source and explain the actual compiler errors you're getting.

Also, the code you posted seems really redundant, you do the same thing in both if statements (edit: Oh, it's not exactly the same, but it still seems weird), and you probably don't even need to check for parity if you know that i will always start as an even number, zero.
Last edited on
it doesn't have any compiler errors..its just keeps on poping a window showing "source file not compiled"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main()

{
    int i=0;
    int n;
    cin>>n;
    while (i<=n)
    {
          
          if (n%2==0)
             {
                i=i+1;
                cout<<i<<endl;
                i++;
             }
          if (n%2!=0)
             {
                i=i+2;
                cout<<i<<endl;
                i++;
             }
    }
system ("pause");
return 0;
}
Are you actually trying to compile it, or are you just trying to run it without compiling?
Compiles and runs (not correctly) for me.

Line 12 and 18: i is your loop index (i.e. number from 1 to n). Here you're checking if the limit is even or odd. You should be checking if i is even or odd.

As I read the instructions, you need to keep separate even and odd sums.
Line 5:
1
2
3
4
5
6
7
8
9
10
11
{ int even_sum = 0;
  int odd_sum = 0;
// In place of line 14:
  even_sum += i;
// In place of line 20:
  odd_sum += i;
//  After line 23: 
  i++;
//  After line 24: 
  cout << "Sum of even numbers = " << even_sum << endl;
  cout << "Sum of odd numbers = " << odd_sum << endl;


Line 14 and 20: You don't want to increment i inside the if statements. You should increment i once at the bottom of the loop (after line 23).

Line 18: You don't need an if statement here. Just an else.
If a number is not even, it must be odd.


Last edited on
@AbstractionAnon

tnx sir..i tried putting those codes to my code and im half satisfied of the outcome..

the code is like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    int i=0;
    int even_sum=0;
    int odd_sum=0;
    int n;
    cin>>n;
    while (i<=n)
    {
          
          if (n%2==0)
             {
                even_sum+=i;
                cout<<i<<endl;
                
             }
          else
             {
                odd_sum+=i;;
                cout<<i<<endl;
                
             }
             i++;
    }
cout << "Sum of even numbers = " << even_sum << endl;
cout << "Sum of odd numbers = " << odd_sum << endl;



and i inputs 20 as n..and the output is like this..

0
.
.
.
20
sum of even of even numbers = 210
sum of odd numbers = 0

is there anyway to show both instead of just the even numbers?

or am i wrong somewhere?


thnx again sir

Line 9: You're taking the modulo of n. Modulo of n (the limit) is going to be the same for all iterations of your loop. You want to test if i is even or odd.
@AbstractionAnon

o ic ..anyway tnx sir..
its a big help.

:)
Topic archived. No new replies allowed.