how to print the sum of the remainder of an integer

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1178 Views - Last Post: 13 December 2012 - 07:26 AM Rate Topic: -----

#1 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

how to print the sum of the remainder of an integer

Posted 12 December 2012 - 02:47 PM

hi i need urgent help asap in C#

i need to print the sum of the remainder of an integer.
I know how to print the sum of an integer (ex: 1452 = 1+4+5+2)

but how to i add the remainder if my integer is 12.345?
how do i print 3+4+5?

URGENT HELP
Is This A Good Question/Topic? 0
  • +

Replies To: how to print the sum of the remainder of an integer

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6656
  • View blog
  • Posts: 30,975
  • Joined: 10-May 07

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 02:48 PM

Posted in C/C++ not C# (don't worry, I'll move it)
Posted a generic topic description (don't worry, I'll update it)
Was This Post Helpful? 0
  • +
  • -

#3 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 02:50 PM

View Postno2pencil, on 12 December 2012 - 02:48 PM, said:

Posted in C/C++ not C# (don't worry, I'll move it)
Posted a generic topic description (don't worry, I'll update it)

can you help me? i would really appreciate it
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11128
  • View blog
  • Posts: 19,070
  • Joined: 19-March 11

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 02:52 PM

You'll use the % and / operators (modulo and integer division). Modulo gives the remainder from division, and integer division gives the quotient only (no fractional result is appended). Together, the two will allow you to reduce a number one digit at a time.

12.345 is not an integer.
Was This Post Helpful? 0
  • +
  • -

#5 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 03:00 PM

sorry i meant a number.

right now i can print 123; 1+2+3=6
as follows:
main()
{
   int n, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
 
   printf("Sum of digits of entered number = %d\n",sum);
 
   return 0;
}




what i want to do is to enter a number 123.456 for example such that i can print ALSO 4+5+6

This post has been edited by no2pencil: 12 December 2012 - 03:04 PM
Reason for edit:: Added Code Tags

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6656
  • View blog
  • Posts: 30,975
  • Joined: 10-May 07

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 03:05 PM

& now you're not using code tags.

Pretend you are us. For as urgent as this is to you, we too request things from posters. Just a few, small requirements. Please meet us half way, that's all we ask.
Was This Post Helpful? 0
  • +
  • -

#7 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 03:10 PM

okay sorry let's see if i'm clear on what i'm saying.

i need to print a number for example: 123.456
i need a code that will print the sum of 123 and the sum of 456.

So again, if i have 123.456 my output should give me
1+2+3=6;
4+5+6=15.

sorry again if i'm not clear.
Was This Post Helpful? 0
  • +
  • -

#8 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 03:48 PM

Do you need C# help or C/C++ help? You state you need C# help but posted C/C++ code.

And to answer your question.

Take number mod 1, this should give you the decimal portion of the number.
while your number isn't zero
-- multiply number by 10
-- get integer of result
-- add to your total
-- take result from first step of this block mod 1
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11128
  • View blog
  • Posts: 19,070
  • Joined: 19-March 11

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 04:09 PM

You seem to have the integers case down pretty well. Can you see a way to adapt that solution to deal with the rational part?
Was This Post Helpful? 0
  • +
  • -

#10 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 04:14 PM

No unfortunately, i know i'm missing something just don't know what. You can see from what i posted i can find the sum of the a number for example 1234 but for a remainder i cant.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11128
  • View blog
  • Posts: 19,070
  • Joined: 19-March 11

Re: how to print the sum of the remainder of an integer

Posted 12 December 2012 - 04:18 PM

Perhaps it'll help if I tell you I really can't give you any further clues without just telling you the answer - it's that close to what you're already doing. That being said, you should probably work on it for a little while.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: how to print the sum of the remainder of an integer

Posted 13 December 2012 - 04:30 AM

Do you even really know what language you're supposed to be doing your assignment in? First you posted in the C/C++ forum with a question on C#. Then when you finally got around to posting your code after just pretty much begging for someone to give you the answers, you posted standard C code. Could it be that it's just some code you found on the net, and you're hoping that will qualify as enough of an effort that we'll feed you the code in C#?
Was This Post Helpful? 2
  • +
  • -

#13 YannB   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 12-December 12

Re: how to print the sum of the remainder of an integer

Posted 13 December 2012 - 05:01 AM

Dude relax don't be an ass.
I study in a foreign language I needed help and you all gave me your idead which I appreciate. The program is in C for your information and was already move to C purposely.
I understand now what the program must do ill open a new post which is more clear to understand what I'm looking for and honnestly do me a favor if its to judge or give me stupid comments like that by being a bitch like you are right now don't enter the next convo.
For the rest of real professionals: thank you for your help and sorry for not being clear with my statement and yes this is still urgent matter will be more specific in a few minutes




View PostJackOfAllTrades, on 13 December 2012 - 04:30 AM, said:

Do you even really know what language you're supposed to be doing your assignment in? First you posted in the C/C++ forum with a question on C#. Then when you finally got around to posting your code after just pretty much begging for someone to give you the answers, you posted standard C code. Could it be that it's just some code you found on the net, and you're hoping that will qualify as enough of an effort that we'll feed you the code in C#?

Was This Post Helpful? -2
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6330
  • View blog
  • Posts: 21,733
  • Joined: 05-May 12

Re: how to print the sum of the remainder of an integer

Posted 13 December 2012 - 06:10 AM

JackOfAllTrades is not being an ass. He is giving you an honest assessment (I couldn't resist the pun) of your behavior on this thread. It really does look suspicious if you are asking for help in one programming language and then posting code that you claim to have written yourself in another language that was not in the language you were asking for help in.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11128
  • View blog
  • Posts: 19,070
  • Joined: 19-March 11

Re: how to print the sum of the remainder of an integer

Posted 13 December 2012 - 06:40 AM

View PostYannB, on 13 December 2012 - 07:01 AM, said:

Dude relax don't be an ass


Jack's post was quite reasonable. Do you really think that we can't tell that you have no idea what you're doing? You didn't write that code - you didn't even know what it did!

It's perfectly fine to be a beginner and not know what you're doing, everyone here has done that multiple times. But it's going to be hard to help you get past being a beginner if you're trying to play games.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2