编写一个C ++程序,计算0到100之间的所有可被5整除的数字的总数

时间:2019-07-16 13:15:14

标签: c++

我希望使用C ++语言编写一个程序,该程序将找到0到100之间的数字并被5整除,然后求和它们的总数

#include <cmath> 
#include <iostream> 
using namespace std; 

// Returns sum of n digit numbers 
// divisible by '5' 
int totalSumDivisibleByNum(int n, int number) 
{ 
    // compute the first and last term 
    int firstnum = pow(10, n - 1); 
    int lastnum = pow(10, 5); 

    // sum of number which having 
    // n digit and divisible by number 
    int sum = 0; 
    for (int i = firstnum; i < lastnum; i++) 
        if (i % number == 0) 
            sum += i; 
    return sum;

0 个答案:

没有答案