0

Hy, i am writing a program to find "Common factors" my program finds factors correctly and also find common factors but do output some of them. Can anyone help me to find all the common factors? This is what I did.

#include<iostream> using namespace std; int main (){ int a,i,factors_1[100],memory_1; cin>> a; cout<<"Factors Are:" << endl; memory_1=0; for(i=1 ; i<=a; i++){ if(a%i==0){ factors_1[memory_1]=i; memory_1++; } } for(int z=0;z<memory_1;z++){ cout<< factors_1[z] << endl; } int m,j,factors_2[100],memory_2; cin>> m; cout<< "Factors Are:" << endl; memory_2=0; for(j=1; j<=m; j++){ if(m%j==0){ factors_2[memory_2]=j; memory_2++; } } for(int q=0;q<memory_2 ;q++){ cout<< factors_2[q] << endl; } int w,common[100],v; v=0; for(w=0;w<memory_1;w++){ if(factors_1[w] == factors_2[w]){ common[v] = factors_1[v] ; v++; } } cout<< "Common Factors Are" << endl; for(int o=0;o<v;o++){ cout<< common[o] << endl; } return 0; }

3rd Feb 2017, 9:15 PM
abadullah chaudhary
abadullah chaudhary - avatar
3 Answers
0
factors_1[w]==factors_2[w] factors_1 and factors_2 could have the same values but at the diferent places. For exaple 1,2,3 and 1,3,4. both of them has 3 but at the diferent plases. common[v]=factors_1[v] your loop use w variable it should be factors_1[w]. now it will copy corectly. and factors 1 and 2 could have different sizes.
3rd Feb 2017, 11:15 PM
ihor
0
what changes should I make?
4th Feb 2017, 4:56 AM
abadullah chaudhary
abadullah chaudhary - avatar
0
if u compile this code I will work correctly for some inputs but incorrect for others. like if u input 50, 50 .. I will give all the common factors butt if u give 10 and 20.. it will give only 1 common factors.
4th Feb 2017, 5:05 AM
abadullah chaudhary
abadullah chaudhary - avatar