LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-14-2012, 04:20 AM   #1
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Rep: Reputation: Disabled
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<stdio.h>
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;
}
 
Old 09-14-2012, 04:38 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Could you post a simple example of some numbers and their expected results?
Your explanation is not so clear...
 
Old 09-14-2012, 05:02 AM   #3
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 09-14-2012, 06:05 AM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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 <stdio.h>
# include <stdlib.h>

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.
 
Old 09-14-2012, 06:11 AM   #5
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
your solution is fine but is it possible without using any
arrays/strings use only integers
- Use a single loop
 
Old 09-14-2012, 06:42 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
 
Old 09-14-2012, 06:47 AM   #7
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
well it will be a great help if u write the logic part only as i am beginner to c
 
Old 09-14-2012, 06:57 AM   #8
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
I'll enrich your sample, but I'll leave some blanks for you to fill
Code:
#include<stdio.h>
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.
 
Old 09-14-2012, 07:07 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by 414N View Post
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.
 
Old 09-14-2012, 07:15 AM   #10
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
thanku so much for the helpp
 
Old 09-14-2012, 07:22 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by johnsfine View Post
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 View Post
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.
 
Old 09-14-2012, 10:02 AM   #12
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
that logic is not working properly
here is my code
could you help me fixing it?

#include<stdio.h>

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;
}
 
Old 09-14-2012, 10:16 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 View Post
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.
 
Old 09-14-2012, 10:23 AM   #14
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
 
Old 09-14-2012, 10:30 AM   #15
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
It is not giving the actual values not even for odd,even positions
 
  


Reply

Tags
digits, sum


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get file permission as digits? cpthk Linux - Software 1 06-28-2010 05:16 PM
[SOLVED] How to test for a range of digits? btacuso Linux - Newbie 2 05-20-2009 07:34 PM
I need help finding out last 4 digits s_b Linux - Newbie 1 10-16-2008 08:16 AM
BASH - convert single digits to double digits. rickenbacherus Programming 7 05-07-2008 06:53 AM
grep and digits gawain Linux - Software 7 02-20-2008 02:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration