By Rahul


2020-08-01 13:28:56 8 Comments

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

2 comments

@Deepak Patankar 2020-08-01 13:36:05

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;
}

@JeJo 2020-08-01 13:42:27

You can avoid having factor too. Simply return i for the true case. See here: wandbox.org/permlink/vnlV8nDNpWbs9axE

@Deepak Patankar 2020-08-01 13:51:07

Hey @JeJo, thanks for the suggestion!!!

@Rahul 2020-08-01 14:06:32

@eepak Patankar thanks for your help but not proper output where you print the all factor number

@Rahul 2020-08-01 14:07:00

@DeepakPatankar please check my program onlinegdb.com/BJLlvxQ-P

@Deepak Patankar 2020-08-01 15:20:42

Hey @Rahul, What output you are expecting and what you are getting ?

@Rahul 2020-08-02 05:45:22

@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

@Deepak Patankar 2020-08-02 07:56:47

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.

@mo1ein 2020-08-01 14:19:12

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);
}

@Johnny Mopp 2020-08-01 14:36:55

@mo1ein 2020-08-01 16:26:06

@JohnnyMopp There is not much difference in this problem . check this link .

@Mehdi Mostafavi 2020-08-02 09:10:39

@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". "

@mo1ein 2020-08-02 16:47:53

@MehdiMostafavi OK. Thank's Mehdi good suggestion :))

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How do you set, clear, and toggle a single bit?

10 Answered Questions

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2408115 View
  • 4242 Score
  • 1 Answer
  • Tags:   c++ c++-faq

15 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

28 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

40 Answered Questions

79 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2188589 View
  • 3008 Score
  • 79 Answer
  • Tags:   c++ string split

19 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 533873 View
  • 1845 Score
  • 19 Answer
  • Tags:   c++ linux profiling

Sponsored Content