5.7 Boucle for
Good morning everyone! In this course, we will do an exercise which consists in creating a "for" instruction using numbers ranging from 1 to 1000 inclusive. Add up all the numbers that can be divided with both 3 and also with 5. For the numbers that meet the above conditions. Display the number. In other words that are divisible both by 3 and by 5. Then take a break from the loop, once you have found 5 numbers that meet the conditions. And after being out of the loop, display the sum of the numbers that meet the conditions. So you’re ready? Come on, let’s go! So, we will create two variables and initialize to 0. int count = 0 And int sum = 0. We create a loop, ranging from 1 to 1000, so "for" In parentheses " i = 1" i <= 1000 i++ Now what we want to do, this is to add up all the numbers that can be divided with both 3 and also with 5. After incrementing the variable "count". And then display the numbers we found. First, we will see if the numbers are divisible by 3 and by 5. We will do here, "if" Between parentheses and another parentheses. i % 3 = 0 and in brackets i % 5 =0 Here we check if the number is divisible by 3, then the operator "and" . to also check if the number is divisible by 5 So a reminder the operator "and". Here, is true if both conditions are "true". increment our variable count. So count ++ and sum += i Which will add the value as we browse And then we display it Number found. is equal more What we want to do now is to do a "if" that will test to see if we have actually found 5 numbers if In parentheses count = 5 break So, if we find 5 we will go out of the loop for After the loop for. We display the sum of the numbers. We will now execute the code. And as we see, we have 5 numbers 15, 30, 45, 60 and 75 which are divisible by 3 and 5. And then we have the sum of these numbers. That was it for this challenge Let’s meet at the next video!