943,718 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8391
  • C++ RSS
Aug 17th, 2004
0

Sum of digits

Expand Post »
I want to know how to write a program to obtain sum of the first and last digit of a number?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
smithag261 is offline Offline
6 posts
since Jul 2004
Aug 18th, 2004
0

Re: Sum of digits

Sure...

VB Code:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 115
Solved Threads: 5
Junior Poster
Toba is offline Offline
192 posts
since Jun 2004
Aug 18th, 2004
0

Re: Sum of digits

or, in C++,
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Nov 4th, 2010
-1
Re: Sum of digits
#include<iostream.h>
#include<conio.h>
#include<string.h>

class sod
{
int a,b,c,i,num,num1,sum,n;
public:
void getdata()
{
cout<<"\tProgram for sum of digits\n\n\n";
cout<<"enter a one, two, three, four or five digit number ";
cin>>num;
}
void sumod()
{
sum=0;
n=0;
num1=num;
while(num1>0)
{
b=num1/10;
num1=b;
n++;
}

for(i=0;i<n;i++)
{
a=num%10;
b=num/10;
num=b;
sum=sum+a;
}

}
void putdata()
{
cout<<"\nSum of the digits of the above number is = "<<sum;
}
};

int main()
{
sod s1;
clrscr();
s1.getdata();
s1.sumod();
s1.putdata();
getch();
return 0;
}
Reputation Points: 7
Solved Threads: 0
Newbie Poster
keshalidani is offline Offline
2 posts
since Nov 2010
Nov 4th, 2010
-1
Re: Sum of digits
C++ Syntax:
for 4 digit:

#include<iostream.h>
#include<conio.h>

class sod
{
int a,b,c,i,n,sum;
public:
void getdata()
{
cout<<"enter a four digit number ";
cin>>n;
}
void sumod()
{
sum=0;
for(i=0;i<4;i++)
{
a=n%10;
b=n/10;
n=b;
sum=sum+a;
}
}
void putdata()
{
cout<<"\nSum of the digits of the above number is = "<<sum;
}
};

int main()
{
sod s1;
clrscr();
s1.getdata();
s1.sumod();
s1.putdata();
getch();
return 0;
}
Reputation Points: 7
Solved Threads: 0
Newbie Poster
keshalidani is offline Offline
2 posts
since Nov 2010
Nov 4th, 2010
0
Re: Sum of digits
Do you know we have a little button called [CODE] for this?
Reputation Points: 82
Solved Threads: 34
Posting Whiz in Training
hiddepolen is offline Offline
291 posts
since Oct 2010
Nov 4th, 2010
0
Re: Sum of digits
Or in Javascript...
C++ Syntax (Toggle Plain Text)
  1. function sumDigits(val) {
  2. if (!isNaN(val)) {
  3. val = parseInt(val, 10) // ensure integer
  4. if (val>9) {
  5. var sum = val%10 // last digit number
  6. while (val>10) { // searching for the first number
  7. val = parseInt(val/10, 10)
  8. }
  9. alert(sum+val) // now what left is the last + first digit
  10. }
  11. else { alert(val) }
  12. }
  13. else { alert(val+" is not a number") }
  14. }
Last edited by Taywin; Nov 4th, 2010 at 12:25 pm.
Reputation Points: 229
Solved Threads: 238
Posting Virtuoso
Taywin is offline Offline
1,723 posts
since Apr 2010
Nov 4th, 2010
0
Re: Sum of digits
If n is a non-negative integer:

The last digit of n is n%10.

If n < 10, the first digit of n is n.

If n >= 10, the first digit of n is the first digit of n/10.

This is all you need to know in order to solve the problem.
Reputation Points: 359
Solved Threads: 109
Practically a Master Poster
arkoenig is offline Offline
680 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MFC Mutiple Document Type Application.
Next Thread in C++ Forum Timeline: Stock Market





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC