please explain the working of this code( as a part of my code ) and the problems involved in it
for(int l=0;l<10;l++){
if(no==0||no==1)break;
if(no%a[l]!=0){c++;}
if(no==a[l]){flag=1;break;}
if(no%a[l]==0)break;
}if(c==10||flag==1){cb++;i=j+1;break;}
Please tell me the problem in this code
Hello @Ignitor,
Considering.
no: to be the number that you want to check for CB number, and
a[]: an array containing the list of CB numbers given in the question
- first if-condition will break the execution of loop if no is 0 and 1.
- second if-condition will increment the count for CB number if it is not divisible by any of the number given in the array.
- third if-condition will break the loop if it belongs to a[]
- fourth if-condition will break the loop no is divisible by an ith element of a[].
- This if the condition will never satisfy.
This is logic is not gonna work.
You need to think of a better approach to solve the problem.
You can also refer to the following approach:
Approach:
- check for all sub-strings of the given string of digits, starting from all strings of length 1 and then gradually checking for the strings of increasing size:
1.1. check if the sub-string is CB number: for this, create a function
1.1.1. if sub-string is 1 or 0 return false.
1.1.2. if sub-string is any of the {2,3,5,7,11,13,17,19,23,29}, then return true.
1.1.3. if sub-string is divisible by any of the {2,3,5,7,11,13,17,19,23,29}, then return false.
1.1.4. return false
1.2. now if it is a substring:
1.2.1. call a function that marks the index as visited for indexes which are part of that CB number.
1.3. increment the count for CB number.
2.Print the count
Hope, this would help.
If you still have any issue, feel free to ask.
Give a like if you are satisfied.