Question

R programming language. Write a R code that adds integer 1, 2, 3, …. Until the...

R programming language.

Write a R code that adds integer 1, 2, 3, …. Until the sum is over 1000. Remember, the upper bound is not known. The stop is handled by the sum when it is over 1000. Students use a) while and b) repeat to do the work

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please Refer below snapshot of code i have commented code as well please refer in case of any doubt please let me know in comment section , i would be happy to help you.

Here is code:-

A)Using while

----------------

#R version 3.3.2
# intializing num and sum
num <- 1
sum <- 0
# test to check if sum is less than 1000 and as soon as it is greater than 1000 loop should be false
while(sum < 1000){
  

num=num+1
sum = sum + num

  

}
print(sum)
print(num)

------------------------

B) using repeat

-----------------

#R version 3.3.2
  
num <- 1
sum <- 0

repeat{


num=num+1
sum = sum +num
if(sum>=1000){
  
break
}
}

print(sum)
print(num)

--------------------

1 #R version 3.3.2 2 # intial!zing num and sum 5 # test to check if sum is less than 1000 and as soon as it is greater than 11 #R version 3.3.2 3 num<1 Ling 6 repeat 7 Ne num-num+1 sum sum +num 11 if (sum-1000) 12 13 14 15 16 17 print (sum) 18 print

Explanation:-

when sum became greater than 1000 then while loop and repeat loop became false so in that time sum is 1034 and that time num value would be 45,

in mathematics we know that sum of n number formula that is:-

n(n+1)/2 if we will put n =45 than 45(46)/2=1035 let us take one number below ie. n =44 so 44(45)/2=990 so at num 45 it will became false first time .in case of doubt and query please do comment Thanks,

Add a comment
Know the answer?
Add Answer to:
R programming language. Write a R code that adds integer 1, 2, 3, …. Until the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT