• 3 Votes - 4 Average
• 1
• 2
• 3
• 4
• 5
 Again some tasks with while loop. :)
05-16-2016, 11:54
Post: #1
 Eagle_7 Member Posts: 73 Joined: Sep 2012 Reputation: 0 Honor: 0
Again some tasks with while loop. :)
 0
So i have to create a programm that takes a number from user and it adds odd numbers are equal to number that user enterd. This is verry hard dont understand anything.

Here is example.

Enter number : 7

What the programm must do is this.
If user enters 4 than answer must be 16.

Here are some examples.
1 + 3 + 5 + 7 = 16 (Here are 4 odd numbers all are sum together)
1 + 3 + 5 + 7 + 9 + 11 + 13 = 49( here are 7 and all sum together)
It must calculate the numbers sum using while loop.

Thanks. Any help would be apreciated.

Post: #2
 dage5 Super Moderator Posts: 996 Joined: May 2011 Reputation: 18 Honor: 0
RE: Again some tasks with while loop. :)
 0
In C11 I would do it like this:
Code:
```int input; scanf("%d",&input);  //get input int result=0;  //initialize to zero so there will not be some random number for(int i = 0,j = 1; i<input; i++,j = j+2)//i is number of cycles, j is next odd number to add {     result = result + j; } printf("Result is %d",result);```
Also I never knew that when you do this odd number adding the result seems to be equal to number of odd numbers to power of 2.
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100 and there is 10 odd numbers.
Code:
```int input; scanf("%d",&input); int result=0; for(register int i = 0,j = 1; i<input; i++,j = j+2) {     result = result + j; } printf("Result is %d input^2 is %d\n",result,input*input);```

So if nobody will look at your implementation you can cheat.

05-16-2016, 17:14
Post: #3
 Eagle_7 Member Posts: 73 Joined: Sep 2012 Reputation: 0 Honor: 0
RE: Again some tasks with while loop. :)
 0
Believe me ive been cheating the system many times
We have automated cheking system that enters random numbers and verify the right value or sum. Unfortunetly it shows what numbers are enterd and i create tons of if or else statments

if random == "1":
print ("i cheat the system ")

But this time i decided to be honorable programmer and try to complete the code in right way

Post: #4
 dage5 Super Moderator Posts: 996 Joined: May 2011 Reputation: 18 Honor: 0
RE: Again some tasks with while loop. :)
 0
We also had automated testing on few subjects but the tasks we had were hard to cheat if you wanted to score all points. It was just possible for few trivial functions.

You din't mention if it worked. I didn't read your first post properly and I missed it needs to be with while.
Code:
```int input; scanf("%d",&input); int i=0,j=1,result=0; while(i<input) {     result=result+j;     j=j+2;     i++; } printf("Result is %d",result);```

05-16-2016, 18:36
Post: #5
 Eagle_7 Member Posts: 73 Joined: Sep 2012 Reputation: 0 Honor: 0
RE: Again some tasks with while loop. :)
 0
im not sure what type of programming language are you using.
Python compiler seems not to recgonize it.

Post: #6
 dage5 Super Moderator Posts: 996 Joined: May 2011 Reputation: 18 Honor: 0
RE: Again some tasks with while loop. :)
 0
Yes it's C and I did not expect you to copy paste it but maybe look at what it does and adapt it for python. When I see python syntax there are few strange things but in general I can understand what is the logic behind it and write it in my language of choice.
My first attempt to write in Python ever in my life after 5 minutes of googling:
Code:
```input = //something to get input i = 0 j = 1 result = 0 while (i < input):    result=result+j    j=j+2    i=i+1 //something to write result on screen```

 « Next Oldest | Next Newest »

 » Powered by: betamaster.us «» Partner sites : vsee.us | knet.us |