I'm trying to solve a problem that my friend gave me and i'm really stuck at it, I'm solving this now for about 1 whole day and my PC's about to melt.

Im trying to figure out how to add numbers in a range i.e I input the number 6 and it will give me the sum of 21 because 1+2+3+4+5+6 = 21,Number limit is 10

Im using DevC++ as Compiler.

Here's my code, it suck's cause it just multiplies the input by 2.

#include<iostream>
#include<conio.h>
using namespace std;

int x,y,sum;

main()
{

      cout<<"Enter Number:";
      cin >> x;
      if(x<=0 || x>=11)
      {
      cout<<"WRONG INPUT\n";
      }   
      else
      
      {     
          for(y=1;y<=x;y++)
         sum=y+x;
         
          {
             cout << sum ;
          }
      }
      
getch();
}

1 + 2 + 3 + ... + (N-1) + N == N * (N+1) / 2
for M>1, M + (M+1) + ... + (N-1) + N == N * (N+1) / 2 - M * (M-1) / 2

@vijayan

I don't get it. I just started using c++ for about a week or two. Thank's so much.

>> I don't get it.
!!

#include <iostream>
int main()
{
   int N ;
   std::cout << "Enter Number:" ;
   std::cin >> N ;
   if( N < 1 || N > 10 ) /* ... */ ;
   else std::cout << N * (N+1) / 2 << '\n' ; 
}

Thanks so much, I didn't get it at first because your using a different compiler i guess. I manage to make it now. Im working out on problems given by my friend to enhance my code writing. Hope I can get his next question right without asking help. Good day.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.