Below you have two different codes. The difference between the two is simply for the variable sum, sum was initialized compared to giving sum the value 0 sum =0;
#include <iostream>
using namespace std;
int main(){
    
    //This code will display first ten numbers and get the sum of it
    cout << "The natural numbers are: " << endl;
     for(int i=1; i<=10; i++)
     {
         cout << i << " ";
         
     }
    cout << endl;
// variable was initialized 
    int sum;
    cout << "The sum of first 10 natural numbers: ";
    for(int i=1; i<=10; i++)
      {
          sum = sum +i;
      }
    cout << sum;
    cout << endl;
    
}
This Code outputs:
The natural numbers are: 1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers: 32821
Program ended with exit code: 0
#include <iostream>
using namespace std;
int main(){
    //This code will display first ten numbers and get the sum of it
    cout << "The natural numbers are: " << endl;
     for(int i=1; i<=10; i++)
     {
         cout << i << " ";
         
     }
    cout << endl;
// here I gave the value 1... this time it worked
    int sum =0;
    cout << "The sum of first 10 natural numbers: ";
    for(int i=1; i<=10; i++)
      {
          sum = sum +i;
      }
    cout << sum;
    cout << endl;
    
}
This Code outputs:
The natural numbers are: 1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers: 55
Program ended with exit code: 0
Why does the code do this? Can someone please explain to me why they gave me two different sums?
 
     
     
     
    