need code

how to write a programm in which we enter positive integer and it will give us the sum of all odd integers upto the entered positive integer..e.g if we enter 12 it will give us the sum of 1+3+5+7+11
Last edited on
Better post it in the job section.
http://www.cplusplus.com/forum/jobs/
closed account (48bpfSEw)
Analyse the problem and finding patterns...

Fact A: starting value is 1
Fact B: next value is 3

How can I calculate the next value from the first?

1 +2 = 3 .... oh that's right!

Is +2 the pattern to calculate all the odd numbers?

verification phase:

1 +2 = 3 ok
3 +2 = 5 ok
5 +2 = 7 ok

test compled successfull


Next question:

how can I get a user input in c++? there are these cin and cout. Which one works?

Next question:

how can I store a integer value in a variable?

there are key-words like int, bool, float.... Which one fits the task?

Next question:

how can I control the work-flow or in other words how can I start, continue and stop a loop?

there is a "if"-clause and "for", "while" -loops. Which can I use?



other methods to understand numbers:
https://www.wolframalpha.com/input/?i=1%2B3%2B5%2B7%2B9
Last edited on
Since it helps me playing around with code since I am in a programming 1 class, I went ahead and put this together...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

int main()
{
    //declare and initalize input
    int input = 0;
    
    //declare and initialize total
    int total = 0;
    
    //promt user to enter a whole number
    cout << "Enter a whole number: ";
    
    //get and store the user entered number in input
    cin >> input;
    
    //loop i starting at one
    //while i is less than or equal to input
    //each loop increment i by 2
    for(int i = 1; i <= input; i += 2)
    {
        //output each i
        cout << i << endl;
        
        //add each i to total
        total += i;
    }
    
    //output the total
    cout << total << " = Total" << endl;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.