Problem Description:
First 10 odd numbers include 1,3,5,7,...,19. Similar to Code 1 we have to add them all. Here we need a technique by which we can generate these numbers one at a time so that we can add each one of them. As learned earlier the Summation technique for adding them is using the code sum=sum+i where i should range from 1 to 19 generating only odd numbers one at a time. The technique that we will use to generate odd numbers is by dividing each number by 2, if divisible then its even producing reminder as 0, else its odd where reminder is not equal to 0. Now for apply this technique will can use the mod operator which gives us the remainder. Further we know that our range is limited from 1 to 19 in which we only need to search for odd numbers and then add them.
No comments:
Post a Comment