11 Replies - 2786 Views - Last Post: 13 February 2010 - 11:39 AM Rate Topic: -----

#1 MagiCarpet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 08

Sum of Digits in C

Post icon  Posted 11 December 2008 - 08:34 AM

Hey guys, I was just wondering: If I wanted to ask a user for an integer input and then find the sum of the digits that were entered, would there be any way of going about doing that?
Is This A Good Question/Topic? 0
  • +

Replies To: Sum of Digits in C

#2 Quazzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 08:39 AM

I'm a bit confused. are u asking for the program where the input is something like "23" and the output is "2+3" or the program where the input is "2,3" and the output is "2+3" ? both are easy enough. plz just tell me which one ur looking for.
Was This Post Helpful? 0
  • +
  • -

#3 MagiCarpet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 08:41 AM

Yeah, like if the user put in 44, then there would be a message that says "The sum of the digits is 8." ....something like that.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

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.
Was This Post Helpful? 1
  • +
  • -

#5 Quazzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 08:54 AM

ha! KYA beat me to it. well thanks anyway KYA. I think mine had a small mistake.
Was This Post Helpful? 0
  • +
  • -

#6 MagiCarpet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 08:55 AM

Actually, it's working beautifully on this end the way it is. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Sum of Digits in C

Posted 11 December 2008 - 09:21 AM

Except that's C++, not C.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Sum of Digits in C

Posted 11 December 2008 - 09:58 AM

You could do the same thing with a char array.

I need to L2R though :(
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Sum of Digits in C

Posted 11 December 2008 - 10:00 AM

Indeed you could, with only a couple of changes. I just wanted the OP to be aware, in the event the assignment was C-specific.
Was This Post Helpful? 0
  • +
  • -

#10 MagiCarpet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 10:00 AM

Yeah thats ok...I converted to to C no problem.
Was This Post Helpful? 0
  • +
  • -

#11 cguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-December 08

Re: Sum of Digits in C

Posted 11 December 2008 - 10:08 PM

Well magiCarpet.i believe u should make use of '%' and '/' along with bitwise operators to shorten your code.

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

Was This Post Helpful? 0
  • +
  • -

#12 Guest_jameslee*


Reputation:

Re: Sum of Digits in C

Posted 13 February 2010 - 11:39 AM

thank u
Was This Post Helpful? 0

Page 1 of 1