Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to isolate digits of a number and add them using C++?

Status
Not open for further replies.

dannyvega

Newbie level 3
Joined
Nov 13, 2003
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
36
c++ math problem

I need to make a program that takes a number like 117
and adds the individual digits together (1+1+7) which equals 9
and divides it by 9 to see if the number is divisable by 9
but I don't know how to isolate each digit in order to add them together.
Can anyone help me
 

Re: c++ math problem

hint:

Yoy can divade 117 to 100 and get quotient part

117/100 = 1
---
117-1*100 = 17

17/10 = 1
---
17-1*10 = 7
---
 

Get it below :

hex - the number you have to deal with
divisor - number to test for division

fiction(char hex, char divisor)
{
int tint=0;
while( hex !=0 )
{
tint += hex%10;
hex = hex/10;
}
if( tint !=0 && (tint%divisor ==0) )
return OK;
else
return ERROR;
}
 

Re: c++ math problem

Thanks for the solutions guys but now I'm curious... is ther a possible way to divide a number like 117 into 3 digits (1, 1 & 7) and add them together (1+1+7) to get 9? in code that is.
 

Re: c++ math problem

I thought you could use a command like cin.get although cin.get is for use with characters so I guess you can't use that but is there a way to isolate numbers in that manner (setw) or something like that?

by the way if you want to email me directly it's dannyvegavilla@hotmail.com
 

What you are trying to have ccould be treated as BCD number operations . I am not sure , but I think there is no such operation in C standard library . But as workaround you can use the sprintf to print hex number to the charater string and each position in this character string will be ASCII coded decimal based initial number (of sprintf output will be decimal set).
SO after that it cacn be easy converted to whatever you need . But sprintf is not as short as this above .
If you are curious enough - some procecssors support BCD commands so using assembly you can directly work with such conversion , but check asm commands first .
 

Re: c++ math problem

dannyvega said:
Thanks for the solutions guys but now I'm curious... is ther a possible way to divide a number like 117 into 3 digits (1, 1 & 7) and add them together (1+1+7) to get 9? in code that is.

Maybe you didn't get it, but artem's code does what you ask.

Perhaps you understand another approach (in my opinion not as pretty though):

Code:
int digitSum(int n)
{
	char s[16], *p = s;
	int sum = 0;

	sprintf(s, "%d", n);

	while (*p)
	{
		int digit = *p++ - '0';


		sum += digit;

		// printf("%d ", digit);
	}

	// printf(" += %d ", sum);

	return sum;
}
 

U gave C code Gorilla The m8s already havin some difficulty with C++ BUT who knows...he might know C :).


-Mtuf- :D
 

u can use bit-and.
 

you can use CSTR to convert the number into text then use left(,,,) and right to split the numbers.
 

look maybe you have to take the number, and then do like this
N=N/10
and also the digit will be D=N%10
and do a loop while N#0
and then u can have the digits...its so easy
Mostafa
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top