CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

Need help for repetition method! topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2010, 12:28 PM
Junior Member
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
rangerys is on a distinguished road
| More
Need help for repetition method!

Hi guys,

I have a question to ask:

Write a program to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.

The tricky part for me is the part where the user can type in any digits he wishes.

From the angle i see it, I attempted to do smth along this line:

while x > 1 // x is the number the user entered
x = x/10
count++

using the count i know exactly how many digits are there, from then i proceed to identify each digit,

x % pow(10,n) * 10 to isolate each digit due to the correlation

my question is how to i assign the values i looped out to another variable? like digit1 = 2, digit 2 =2 in this looping function?

thanks for ur help!
Reply With Quote
  #2  
Old 09-10-2010, 03:29 PM
Senior Member
 
Join Date: Sep 2010
Location: Kolkata, India
Posts: 307
Thanks: 0
Thanked 15 Times in 15 Posts
techforum4u is on a distinguished road
| More
Re: Need help for repetition method!

All you need to do is to count the number of digits of a given number..then separate the numbers to add up and pass the number formed to the function..the call will be recursive and when the number of digits is 1 return the actual ans...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Paper Pencil Method of Multiplication anniefaty C++ Programming 1 09-05-2010 02:22 AM
get method spikestar C++ Programming 0 08-31-2010 04:11 AM
how to reverse a string by using array method.? gahhon C Programming 0 08-28-2010 03:11 AM
how to reverse a string by using array method.? gahhon C Programming 0 08-28-2010 03:11 AM
Simplex Method techinspiration C Programming 2 02-18-2010 10:06 PM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:23 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board