How to find divisor of a Number in c++

Правка en4, от notStrongEnough, 2022-12-10 13:39:48

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

Теги math, divisors, cpp, notstrongenough, beginners, mathematics, basic math

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский notStrongEnough 2022-12-10 13:39:48 0 (published)
en3 Английский notStrongEnough 2022-12-10 13:36:38 10
en2 Английский notStrongEnough 2022-12-10 13:35:01 87 Tiny change: 'editorial that how to optimize algorith' -> 'editorial how to optimize the algorith'
en1 Английский notStrongEnough 2022-12-10 13:31:35 1365 Initial revision (saved to drafts)