Sum of the digits

I am trying to write a method which returns the sum of the digits of any integer number.
public class sumofdigits {
    public static int sumofdigits (int number){
         String numberString = Integer.toString(number); // i have changed integer to string.
         int length = numberString.length();
         for(int i = 0; i<length; i++ ){
              String str = String.valueOf(numberString.charAt(i)); // i have changed the char to string
              int n = Integer.parseInt(str);  // then i have changed the string to integer
              int sum = 0;
              sum = sum + n;
              return sum;     
    public static void main(String[] args) {
         System.out.print(sumofdigits(309283));     
}I don't know how to change char to integer so i changed char to string then i move from string to integer...
but it gives missing return statement error... Where am i doing wrong?

Yea i got it ! :)
public class sumofdigits {
    public static int sumofdigits (int number){
         int sum = 0;
         String numberString = Integer.toString(number);
             int length = numberString.length();
         for(int i = 0; i<length; i++ ){
              String str = String.valueOf(numberString.charAt(i));
              int n = Integer.parseInt(str);
              sum = sum + n;
              return sum;
    public static void main(String[] args) {
    System.out.print(sumofdigits(309283));     
}Can you tell me how do i change char to integer and integer back to char?

Similar Messages

  • Sum of the year digit method and declining balance method

    Hi,
    My Client want Deprecation methods Sum of the year digit and declining balance method for tax purpose. This requirement for Colombia Country.
    Which Deprecation Area I activate and how will configure above two methods.
    Regards
    Venki

    Sum-of-the-Years-Digits Method of Depreciation
    Use
    For each year of the expected useful life, the system notes the remaining useful life for the assets and totals the figures in each year. In each fiscal year, the remaining life is divided by this total in order to calculate the depreciation percentage rate for that fiscal year. This method leads to depreciation amounts that are reduced progressively by the same amount each period.
    Since the remaining useful life is no longer defined after the end of the planned useful life, this depreciation method does not allow for depreciation after the end of the planned life. However, you can change to another method after the expected useful life has expired.
    Acquisitions after the depreciation start year or post-capitalization will necessarily lead to a positive net book value at the end of planned life. For this reason, such transactions are not allowed when using the sum-of-the-years-digits method of depreciation. With this method, you have to handle subsequent acquisitions by creating sub-numbers. It is also a requirement that the acquisition year is the same as the depreciation start year.
    Calculation :
    Depreciation = APC * remaining useful life (current period) / total of remaining useful life (over entire useful life)
    APC: 1000
    useful life: 4
    Total remaining useful life: 10 (= 4 + 3 +2 +1)
    Depreciation 1st year = 1000 * 4 / 10 = 400
    Depreciation 2nd year = 1000 * 3 / 10 = 300
    Depreciation 3rd year = 1000 * 2 / 10 = 200
    Depreciation 4th year = 1000 * 1 / 10 = 100
    Declining-Balance Method of Depreciation
    Use
    For the declining-balance method of depreciation, the fixed asset is depreciated by a progressively falling rate. A constant percentage rate is calculated from the expected useful life and a given multiplication factor. This is multiplied with the falling net book value of the fixed asset. For mathematical reasons, the net book value will never reach zero using this method. You change over to straight-line or complete depreciation under these conditions:
    Declining-balance depreciation < straight-line depreciation
    Net book value < x percent of acquisition value
    Net book value < fixed amount
    Net book value < straight-line depreciation
    The changeover method is specified in the internal calculation key.
    Calculation :
    Depreciation = net book value * percentage rate from expected useful life and factor
    APC: 1000
    Exp. useful life: 10
    Net book value: 700
    Multiplication factor: 3
    Depreciation = 700 * (100% / 10 * 3) = 210
    OR You can use following link
    http://help.sap.com/saphelp_45b/helpdata/en/4f/71de3b448011d189f00000e81ddfac/frameset.htm
    http://help.sap.com/saphelp_45b/helpdata/en/4f/71de3b448011d189f00000e81ddfac/frameset.htm
    Regards,
    ANJIREDDY
    Edited by: ANJIREDDYA on May 8, 2010 12:09 PM
    Edited by: ANJIREDDYA on May 8, 2010 12:10 PM

  • Sum of the digit problem

    the statement
    int n = (int)Math.floor(Math.random()*100000 + 1);
    randomly generate a 5 -digit number.
    I need to write the code , which ramdomly generate a 5-digit number (using the above statement) and prints the sum of the digit s. Make sure the program print the number generated.
    Example output:
    Number generated:6743
    sum of digits = 23

    Thnaks for your respond. However , the code should be
    base on the statement below.
    int n = (int)Math.floor(Math.random()*100000 + 1);And you're not able to fit that in with the answer that the other guy handed you?
    I need to write the code , Yes, you do.
    which ramdomly generate a
    5-digit number (using the above statement) and prints
    the sum of the digit s.Your teacher gave you the code to generate the random number. That guy gave you the code to add the digits. What's left that you don't understand?

  • Why can't I use my Adobe ID if I have to reinstall the digital reader?

    I already have an Adobe ID.  I had to reinstall the Digital Reader and it will not authorize using my Adobe ID because it says another computor is already using that ID.  It is the same computor it is only that I had to reinstall the program.

    For me it's the same, I've just bought an iPad mini two weeks ago but I cannot log in with any Apple account in FaceTime.
    I enter my apple account in faceTime, then I press next the second screen and it shows "validating..." but it never gets validated.
    What's happening? This is really annoying

  • HT1848 I purchased a album on my iPhone and was playing it for several days... Last night after syncing it to my laptop i no longer have it on my phone and the only thing that shows up in my iTunes is the digital booklet.

    How in the world do i get my lost Album back?
    I purchased a Album from Itunes on my iphone, Played it for several days. Then when I synced my phone to my laptop, the album is no longer there. It shows up in the Itunes on my computer but only as the digital booklet, which you are supposed to get after a purchase.
    I tried downloading all songs into itunes, that didnt work. I tried logging out of my account with no avail. I tried seeing if i could go into iTunes again and buy redownload it. But it showes that I have to purchase it again...
    Any help would be appreciated.
    Thanks

    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    http://support.apple.com/kb/HT2519

  • HT4108 I'm trying to connect my iPad to my tv so that I can mirror whatever is on my iPad. I want to stream movies online and watch on the tv. I have the digital av adapter and an hdmi cable but it's not working. Any tips ?

    I'm trying to connect my iPad to my tv so that I can mirror whatever is on my iPad. I want to stream movies online and watch on the tv. I have the digital av adapter and an hdmi cable but it's not working. Any tips ?

    If this is an iPad 1, it won't mirror, but it will still output video from the 30 Pin dock connector.
    If your new TV has a conventional VGA in and stereo inputs you can try this connector.
    http://store.apple.com/us/product/MC748ZM/A/apple-composite-av-cable?fnode=3a
    I used to stream video from both Netflix and YouTube and from my Mac using iTunes sharing through my iPad then with this connector plug into my iPad and TV.
    It won't be high resolution, but it will get you picture from iPad to TV.
    Good Luck.

  • I am trying to connect my Apple TV to an older Panasonic projection TV using an HDMI to a DVI cable to the Digital IN. I cannot get any sound. Can you help?

    I am trying to connect my Apple TV to an older Panasonic projection TV using an HDMI to a DVI cable to the Digital IN. I cannot get any sound. Can you help?

    DVI interface does't allow audio. Is there optical IN from your Panasonic TV? By then you can connect your ATV to Panasonic using optical together. It will work.

  • Does the iPhone 4S support HDMI- OUT via the digital AV adapter?

    Does the 4S support HDMI ->OUT via the digital AV adapter or not?  I read that it does (whether it be mirroring, YouTube video playback, movie playback, etc.) but I'm not having any luck.  I recently tried my AV adapter (that I normally use with my iPad 2) but I get nothing.  When I power cycle the phone after connecting the adapter, I get a error stating that the "accessory is not supported by iPhone".  When I go to play a YouTube video, it shows up on the phone's screen for a second and then disappears.  The LCD TV I am connected to never prompts me for the detection of a new HDMI device being connected" either, do I have no clue what's going on.
    Any ideas? Thought?
    Any help to solving this riddle once and for all would be appreciated.
    Thanks and Happy New Year!!
    Andy
    P.S. - my 4S is running iOS 5.0.1 and is a Verizon 16gb model.

    The model number on the adaptor is A1388. The link is here:
    http://store.apple.com/us/product/MC953ZM/A?fnode=MTc0MjU4NjE
    I have the iPhone 4s, running 5.0.1 (9A405)
    I have also experimented with sequence, and I see no particular order needed to make it work. Generally I get it all plugged in and then swipe the iPhone and in a few moments, the screen appears in mirroring mode. Then if I go to Videos I can play my clips with sound, or I can do Keynote presentations, etc.
    So sorry to hear you're having trouble. I imagine you've already double-checked that you're seating it solidly in the dock connector and have a good, working HDMI cable. I am not sure what you mean in your original post about "powering up" the phone. It might also be some Verizon issue, though I don't know what that might be. Is your AV adaptor the Apple branded one?

  • ALV List Display - Is it possible to sum up the subtotal values of  field

    Hi Experts,
    We hav a requirement to do subtotals of the price field for each unique PO Line item, and also to sum up the subtotal values ie. only the subtotal values needs to be added irrespective of the repetation of the po line items in the report.
    PO Number                  PO Item              Price
    XXXXXXXX                 10                      20
    XXXXXXXX                 10                      20
      Subtotalled                                          20
    YYYYYYY                 10                      10
    YYYYYYY                 10                      10
      Subtotal showing                                10  
    We want the total of price to be           30 ie (Subtotal value of XXXX, 10 = 20 and YYYYY,10 = 10 ) but currently do_sum is been used in the fieldcat of the report which displays all the occurances of the record... = 60(202010+10)
    Kindly help asap.
    Thanks,
    Rosy

    Hi rosy,
    the question pops up to why you have duplicates in the display table. If you
    DELETE ADJACENT DUPLICATES FROM
    the output table, everything should be fine.
    Regards,
    Clemens

  • Discoverer plus  report does not sum up the column

    Hi All,
    I am running the discoverer report from discoverer plus (web version) and was
    trying to use the sum function to sum the total amount of the report.
    The sum function does not work and it only display Cell
    Sum: (blank) with no actual data.
    When i checked the report details some columns are calculated based on the columns which i want to sum up.
    My query uses CASE function to calculate one of the columns.
    I changed the aggregation behavior setting to "Show the sum of the calculated values from the contributing cells "
    then also i'm not able see the totals.
    Any ideas please share with me.
    Thanks in advance,

    Hard to answer your question without seeing what you are trying to do. Issues like this are often better reported to Oracle support, where they can get a web sesison going and be able to see what you see on your computer. Would be helpful to illustrate exactly what your detail looks like and how you are trying to create your sum. There could be several possible issues that could cause your problem, so all I can do is speculate some. Are you trying to sum one column, or are you creating multiple totals (for multiple columns)? Do you have any rows in the column with a NULL value? NULL values will be a problem when trying to sum up a column. You would need to convert the NUll value to something else, probably a zero. That would be the first thing I would look at. Hope that helps a little bit.
    John Dickey

  • I recently purchased Star Trek into Darkness on Bluray in the UK. When the digital copy with the movie had finished downloading in iTunes, it said processing and then iTunes suddenly stopped working and I had to shut it down. Now I can't see it at all.

    Can any one help as the digital copy downloaded but was processing when iTunes froze and then I had to shut it down. I have the latest version and am using iTunes on a windows PC. I would imagine similar problems could arise on download processing if there was a power cut so surely iTunes knows that my ID has redeemed the movie and it will continue now?
    Can anyone help?

    I forgot to add that when I looked at the store for available downloads it only continued to download the movie extras  which is only 69MB of menu items and no movie content.

  • PCI 6602:How can I use the digital lines of the board and in the same time to generate pulse train using a counter?

    Hello!
    My problem appeared when I tried to update my code from Traditional NI-DAQ Legacy to DAQmx.
    I am using 2 counters (counter 5 and counter 7)  from PCI-6602, to generate pulse train, and also the Digital I/O lines of the port 0 (the lines form 0 to 7). What I do in my application is that I am starting to generate the pulse train on the output of the 2 counters, and after that I am playing with the state of the digital lines.
    In traditional there was no problem using the counters and the digital lines in the same time, everything was going perfectly, but in DAQmx this is not possible.
    What happens: I start to generate pulse train on the output of the counters,  no errors encountered, but when I try to modify the state of one line of the digital port the generation of the pulse train is stopped. This is happening when I start the task associated to the digital port.
    My question is: it is possible to create a channel on the digital lines without altered the channels created for the counters?
    Another thing what I manage to see using the  "Measurement & Automation Explorer" and Test panels for PCI-6602, basically is the same thing, I generate pulse train on the output of the counter 7 and try to start a task on the digital line, but I get one error :
    "Error -200022 occurred at Test Panel
    Possible Reason(s):
    Measurements: Resource requested by this task has already been reserved by a different task.
    Device: Dev4
    Terminal: PFI8"
    Instead if I use the counter 0 or counter 1 to generate pulse train I don't encounter the same problem.
    Which resources are used by the counters 2 to 7 from the PCI-6602 board and the counters 0 and 1 do not use?
    Thank in advance for any replies!
    Ciprian
    Solved!
    Go to Solution.

    Hello Jordan, thank you for your reply.
    I am sorry but I can not see or run your example, I don't use LabView, I use Visual C++ for developing.
    Here is the code for generating the pulse train:
    GeneratePulseTrain(unsigned long ulCount1, unsigned long ulCount2)
        short nStatus = 0;
        nStatus = DAQmxCreateTask("",&m_taskHandle);
        nStatus = DAQmxCreateCOPulseChanTicks (m_taskHandle, "Dev4/count5", "", NULL, DAQmx_Val_Low, 0.0, ulCount1,ulCount2);
        if( bTriggerMode == true) // if hardware trigger is enabled
            nStatus = DAQmxSetTrigAttribute (m_taskHandle, DAQmx_ArmStartTrig_Type, DAQmx_Val_DigEdge);
            nStatus = DAQmxSetTrigAttribute (m_taskHandle, DAQmx_DigEdge_ArmStartTrig_Edge, DAQmx_Val_Rising);
            nStatus = DAQmxSetTrigAttribute (m_taskHandle, DAQmx_DigEdge_ArmStartTrig_Src,"Dev4/PFI17" );
        //set the internal timebase
        nStatus = DAQmxSetCOCtrTimebaseSrc(m_taskHandle,"Dev4/count5","20MHzTimeBase" );
        nStatus = DAQmxStartTask(m_taskHandle);
        return nStatus;
    And the code where I try to set the digital line:
    SetChannelState(short nState)
        short nStatus = 0;
        uInt8 wrtBuf0[1]={0};
        nStatus = DAQmxCreateTask("",&m_taskHandle);
        // Configure line as output 
        nStatus = DAQmxCreateDOChan (m_taskHandle, "Dev4/port0/line0", "", DAQmx_Val_ChanPerLine);
        nStatus = DAQmxStartTask(m_taskHandle);
        wrtBuf0[0] = nState;
        nStatus =DAQmxWriteDigitalLines (m_taskHandle, 1, 0, 0, DAQmx_Val_GroupByScanNumber , wrtBuf0, NULL, NULL);
        nStatus = DAQmxWaitUntilTaskDone(m_taskHandle,10);
        nStatus = DAQmxStopTask(m_taskHandle);
        nStatus = DAQmxClearTask(m_taskHandle);
        m_taskHandle = 0;
        return nStatus;      

  • How can I import data in to the digital word generator in Multisim?

    How can I import data in to the digital word generator in Multisim?
    I just  received this comment from a friend, a RADAR engineer, who has just down loaded Multisim.  He has been using HP/Agilent software.  He has a work around using a piecewise linear voltage waveform with data imported from Excel but this is not really a good solution.  It would also be helpful to import data from Mathcad or equivalent.
    "I thought I was about to be impressed with MultiSim but it ended only in disappointment. There is a word generator in the simulation instrument panel which can drive the DAC with a waveform and it can have thousands of lines of values. I opened Excel, wrote the formula to generate the time and voltage points for a chirp, converted to DAC values in Hex and then went back to the word generator in MultiSim to load the values only to find that you have to enter each value manually. It doesn’t even allow you to paste in a list of values from a text file. I’m not going to type 5000 values by hand. If you get the chance to give feedback to National Instruments please ask them if the paste option can be added to the word generator. MultiSim is useful in many regards, but in this case, it left me with the impression that it is considerably limited in capability compared to what I’m used to."

    Hi,
    You can load your data automatically in the Multisim word generator. Follow these steps:
    - Save your data file (in excel .xslx ir .csv format) on your computer
    - Change the extension of the file to ".dp"
    - Double-click the word generator in Multisim and click on Set...
    - In the Settings dialog box, click on Load and then Accept
    - This will prompt you to select the .dp file you have on your computer, select it and you're good to go
    However, in Multisim you have the option of creating your own custom simulation analysis and instrument.
    I will try creating the instrument and send it back to you but it might take some time.
    Multisim and LabVIEW are very powerful in test automation, with the custom instruments you create for Multisim you don't need to export your data file into excel from LabVIEW (or MathCAD or other tools) and then reload it into Multisim. The test procedure is automated instead.
    Please check this reference design about automated simulation
    http://zone.ni.com/devzone/cda/tut/p/id/7825
    Here is how you can create your own custom measurement tool in Multisim and LabVIEW, but as I mentioned, I will create the word generator and come back to you anyways
    http://zone.ni.com/devzone/cda/tut/p/id/5635
    Let me know if you have any questions.
    Mahmoud W
    National Instruments

  • How to delete the Digital ID from Acrobat Pro 9 Extended?

    Hi, please help me
    I want delete the Digital ID
    when I open the menu "Advanced"----"Security Settings"
    in the dialoge I chose Digital IDs, then Windows Digital IDs
    there is a list and I can see many iterms of digital IDs
    I want delete each of that
    when I chose one of them and click the button "Remove ID"
    the softeware tell me that "Removing a digital ID is only permitted if the ID was created by Adobe reader or acrobat version 8.0 or later "
    what should I do ?

    Hi Koalar2010why,
    I know it's been a long time since you posted your question, but if you are still interested here is what you need to do.
    Launch Internet Explorer
    Select Tools > Internet Options (you can get to Tools either from the menu, the toolbar button or Alt+X)
    Select the Content tab on the Internet Options dialog
    Click the Certificates button in the middle of the Contents panel
    Select the Personal tab on the Certificates dialog (it's usually selected by default)
    --Before continuing I'd recommend exporting the digital IDs to a file in case you ever need to get an old one back again)--
    Highlight one or more digital ID(s) and then click the Remove button
    The next time you launch Acrobat and go back to the Security Settings dialog the digital IDs will be gone.
    Steve

  • How to config the digital write-to-line so it would independent when more than one is being used

    when one digital wirte-to-line is used in the labview vi, a good logic high(5v) can be read using a voltmeter,but when two or more digital write-to-lines are being using at the same time,the logic high seemed to have split voltages and thus having a bad logic high.how to make sure that in the labview program,that when many digital write-to-lines are used at the same time,all of them can abtain a good logic high (around 5v).
    I need the solutions urgently.
    grays
    np

    Hello,
    The probable reason you are seeing the behavior you are is due to a reconfiguration of the digital port. The Write to Digital Port VI is setup to configure the entire port, then write to one line (default settings). If you reconfigure the port it will reset all the lines, and then write your new value. You will want to use the iteration input on the Write to Digital Line so you can avoid the reconfiguration issue.
    The first time the Write to Digital Line is executed in your code wire a 0 to the iteration (or leave it unwired). This will configure the port for write and write your first value. The next time you use the write to digital port VI wire an integer greater than 0. This will bypass the configuration step and simply write the new value to the
    line. You can open the Write to Digital Line VI to see how the iteration input effects the execution and configuration of the digital ports.

Maybe you are looking for

  • Timeline Saying Clips are Offline, when Browser Shows Clips to be OK

    I have a few clips in my timeline that fews the OFFLINE WINDOW. Yet when I check my browser, the clips have no offline slash and can be seen as edited in the canvas. How do I make the clips show normally on the timeline. Also clips are trying to show

  • OfficeJet 6500 dead end

    Hi, Similar to experiences outlined in other posts, I too cannot get officejet 6500 E709n wireless printer to print on demand. If I want to print I have to turn off and then repower the printer, at least once. Sometimes the situation requires two or

  • Camera roll usage is 6.9 GB even there is no photo there , it is happen after i dropped my iPhone , i don't use iCloud also .

    camera roll usage is 6.9 GB even there is no photo there , it is happen after i dropped my iPhone , i don't use iCloud also .

  • ESS/MSS Configuration settings

    Hi Guru's, Please provide usefull documents for ESS/MSS Configurations for the following: ESS Senarios: 1. Leave request and Approval 2.Who's Who 3.Personnel Information 4.Address Information 5.Bank Data 6.Family Information 7.Emergency Details 8.Alt

  • Set default font?

    When I try to set default font on my JMenuBar like this: javax.swing.UIManager.put("JMenuBar.font",new Font("Verdana",Font.PLAIN, 14)); nothing happens? Can someone help me with this.