DIR
DIR

Reputation: 57

how to find the position of the factor number in c++

here I am finding the position of factor number

for example

when user enter runtime 10

1 2 5 10

and when the user enters the position of factor 3

then return the 3rd factor means 5

for example

when user enter the runtime 20

1 2 4 5 10 20

and when the user enters the position of factor 5

then return the 5th factor means 10

I create one program but one thing is remaining to find the factor number position


#include<iostream>

using namespace std;

int Factor(int n) {
    
    int i,j=0;
    
    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            //j=i;            //here i am storing the factor number
            cout << i ;
        }
    }
    return i;
}

int main()
{
    int x;
    
    cout << "Enter your factor number:";
    cin >> x;
    
    Factor(x);

    return 0;
}

Live program link: https://onlinegdb.com/HJ4yAyXZw

now what I am trying to do find the factor number position

how to implement the logic of find the position of factor number

help

Upvotes: 3

Views: 503

Answers (2)

Deepak Patankar
Deepak Patankar

Reputation: 3272

In the for loop, you can maintain a count of the factors found and if the count reaches the position then the factor at that position is the answer you want.

You can either return this answer there itself or can store it in a local variable and return it at the end of the function.

The code explaining this logic:


#include<iostream>

using namespace std;

int Factor(int n, int factorPosition) {
    int i,j=0;
    int factorsSoFar = 0;
    int factor = -1;
    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            factorsSoFar++;          //here i am storing the factor number
            if(factorsSoFar == factorPosition){
                factor = i;
                cout << i ;
                break;
            }
        }
    }
    return factor;
}

int main()
{
    int x, position;
    
    cout << "Enter your factor number: ";
    cin >> x;
    
    cout << "Enter the factor position: ";
    cin >> position;
    
    int factor = Factor(x,position);
    if(factor == -1){
        cout<<"No factor exists for position "<< position<<endl;
    }else{
        cout<<"Factor is: "<<factor<<endl;
    }
    return 0;
}

Upvotes: 2

mo1ein
mo1ein

Reputation: 605

In for loop you can count index (position) of factors . I use bits/stdc++.h because it's faster than iostream . I tried to solve this problem in simple way and saw your int j=0 variable is unused .
So This is my way :

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

void Factor(int n , int position){

    int i,count=0;

    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            count++;            // count position
                                //here i am storing the factor number
            if (count==position){
                cout<<"factor of position "<<position<<" is "<<i<<"\n";
                break;
            }
        }
    }
    if (position>count)
        cout<<"no factor for this position\n";
}

int main()
{
    int factor,position;

    cout << "Enter your factor number: ";
    cin >> factor;

    cout << "Enter the factor position: ";
    cin >> position;

    Factor(factor,position);
}

Upvotes: 3

Related Questions