Sum of digits

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 6
Reputation: smithag261 is an unknown quantity at this point 
Solved Threads: 0
smithag261 smithag261 is offline Offline
Newbie Poster

Sum of digits

 
0
  #1
Aug 17th, 2004
I want to know how to write a program to obtain sum of the first and last digit of a number?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 192
Reputation: Toba is an unknown quantity at this point 
Solved Threads: 5
Toba's Avatar
Toba Toba is offline Offline
Junior Poster

Re: Sum of digits

 
0
  #2
Aug 18th, 2004
Sure...

VB Code:
  1. Function sumFirstLast(number As Integer) As Integer
  2.  
  3. Dim numberString As String
  4.  
  5. numberString = Abs(number) & ""
  6.  
  7. sumFirstLast = Val(Mid(numberString, 1, 1)) + Val(Mid(numberString, Len(numberString), 1))
  8.  
  9. End Function
what? WHAT?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Sum of digits

 
0
  #3
Aug 18th, 2004
or, in C++,
  1. int sumFirstLast( int n )
  2. {
  3. char s[32];
  4. return ((n % 10) + (*itoa(n,s,10) - '0')); // itoa converts an int into a string
  5. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC