programming problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beck322
    New Member
    • Jan 2007
    • 12

    programming problem

    Hello everybody i need help with a problem please!!!
    I am new to this and just trying to figure my Homework out....
    Ok here it comes:

    Write a program that asks the user to enter an integer between 1 and 10, sums the squares of the integers from 1 to the number entered, and display the sum. Thus, if the user enters 5, the program should display the following:

    The sum of the squares of the integers from 1 to 5 is 55.


    I am sure for you guys it is not an issue i am just starting my c++ class .....

    please help me and give me some hints

    Thx

    Markus
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Steps:

    1. Ask the user for input between 1 and 10.
    2. Square each number from 1 to inputted number and add it to a holding variable.
    3. Display the sum.

    Which part are you having problems with?

    Comment

    • beck322
      New Member
      • Jan 2007
      • 12

      #3
      Originally posted by RedSon
      Steps:

      1. Ask the user for input between 1 and 10.
      2. Square each number from 1 to inputted number and add it to a holding variable.
      3. Display the sum.

      Which part are you having problems with?
      Ok i got that but i am not familiar with all the vocabulary yet
      that is my problem:
      1. cout<<" Enter integer between 1 and 10:"<< endl;
      cin >>integer>>;

      please be patient with me i am new to this

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by beck322
        Ok i got that but i am not familiar with all the vocabulary yet
        that is my problem:
        1. cout<<" Enter integer between 1 and 10:"<< endl;
        cin >>integer>>;

        please be patient with me i am new to this
        You are on the right track: (I fixed an error in your code)

        Code:
        cout<<" Enter integer between 1 and 10:"<< endl;
            cin >>integer;
        Keep going!

        Comment

        • beck322
          New Member
          • Jan 2007
          • 12

          #5
          Originally posted by RedSon
          You are on the right track: (I fixed an error in your code)

          Code:
          cout<<" Enter integer between 1 and 10:"<< endl;
              cin >>integer;
          Keep going!
          like that?
          int main()
          {
          int i;

          cout<< endl;
          cout<<" Enter integer between 1 and 10 : "<< endl;
          cin>>i

          return 0;
          }

          and how do i square all the integers?

          english is my second language too i am an international student from germany...

          Comment

          • beck322
            New Member
            • Jan 2007
            • 12

            #6
            Originally posted by beck322
            like that?
            int main()
            {
            int i;

            cout<< endl;
            cout<<" Enter integer between 1 and 10 : "<< endl;
            cin>>i

            return 0;
            }

            and how do i square all the integers?

            english is my second language too i am an international student from germany...
            ok i just forgot to put a semicolon after the i now it runs but i dont know how to
            square the inputs

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              In order to get i squared, you can use i*i.

              The best way to calculate this value would be to use a for... loop, like so:

              Code:
              int sum = 0;
              for (int j = 1; j <= i; j++) { // i is the user-defined variable
                 //Calculation to add i*i to sum here
              }

              Comment

              • beck322
                New Member
                • Jan 2007
                • 12

                #8
                #include "stdafx.h"
                #include <iostream>
                using namespace std;


                int main()
                {
                int total = 0,
                i,
                number;

                cout<< " Give me a number between one and 10:";
                cin>> number;
                for( i= 1; i <=number; ++i)
                {
                total=total+i*i ;

                }

                cout << "The sum of squares of the integers from 1 to " << number <<"is :"<< total<< endl;


                return 0;
                }

                I figured it out thank you!!!!!!

                Comment

                • beck322
                  New Member
                  • Jan 2007
                  • 12

                  #9
                  #include "stdafx.h"
                  #include <iostream>
                  using namespace std;


                  int main()// While loop same Exercise
                  {
                  int count=1,
                  number,
                  total=0;

                  cout<< " Give me a number between one and 10:";
                  cin>> number;
                  cout<< endl;
                  while(count <= number)

                  {
                  total +=count * count;
                  ++count;

                  }

                  cout << "The sum of squares of the integers from 1 to " << number <<"is :"<< total<< endl;


                  return 0;
                  }

                  Or like that!!!!!

                  Comment

                  • Roonie
                    New Member
                    • Mar 2007
                    • 99

                    #10
                    good!

                    do you need to perform checks on your input data? if so that will be another few steps . . . for instance, do you need to check to make sure that the input is not greater than 10?

                    (and your english is very good.)

                    Comment

                    Working...