summ of the first 20 odd numbers from 1 to 100

I am trying to store and find the sum of the first 20 odd numbers from 0 to 100 but each time i run the program i get a black screen. Thanks for your time.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 #include <iostream>

using namespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);

int main()
{

    int odd[20];//array to sotre first 20 odd numbers

for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
    {
do{//do while loop to store only the first 20 odd numbers
if(i % 2 !=0)
{cin>>odd[i];}//store in the array

  } while (odd[i]<20);

    }


    print(odd);//display the result from the void function
    return 0;
}


int oddSum(int a[])//function to find the sum
{
int sum =0;

for(int i = 0; i < 20; i++)
    {sum = sum + a[i];}

return (sum);
}


void print (int a[])//function to display the results
{
    cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
1
2
if(i % 2 !=0)
{cin>>odd[i];}//store in the array 
cin is for console input, the user needs to write the number.
¿did you realize where are you trying to store?
hmm it had worked in another code that i wrote so i tried it here i had odd[i]=i before but it didn't work.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);

int main()
{

    int odd[20];//array to sotre first 20 odd numbers

for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
    {
do{//do while loop to store only the first 20 odd numbers
if(i % 2 !=0)
{odd[i]=i;}//store in the array

  } while (odd[i]<20);

    }


    print(odd);//display the result from the void function
    return 0;
}


int oddSum(int a[])//function to find the sum
{
int sum =0;

for(int i = 0; i < 20; i++)
    {sum = sum + a[i];}

return (sum);
}


void print (int a[])//function to display the results
{
    cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
Last edited on
Indent your code properly
1
2
3
4
5
6
7
8
for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
{
   do{//do while loop to store only the first 20 odd numbers
      if(i % 2 !=0){
         odd[i]=i;//store in the array
      }
   }while (odd[i]<20);
}
Your code makes no sense, do a desk test.
I did some editing and it runs but the answer is incorrect:(

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
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);

int main()
{

    int odd[100];//array to store odd numbers

        for(int i = 0; i < 100; i++)//loop to count to 100
    {
if(i % 2 !=0)//if statement to store odd numbers if it is odd
             {odd[i]=i;}//store in the array
    }


    print(odd);//display the result from the void function
    return 0;
}


int oddSum(int a[])//function to find the sum
{
int sum =0;

for(int i = 0; i < 20; i++)
    {sum = sum + a[i];}

return (sum);
}


void print (int a[])//function to display the results
{
    cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
Last edited on
You only store odd numbers at odd indices in your odd array. The rest of the elements have unspecified contents. So, if you print the first 20 elements of your odd array, you (would) print a bunch of junk.

[Edit: And there is no real reason to store the odd numbers:]

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    unsigned oddsTotal = 0 ;

    for ( unsigned oddsFound = 0; oddsFound < 20; ++oddsFound )
        oddsTotal += oddsFound*2 + 1  ;

    std::cout << "The sum of the first 20 odd numbers is: " << oddsTotal << '\n' ;
}
Last edited on
Storing the odd numbers is part of the question.
Last edited on
Change your main to this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{

    int odd[100];
    int j = 0;
    for(int i = 0; i < 100; i++)
    {
        if(i % 2 !=0)
        {
            odd[j]=i;
            cout << i << endl;
            if(j == 20)
                break;
            j++; 
        }
    }


    print(odd);//display the result from the void function
    return 0;
}

and you can also use this approach.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    cout << "Enter the number of odd numbers to be summated from 1\n : ";
    int limit;
    cin >> limit;
    int oddSum = 0;
    int oddnum = 1;
    vector<int> odds;
    odds.reserve(20);
            odds.push_back(oddnum);
    for(int i = 0; i < limit; i++)
    {
            oddSum+=oddnum;
            oddnum+=2;
            odds.push_back(oddnum);
    }
    cout << oddSum;
    return 0;
}
Last edited on
Wow thanks i really appreciate all your help it worked :)
Topic archived. No new replies allowed.