Блог пользователя notStrongEnough

Автор notStrongEnough, история, 18 месяцев назад, По-английски

Today we will learn how can we find the devisor of a number in c++

First of all, What is the Divisor?

A divisor is a number that divides another number either completely or with a remainder. In division, we divide a number by any other number to get another number as a result. So, the number which is getting divided here is called the dividend. The number which divides a given number is the divisor.

For example

Similarly, if we divide 20 by 5, we get 4. Thus, both 4 and 5 are divisors of 20.

Now how can we find it using c++?

  • Step 1: Get a number n
  • Step 2: Run a for loop from 1 to n (Where i=1; i<=n; i++)
  • Step 3: Check whether i is a divisor or not. (n % i == 0) If it's return true then count the i is a divisor of this number.

Done! Very simple 3 steps. I can see the full c++ code here

#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
        if(n % i == 0){
            cout<<i<<" ";
        }
    }
    cout<<endl;
}

int main() {
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

I'll discuss in another editorial how to optimize the algorithm to find divisor

Полный текст и комментарии »

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится