Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Alternate Digits sum
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-14-2012, 04:20 AM #1 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Rep: Alternate Digits sum a function that takes an integer as a parameter and adds the alternate digits and every 3rd digit in the integer? The simple addition of digits of number is like this way Can any one help me with the changes or modifications we need to do in this #include int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10){ r=num%10; sum=sum+r; } printf("Sum of digits of number: %d",sum); return 0; }
 09-14-2012, 04:38 AM #2 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 613 Rep: Could you post a simple example of some numbers and their expected results? Your explanation is not so clear...
 09-14-2012, 05:02 AM #3 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: For eg: given an integer 158176 your program should add - 1,8,7 which are in the position 0,2,4 of the integer AND - 5,1,6 which are in position 1,3,5 of the integer AND - 8,6 which are in every 3rd place in the integer. So the answer will be 1+8+7=16, 5+1+6=12 and 8+6=14. The output will be 16,12,14
 09-14-2012, 06:05 AM #4 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 613 Rep: Instead of using multiple divisions like you're doing right now, why don't you convert the number to a string and then process it character by character, using an index as indication of whether you're pointing to an even position or an odd position or a position divisible by 3? Code: ```# include # include int main (){ int num, i, size; int nOdd, nEven, nThree; char buf[30]; nOdd = nEven = nThree = 0; printf ("Enter a number:"); scanf ("%d", &num); size = sprintf(buf,"%d", num); for (i = 0; i < size; i++){ if ( i % 2 == 0 ) nEven+=buf[i] - '0'; else nOdd+=buf[i] - '0'; if ((i+1) % 3 == 0 ) nThree+= buf[i] - '0'; } printf ("Initial number = %d\neven = %d\nodd = %d\nthree = %d", num, nEven, nOdd, nThree); return EXIT_SUCCESS; }``` itoa() is not in standard C (maybe you've seen it on other systems), so I had to resort to sprintf for the conversion. Last edited by 414N; 09-14-2012 at 06:06 AM.
 09-14-2012, 06:11 AM #5 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: your solution is fine but is it possible without using any arrays/strings use only integers - Use a single loop
 09-14-2012, 06:42 AM #6 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 613 Rep: Well, you could reuse the sample you posted but paying attention to a couple of things. In your code, you're extracting digits from the units upwards so, feeding 127 as num to the program you'll have: Code: ```|loop number| 1 | 2 | 3 | | num | 127 | 12 | 1 | | r | 7 | 2 | 1 |``` You need an index while looping which tells you the original position of the digit and this can be done if you extract the number of digits in num prior to running the loop (i.e. 3 in 127 case). This way you'll extract the correct even and odd digits.
 09-14-2012, 06:47 AM #7 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: well it will be a great help if u write the logic part only as i am beginner to c
 09-14-2012, 06:57 AM #8 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 613 Rep: I'll enrich your sample, but I'll leave some blanks for you to fill Code: ```#include int main(){ int num,r,i; int nOdd = 0, nEven = 0, nThree = 0; printf("Enter a number: "); scanf("%d",&num); for(i = DIGITS_IN_NUM; i >= 0; i--, num/=10){ r=num%10; if ( i % 2 == 0 ) nEven+=r; else nOdd+=r; if ((i+1) % 3 == 0 ) nThree+=r; } PRINT_OUTPUT }``` I left for you to fill in DIGITS_IN_NUM and PRINT_OUTPUT as those should be fairly easy to figure out.
09-14-2012, 07:07 AM   #9
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,203

Rep:
Quote:
 Originally Posted by 414N I left for you to fill in DIGITS_IN_NUM and PRINT_OUTPUT as those should be fairly easy to figure out.
1) Why is DIGITS_IN_NUM easy for a beginner to figure out?

2) Please don't provide direct answers (even incomplete ones) for homework assignments. Provide explanations, or answer specific questions, or point out the bugs in what the student has tried, or make suggestions. But don't do the assignment for them.

