You are using a version without Ads of this website. Please, consider donating:

### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` `````` #include 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:"<
 ``12`` ``````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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include 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:"<
Last edited on
 ``12345678`` ``````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:(

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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:"<
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:]

 ``1234567891011`` ``````#include 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 :
 ``12345678910111213141516171819202122`` ``````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.
 ``1234567891011121314151617181920212223`` ``````#include 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 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.

You are using a version without Ads of this website. Please, consider donating: