3

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

DIR
  • 57
  • 8
  • 1
    In the for loop you can also maintain a count of the factors found and if the count reaches the position, then you can return that number. – Deepak Patankar Aug 01 '20 at 13:31

2 Answers2

3

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);
}
mo1ein
  • 535
  • 4
  • 18
  • 2
    FYI: [Why should I not #include \?](//stackoverflow.com/q/31816095) – 001 Aug 01 '20 at 14:36
  • @JohnnyMopp There is not much difference in this problem . check this [link](https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c) . – mo1ein Aug 01 '20 at 16:26
  • @mo1ein Try not to use this method at all. "I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers". " – Mehdi Mostafavi Aug 02 '20 at 09:10
  • @MehdiMostafavi OK. Thank's Mehdi good suggestion :)) – mo1ein Aug 02 '20 at 16:47
2

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;
}
Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35
  • 1
    You can avoid having `factor` too. Simply return `i` for the true case. See here: https://wandbox.org/permlink/vnlV8nDNpWbs9axE – JeJo Aug 01 '20 at 13:42
  • @eepak Patankar thanks for your help but not proper output where you print the all factor number – DIR Aug 01 '20 at 14:06
  • @DeepakPatankar please check my program https://onlinegdb.com/BJLlvxQ-P – DIR Aug 01 '20 at 14:07
  • Hey @Rahul, What output you are expecting and what you are getting ? – Deepak Patankar Aug 01 '20 at 15:20
  • @DeepakPatankar when user enter 10 then displays all factor-like 1 2 5 10 and then ask the position of factor 2 then position of factor is 2 – DIR Aug 02 '20 at 05:45
  • I removed the `cout << i ` line from your code, you can add it back and store your factors in an array, and then ask the user to input the position you want to print. – Deepak Patankar Aug 02 '20 at 07:56