Write a program to accept an integer range and print the sum of number that are divisible by either 3 or 5Input Format:An integer range is given inputOutput Format:Print the calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:10Sample Output 1:33Sample Input 2:30Sample Output 2:225
Question
Write a program to accept an integer range and print the sum of number that are divisible by either 3 or 5Input Format:An integer range is given inputOutput Format:Print the calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:10Sample Output 1:33Sample Input 2:30Sample Output 2:225
Solution
To solve this problem, we can follow these steps:
- Read the input integer range from the user.
- Initialize a variable
sumto store the sum of numbers divisible by 3 or 5. - Iterate through each number in the range from 1 to the given input integer.
- Check if the current number is divisible by either 3 or 5 using the modulo operator (%).
- If the number is divisible by either 3 or 5, add it to the
sumvariable. - After iterating through all the numbers, print the value of
sumas the output.
Here is the code implementation in Python:
# Step 1: Read the input integer range
n = int(input("Enter the integer range: "))
# Step 2: Initialize the sum variable
sum = 0
# Step 3: Iterate through each number in the range
for i in range(1, n+1):
# Step 4: Check if the number is divisible by 3 or 5
if i % 3 == 0 or i % 5 == 0:
# Step 5: Add the number to the sum
sum += i
# Step 6: Print the calculated sum
print("Sum of numbers divisible by 3 or 5:", sum)
Now, if you run this program and provide the input range, it will calculate and print the sum of numbers divisible by either 3 or 5.
Similar Questions
Write a program to accept an integer N and print the sum of first N odd numbersInput Format:An integer range is given inputOutput Format:Print only calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:2Sample Output 1:4Sample Input 2:10Sample Output 2:100
Problem Statement:Write a program to find the sum of numbers in the given range.Input Format:Given two integers start and end ranges separated by SPACE.Output Format:Print the sum in the given range.Constraints1<=start < end <= 1000000NOTE : The calculated sum value might exceed integer range.Sample Input 1:10 20Sample Output 1:165Sample Input 2:1 100Sample Output 2:5050
Given an integer value, if it is divisible by 3 print “HI”, if it is divisible by 5 print “HELLO”. If it is divisible by both print “HIHELLO”, else print "NONE"Input Format:Enter an integer as a inputOutput Format:Follow the format as sample output - "HI"or "HELLO" or "HIHELLO" or "NONE"Constraints:1 <= INPUT <= 10^15Sample Input 1:55Sample Output 1:HELLOSample Input 2:12Sample Output 2:HI
Problem Statement:Find the sum of even numbers in the given range.Input Format:Accept two integer as inputOutput Format:Print the output as sum of even number available in the given rangeConstraints:1 <= INPUT <= 10^15Sample Input 1:10 20Sample Output 1:90Sample Input 2:30 33Sample Output 2:62
Output format :Print’1’ if n is a perfect number, else print the sum of the proper divisors of n.The output will be matched to the candidate’s output printed on STDOUTCode constraints :0< n <109Sample test cases :Input 1 :22Output 1 :14
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.