Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
You are given a function,
int DivisibilityByEleven(int num);

The function accepts an integer 'num' as input. You have to implement the
function such that it returns the number of contiguous integer fragments of
'num' that are divisible by 11. Contiguous integer fragments of a number,
say 1273, are:
1, 2, 7, 3, 12, 27, 73, 127, 273 and 1273

Example
Input
1215598
Output
4
Explanation
55, 121, 12155 and 15598 are contiguous fragments of the number 1215598 that are divisible by 11

Sample Input
55

Sample Output
1



please explain me this code

What I have tried:

#include<stdio.h>
int DivisibilityByELeven(int num)
{
     int digit=0;
     int temp;
     for(int i=num;i>0;i=i/10)
     {
     	digit++; //Counting the number of digits in the parameter/
	 }
	 printf("%d\n",digit);
     if(digit==2)
     {
     	if(num%11==0)
		 {
		 	return 1;
		 }
	 }
	 else
	 {

	 int conti=0; //count variable
	 int div=10;
      temp=num;
      int count=0;
	 while(temp>0)
	 {
	 	while(count
Posted
Updated 30-Aug-22 1:53am
Comments
Member 15752290 30-Aug-22 8:06am    
Line 30 to 38 please explain
Greg Utas 30-Aug-22 8:07am    
Your code ends somewhere in the middle. The fragment that you provided only handles 2-digit numbers, and it's unclear whether it handles a 1-digit number correctly.
Member 15752290 30-Aug-22 8:12am    
Yes it handles only 2digit number
Greg Utas 30-Aug-22 8:25am    
Where are lines 30 to 38? The code fragment doesn't even have that many lines. And the 2-digit case is trivial. num % 11 finds the remainder (modulo) of num when divided by 11. If it's 0, then num is evenly divisible by 11.
jeron1 30-Aug-22 9:58am    
So you want someone here to explain code you wrote?

1 solution

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

And ... if you don't understand code, you won't learn anything useful by finding it on the internet and hoping. You learn skills by doing, not looking - you can watch as much of the Tour de France as you can find, but you still won't be able to ride a bicycle!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900