I am writing this in C, and i will forward my output into a different text file.
ID: 3605546 • Letter: I
Question
I am writing this in C, and i will forward my output into a different text file.
As supposed, I need to find a multiple of K,(k is a number, any number might be entered) and will read in some 50 to100 numbers in to an array "arr[]". My code here is to determine the first multiple of K and print it out, if there's none, I have to write there's no multiple of K. My program was great in finding the multiple of K and output its location, but when theres none, it keep printing out weird number. Please help fixing my code thank you
int d=k;//set int d equal to k, k is a constant
int u;
int n;
int nth;//the location of that multiple of k
int multi;//the multiple of K
for(u=0;u<length;u++){//length is the length of the array
if(arr[u]%d==0){//finding multiple of k
break;//terminate immediately once it find a multiple of k
}
else{
printf("There's no multiple of K ");
}
n=u+1;
}
nth=n+1;//
multi=arr[n];
printf("The first multiple of %d is %d. It is the %dst number ", k, multi, nth);
Explanation / Answer
I removed the else because you need to iterate the complete set of numbers and then come to conclusion.
I also took one variable found which will tell me whether the multiple is found or not
int d=k;//set int d equal to k, k is a constant
int u , found = 0;
int n;
int nth;//the location of that multiple of k
int multi;//the multiple of K
for(u=0;u<length;u++){//length is the length of the array
if(arr[u]%d==0){//finding multiple of k
found = 1;
break;//terminate immediately once it find a multiple of k
} //If terminates
} //Loop Terminate
if(! found) //Not found
printf("There's no multiple of K ");
n=u+1;
nth=n+1;//
multi=arr[n];
if(found)
printf("The first multiple of %d is %d. It is the %dst number ", k, multi, nth);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.