Good day C gurus
I was trying to code the biggest commum divisor for two number program but was confronted to a problem
here is my code by the way
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=100;
int b=34;
int diva[100]={0};// is the array in which i store the divisors or the numbuer a;
int divb[100]={0};// is the array in which i store the divisors or the numbuer b;
int max=1;
int counta=0;int countb=0 ;int i; int j=0;int k=0;
for(i=1;i<a;++i)
{
if(a%(a-i)==0)
{
diva[j]=a-i;
j++;
}
}
for(i=1;i<b;++i)
{
if(b%(b-i)==0)
{
divb[k]=b-i;
k++;
}
}
for(j=0;diva[j]!=0;++j)
{
printf(" the divisors of the number %i are %i\t",a,diva[j]);
++counta; // I needed to use a counter to know how many elements in my array diva I do have
}
for(k=0;divb[k]!=0;++k)
{
printf("\nthe divisors of the number %i are %i\t",b,divb[k]);
++countb; // I needed to use a counter to know how many elements in my array diva I do have
}
for(j=0;j<=counta;++j)
{
for(k=0;k<=countb;++k)
{
if(diva[j]==divb[k] && diva[j]>max )
max=diva[j];
}
}
printf(" the biggest commun divisor is:%d", max);
}
My code is fine, but during the coding process i got stuck with the following problems and i'm just trying to understand them:
- If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?
- Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?
As you can see, im using the counters (counta and countb) to figure out the necessary loops numbers.
Many thanks in advance!!