If you don't want to figure out DIGITS_IN_NUM in advance, and you only want to go through the sequence of digits once backwards, you still can easily compute the odd and even sums, you simply don't know which was which until you reach the end. That is OK because you don't need to know which was which until it is time to print them.

That idea is uglier for "every third" because you would need to compute at least two more sums as you run through the digits backwards. Then at the end, the one you print might be one of those two or might need to be computed from the total (from the odd/even sums) minus the two of three sums. It is a bit messy just to get the sum of every third digit. But it is a valid approach.

Another approach: Using a recursive function could give a simpler looking solution, but actually takes more space at run time than an array or string solution.

Of course you could also compute DIGITS_IN_NUM in advance.

Last edited by johnsfine; 09-14-2012 at 07:19 AM.

 09-14-2012, 07:15 AM #10 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: thanku so much for the helpp
09-14-2012, 07:22 AM   #11
414N
Member

Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 613

Rep:
Quote:
 Originally Posted by johnsfine 1) Why is DIGITS_IN _NUM easy for a beginner to figure out?
He already did that iteratively in his own code (likely without noticing it).
The OP said he's a beginner with C, but I thought he could know the math needed to determine that number without a loop on every digit to count them.
Quote:
 Originally Posted by johnsfine 2) Please don't provide direct answers (even incomplete ones) for homework assignments. Provide explanations, or answer specific questions, or point out the bugs in what the student has tried, or make suggestions. But don't do the assignment for them.
You're right, I just went into over-zealous mode.
Will try to restrain myself next time.

 09-14-2012, 10:02 AM #12 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: that logic is not working properly here is my code could you help me fixing it? #include int countDigits(int num); int main() { int count,i,num,nEven,nOdd,nThree,r; printf("Enter a number: "); scanf("%d",&num); count = countDigits(num); for(i = count; i >= 0; i--, num/=10) { r=num%10; if ( i % 2 == 0 ) nEven+=r; else nOdd+=r; if ((i+1) % 3 == 0 ) nThree+=r; } printf ("Initial number = %d\neven = %d\nodd = %d\nthree = %d", num, nEven, nOdd, nThree); } int countDigits(int num) { static int count=0; if(num!=0){ count++; countDigits(num/10); } return count; }
09-14-2012, 10:16 AM   #13
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,203

Rep:
Your countDigits function is not very good, because it can only be used once per run of the program and because it gives the wrong answer if the original number is zero. But neither of those are your serious problem.

You never initialized any of nEven, nOdd, or nThree which is a serious problem.

Quote:
 Code: `for(i = count; i >= 0; i--`
Think though how many times the loop will be executed with those instructions. Is that the number of times you want the loop executed? I know you copied that code from 414n, but you should understand what you are copying and not just trust it. (That isn't directly a serious error, because of the value of num during the "error". But I expect your instructor would still grade it as an error. I would.)

Quote:
 Originally Posted by Balvinder87 For eg: given an integer 158176 your program should add - 1,8,7 which are in the position 0,2,4 of the integer AND - 5,1,6 which are in position 1,3,5 of the integer AND - 8,6 which are in every 3rd place in the integer.
Think through what value i will have for each "position" of your 6 digit example. Is that what you intended? Hint start by thinking about what value i has on the first time through the loop when the code is working on the last digit. This is best fixed together with fixing the earlier non serious error.

Last edited by johnsfine; 09-14-2012 at 10:28 AM.

 09-14-2012, 10:23 AM #14 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 613 Rep: Please, put code inside [code][/code] tags, as it's a lot more readable. Also, are you indenting your code? What's not working properly? Adding to what johnsfine said:the test for digits in a divisible by 3 position needs to be adjusted (my fault) because now we're counting from "count" backwards, so you need to check that (i+1) isn't greater than (count); the main returns no int value.
 09-14-2012, 10:30 AM #15 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: It is not giving the actual values not even for odd,even positions

 Tags digits, sum