Table of Contents
Example
Output
Home Backend Development C++ Translate the following content into Chinese: In C programming, find the sum of numbers within N that can be divided by 2 or 5

Translate the following content into Chinese: In C programming, find the sum of numbers within N that can be divided by 2 or 5

Sep 20, 2023 am 08:25 AM
Divisible Sum programming c programming

Translate the following content into Chinese: In C programming, find the sum of numbers within N that can be divided by 2 or 5

The sum of n natural numbers that can be divided by 2 or 5 can be found by finding the sum of all natural numbers within N that can be divided by 2 and the sum of all natural numbers that can be divided by 5 within N. Come and find out. Subtract these two sums by the sum of natural numbers within N that are divisible by 10, and that's what we want. This method is an efficient way to find the sum of large values ​​of n.

Some of you must be thinking of using loops and conditional statements and then adding up all numbers divisible by 2 or 5, but this approach is inefficient as it has time complexity of order n . This means that for larger values ​​of n, the program will run the loop n times. And doing it this way makes the program heavier.

Find the formula for the sum of n natural numbers that can be divisible by 2

Sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2
Copy after login

Find the formula for the sum of n natural numbers that can be divisible by 5

Sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2
Copy after login

Find the formula for the sum of n natural numbers that can be divisible by 5 Formula for the sum of natural numbers divisible by 10

Sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2
Copy after login

Expected output

Sum = Sum2 + Sum5 - Sum10
Copy after login

Example

#include <stdio.h>
int main() {
   int n = 25;
   long int sum2, sum5, sum10;
   sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2;
   sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2;
   sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2;
   long int sum = sum2 + sum5 - sum10;
   printf("Sum is %d", sum);
   return 0;
}
Copy after login

Output

Sum is 201
Copy after login

The above is the detailed content of Translate the following content into Chinese: In C programming, find the sum of numbers within N that can be divided by 2 or 5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Detailed explanation of integer division operations and remainder calculation methods in Go language Detailed explanation of integer division operations and remainder calculation methods in Go language Mar 23, 2024 pm 06:00 PM

Detailed explanation of integer division operations and remainder calculation methods in Go language. In Go language, integer division operations and remainder calculations are common mathematical operations. This article will introduce how to perform integer division operations and remainder calculations in the Go language, and provide specific code examples. Integer division operation In the Go language, the / symbol is used for integer division operation. Integer division operation refers to taking the quotient of dividing two numbers. The result is the integer part, that is, the decimal part is ignored and no rounding operation is performed. Integer division operations are often used to calculate the integer quotient after division. Code example: package

Absolute tuple sum in Python Absolute tuple sum in Python Sep 12, 2023 pm 07:37 PM

In Python, tuples are immutable sequences that can store multiple elements of different types. They are often used to represent collections of related values. Tuple summation involves adding the corresponding elements of two or more tuples to produce a new tuple. However, in some scenarios, it may be necessary to calculate the absolute sum of elements instead of the traditional sum. In this blog post, we will explore how to perform absolute tuple sums in Python. Traditional Tuple Sum Before we delve into absolute tuple sum, let’s first understand how to do traditional tuple sum. Given two tuples of the same length, we can use a simple Python loop or list comprehension to calculate the sum of the corresponding elements −deftuple_sum(t1,t2):

Do you know how to sum a Word table? Do you know how to sum a Word table? Mar 21, 2024 pm 01:10 PM

Sometimes, we often encounter counting problems in Word tables. Generally, when encountering such problems, most students will copy the Word table to Excel for calculation; some students will silently pick up the calculator. Calculate. Is there a quick way to calculate it? Of course there is, in fact the sum can also be calculated in Word. So, do you know how to do it? Today, let’s take a look together! Without further ado, friends in need should quickly collect it! Step details: 1. First, we open the Word software on the computer and open the document that needs to be processed. (As shown in the picture) 2. Next, we position the cursor on the cell where the summed value is located (as shown in the picture); then, we click [Menu Bar

Find numbers that are not divisible by any number in a range, using C++ Find numbers that are not divisible by any number in a range, using C++ Sep 13, 2023 pm 09:21 PM

In this article, we will discuss the problem of finding numbers between 1 and n (given) that are not divisible by any number between 2 and 10. Let us understand this with some examples - Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharenotdivisible. Solved Simple method if

In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64. In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64. Sep 01, 2023 pm 08:17 PM

In this tutorial, we will write a program that checks whether a given binary number is divisible by 64. We are given a binary number and we can remove some bits to make it divisible by 64. After removing the bits, if the number is divisible by 64, print Yes, otherwise print No. The method we will use is very simple. Let's look at the steps to solve the problem. Initialize a binary number in string format. Iterate over the given binary numbers. Count the number of zeros. If a binary number contains greater than or equal to 6 zero bits, the number is divisible by 64. Prints whether the given binary number is divisible by 64. Example Let's look at the code. #include&lt;bits/stdc++.h&gt;usi

Find the sum of an arithmetic sequence of staggered signs Find the sum of an arithmetic sequence of staggered signs Sep 16, 2023 pm 05:01 PM

An arithmetic progression (AP) is a sequence of numbers in which the difference between two consecutive terms is the same. The difference is calculated by subtracting the second term from the first term. Let us understand AP with an example sequence, 5,7,9,11,13,15,... The tolerance (d) of this arithmetic series is 2. This means that each subsequent element differs from the previous element by 2. The first item (a) in this sequence is 5. The general formula to find the nth term is a{n}=a+(n-1)(d) In this problem we are given an AP and we need to find the sum of a series of alternating signed squares, the series will be As shown below, a12-a22+a32-a42+a52+... Let us take an example for clearer understanding&amp;

Find the sum of elements in an array using the array_sum() function in PHP Find the sum of elements in an array using the array_sum() function in PHP Nov 18, 2023 am 11:20 AM

Title: Find the sum of array elements using the array_sum() function in PHP. PHP is a widely used server-side scripting language. It provides numerous built-in functions that can simplify the development process and improve efficiency. Among them, the array_sum() function is a very practical function that can be used to calculate the sum of elements in an array. In this article, we will learn how to use the array_sum() function and give specific code examples. First, we need to understand the use of array_sum() function

How to automatically sum totals in excel How to automatically sum totals in excel Mar 20, 2024 pm 12:20 PM

For users who often use excel tables, the automatic sum function is a very simple operation, and it can automatically sum to several decimal places according to our needs, which is much more convenient than manually pressing the calculator. For novice users, you still need to learn how to automatically sum totals in Excel from scratch. Let’s take a look at the steps: Excel automatic sum: First, we need to add the numbers in cells A1 and B1, and Display the results in cell C1. To do this, first enter the numbers you want to add in cells A1 and B1. Next, select cell C1 and enter the following formula: `=A1+B1`. After pressing the Enter key, cell C1 will display the sum of the numbers in cells A1 and B1.

See all articles