Sum of Digits in C
Page 1 of 111 Replies - 2444 Views - Last Post: 13 February 2010 - 11:39 AM
#1
Sum of Digits in C
Posted 11 December 2008 - 08:34 AM
Replies To: Sum of Digits in C
#2
Re: Sum of Digits in C
Posted 11 December 2008 - 08:39 AM
#3
Re: Sum of Digits in C
Posted 11 December 2008 - 08:41 AM
#4
Re: Sum of Digits in C
Posted 11 December 2008 - 08:48 AM
string response;
int total = 0;
cout << "Enter a number:";
cin >> response;
for(int i = 0; i < response.size(); i++)
{
total += (int)response.at(i);
}
I haven't ran it though, the typecast may or may not be necessary.
#5
Re: Sum of Digits in C
Posted 11 December 2008 - 08:54 AM
#6
Re: Sum of Digits in C
Posted 11 December 2008 - 08:55 AM
#7
Re: Sum of Digits in C
Posted 11 December 2008 - 09:21 AM
#8
Re: Sum of Digits in C
Posted 11 December 2008 - 09:58 AM
I need to L2R though
#9
Re: Sum of Digits in C
Posted 11 December 2008 - 10:00 AM
#10
Re: Sum of Digits in C
Posted 11 December 2008 - 10:00 AM
#11
Re: Sum of Digits in C
Posted 11 December 2008 - 10:08 PM
below is a code to calculate sum of digits :
#include<stdio.h>
#include<conio.h>
int main()
{
int num=0,tot=0;
printf("Enter the number:");
scanf("%d",&num);
while(num)
{
tot = tot + (num%10);
num=num/10;
}
printf("The sum of Number is %d\n",tot);
getch();
return 0;
}
Hope this is useful to u..
This post has been edited by cguru: 11 December 2008 - 10:11 PM
|
|

New Topic/Question
Reply




MultiQuote





|