I'm making a code that will add the squares of a range, ie input number is 4,the squares of 1+ square of 2+square of 3+square of 4 (1+4+9+16)= 30.

In my code I'm using the caret symbol for the square but it just multiply the number by 2 and not squaring it.

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

int x,sum;

main()
{

      cout<<"Enter Number: ";
      cin >> x;
      if(x<=0 || x>=11)
      {
      cout<<"WRONG INPUT\n";
      }   
      else
      
      {     
          
         x=x*(x^2)/2;
         sum=x;
          {
             cout <<"SUM: "<<sum;
          }
      }
      
getch();
}

>>In my code I'm using the caret symbol for the square but it just multiply the number by 2 and not squaring it.

Of course it doesn't work for you-- that isn't what the carret symbol does in c and c++. you have to use the * symbol and simply multiply the number with itself, such as

a_squared = a * a;

>> x=x*(x^2)/2;
This isn't doing what you want to do either. That whole line is pretty useuess to your assignment. What you need is a loop that counts from 1 to x and sums up the square of each of those numbers. You can't do that in just one line of code.

I tried using this as my condition but it just give me the sum of the range and not the sum of the squares in the range.

y=2;
         x=x*(x+1)/2;
         x*y;
         sum=x;

You are making that a lot more difficult than it really is. As I said before -- you have to use a loop; can't be done all at one time. And you are still trying to divide by 2 -- why ????? Divison is not necessary, only multiplication. such as

sum += x * x;

Now put the above in a loop which decrements x to 0 and you have the solution to the problem.

z=2;
                     y=x*z;
                     for(y=1;y<=x;y++)

I tried this loop but all it do was enumerate the numbers. ie : i input the number 5 i get the output:
5
1
2
3
4
5


Sorry Ancient I'm just a newbie in using the c++ language.

please post the exact code that you wrote. There is no way that the code you posted can produce that output. Just copy it from your program and paste it to here.

>> y=x*z;
And what exactly is that supposed to do? what is z? You can't just toss a bunch of random variables at a program and expect it to work. Programs are executed from top to bottom, so you must code it in the order you want it to be executed. The loop goes first and then the line that does the calculations. I already posted an example of how to do the calculation. If you want to use y as the loop counter, great. Then in the code I posted just replace x with y.

What's the purpose of the '^' (carot) symbol in C\C++?

Here's my code, what do i exactly need to change ?

#include<iostream>
#include<conio.h>

using namespace std;

int x,y,z;

main()
{
      cout<<"ENTER NUMBER: \n";
      cin>>x;
      if (x<=0 || x>=11)
               {
               cout<<"Wrong Input\n";
               } 
                 else
                 {
                z=2;
y=x*z;
                     for(y=1;y<=x;y++)
                    
                     {
                                      cout<<y*z<<"\n";
                                  
                     
                                      }
                 }
                 
getch();
}

Here's the code, It doesn't square the numbers it just add the numbers in the range. ie:
Input:4
Output: 10(1+2+3+4)

I need to get
Input :4
Output:30(1+4+9+16)

#include<iostream>
#include<conio.h>

using namespace std;

int x,y,z;

main()
{
      cout<<"ENTER NUMBER: \n";
      cin>>x;
      if (x<=0 || x>=11)
               {
               cout<<"Wrong Input
\n";
               } 
                 else
                 {
                
                
                     for(y=1;y<=x;y++)
                    z=2;
                   x += y * z;
                     {
                                      cout<<x<<"\n";
                                  
                     
                                      }
                 }
                 
getch();
}

Here's the code, It doesn't square the numbers it just add the numbers in the range. ie:
Input:4
Output: 10(1+2+3+4)

I need to get
Input :4
Output:30(1+4+9+16)

#include<iostream>
#include<conio.h>

using namespace std;

int x,y,z;

main()
{
      cout<<"ENTER NUMBER: \n";
      cin>>x;
      if (x<=0 || x>=11)
               {
               cout<<"Wrong Input
\n";
               } 
                 else
                 {
                
                
                     for(y=1;y<=x;y++)
                    z=2;
                   x += y * z;
                     {
                                      cout<<x<<"\n";
                                  
                     
                                      }
                 }
                 
getch();
}

Someone please help me T_T

Line 23: should be y * y

delete line 22 because z has no purpose

move the open brace on line 24 to immediately after line 21 so that line 23 and 24 become part of the same for loop.

replace the C language function on line 31 getch() with the C++ function cin.ignore().

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.