Post Reply 
 
Thread Rating:
  • 3 Votes - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Again some tasks with while loop. :)
05-16-2016, 11:54
Post: #1
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
Answer is : 49

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.

Find all posts by this user
Quote this message in a reply
05-16-2016, 14:49 (This post was last modified: 05-16-2016 15:32 by dage5.)
Post: #2
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. Tongue

Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2016, 17:14
Post: #3
RE: Again some tasks with while loop. :)
0
Believe me ive been cheating the system many times Big Grin
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 Big Grin

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


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

Find all posts by this user
Quote this message in a reply
05-16-2016, 17:39 (This post was last modified: 05-16-2016 17:39 by dage5.)
Post: #4
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);

Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2016, 18:36
Post: #5
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.

Find all posts by this user
Quote this message in a reply
05-16-2016, 18:51 (This post was last modified: 05-16-2016 18:53 by dage5.)
Post: #6
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

Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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