Tuesday, December 15, 2009

WAP to find the sum of the digits of a given number

After a long time I am writing a blog post for my own blog. :) . Past few weeks, I was so busy with studies and learning etc. and these days exams are going on. But still I am not able to get rid of my "Love with C". Before the exam of Punjabi and PC computing, I was making C-Programs, instead of preparing those subjects. Well, because its good for me because the program i was wanting to write but never due to my exams i wasn't trying that. I thought i'll take time. But yesterday due to some happenings, I was little frustrated so I just sat in front of computer and started writing the old pending program. I totally involved in program and it gave me desired result. So woooooopy.. :)

Program was, WAP to find the sum of the digits of a given number. Here i wasn't knowing about how to calculate the sum of the digits of a number whose size is not give. Finally i reached at the solution and finished this program.
So here it goes..

/*WAP to find the sum of the digits of a given number.*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
long int n,div;
int i,temp,sum=0,length;
char *str;
clrscr();
printf("\nEnter value = ");
scanf("%ld",&n);
ltoa(n,str,10); //converts long int to string
length=strlen(str); //found the length of string
temp=length-1;
div=pow(10,temp);
for(i=0;i<length;i++)>
{
sum+=(n/div)%10;
div=div/10;
}
printf("\n%d",sum);
getch();
}

Alternate Way


void main()
{
long int n,div,temp;
int sum=0;
clrscr();
printf("\nEnter value = ");
scanf("%ld",&n);
while(n>0)
{
  temp=n%10;
  n=n/10;
  sum+=temp;
}
printf("\n%d",sum);
getch();
}



I wish, try this program and if something is wrong in this program then just let me know. If any other idea to make this kind of program even better then also leave a reply with your own idea.

Regards
Amandeep Singh

41 comments:

  1. Hi I am so excited I found your blog page, I really
    found you by mistake, while I was looking on Bing for something else, Regardless I
    am here now and would just like to say thanks
    for a incredible post and a all round interesting blog (I also love the theme/design), I don't have time to look over it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great work.
    my webpage: travel packages

    ReplyDelete
  2. Hurrah, that's what I was looking for, what a information! present here at this weblog, thanks admin of this web site.
    Also visit my web blog ... online kredit schufa

    ReplyDelete
  3. hello there and thank you for your information – I've definitely picked up anything new from right here. I did however expertise a few technical issues using this web site, since I experienced to reload the web site many times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I'm complaining, but sluggish
    loading instances times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords.
    Well I am adding this RSS to my e-mail and could look out for much
    more of your respective exciting content. Make sure you update
    this again soon.
    Have a look at my web blog ... visit the following web page

    ReplyDelete
  4. Hey! I realize this is sort of off-topic however I had to ask.
    Does building a well-established website like yours require a massive amount work?
    I'm brand new to writing a blog but I do write in my journal on a daily basis. I'd like to start a blog so I can share my
    experience and feelings online. Please let me know if you have any kind of recommendations or tips for new aspiring bloggers.
    Appreciate it!
    Here is my page ; geox online shop outlet

    ReplyDelete
  5. Hi there, I want to subscribe for this web site
    to obtain most recent updates, therefore where can i do it please help out.
    Stop by my website ; Seo Expert Chandigarh

    ReplyDelete
  6. Every weekend i used to pay a quick visit this web
    page, because i want enjoyment, for the reason that this this website conations genuinely pleasant funny information too.
    my webpage: work for stay at home moms free

    ReplyDelete
  7. Asking questions are actually fastidious thing if you are
    not understanding something totally, except this piece of writing offers fastidious understanding even.
    Here is my page zusatzbeitrag der krankenkassen

    ReplyDelete
  8. Thanks for every other informative site. The place else may I am getting that type of info written in such an ideal method?
    I've a challenge that I am just now running on, and I have been on the glance out for such info.
    Also visit my weblog :: tarife krankenkassen

    ReplyDelete
  9. Today, I went to the beachfront with my kids. I found a sea shell and
    gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear."
    She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but
    I had to tell someone!
    Feel free to visit my web site :: make money through clickbank

    ReplyDelete
  10. I hardly comment, but i did some searching and wound up
    here "WAP to find the sum of the digits of a given number".
    And I do have a couple of questions for you if it's allright. Is it simply me or does it appear like a few of the responses come across like coming from brain dead people? :-P And, if you are writing at other sites, I'd
    like to follow anything fresh you have to post.
    Could you list of all of your community pages like your Facebook page, twitter feed,
    or linkedin profile?
    My blog post ; interesting business ideas

    ReplyDelete
  11. You made some really good points there. I looked on the net to find
    out more about the issue and found most
    people will go along with your views on this site.
    Here is my web-site :: How to resell Hosting

    ReplyDelete
  12. My coder is trying to convince me to move to .
    net from PHP. I have always disliked the idea because of the costs.
    But he's tryiong none the less. I've been using WordPress on various websites for about a year and am anxious about switching
    to another platform. I have heard good things about blogengine.
    net. Is there a way I can import all my wordpress posts into it?
    Any help would be really appreciated!
    Also see my webpage > bad credit rating

    ReplyDelete
  13. Thanks for a marvelous posting! I really enjoyed reading it, you may be a great author.
    I will make certain to bookmark your blog and definitely will come back later in life.
    I want to encourage you to definitely continue your great posts,
    have a nice afternoon!
    Feel free to surf my page :: Credit Card Consolidation Loans

    ReplyDelete
  14. If you wish for to obtain much from this paragraph then you have to apply these strategies to your won blog.
    Also see my site > beitragsbemessungsgrenze krankenversicherung 2011

    ReplyDelete
  15. What's up it's me, I am also visiting this site daily, this
    website is in fact good and the users are actually sharing good thoughts.


    My blog ... kredit auch mit negativer schufa

    ReplyDelete
  16. I visited many sites however the audio feature for audio
    songs present at this website is actually wonderful.


    Feel free to surf to my page ... seo placement
    my webpage > pittsburgh seo experts

    ReplyDelete
  17. Thanks for sharing your thoughts. I truly appreciate your efforts and I will be waiting for
    your further write ups thanks once again.

    Also visit my homepage; How To Build Your Business

    ReplyDelete
  18. Hi there, yes this article is really good and
    I have learned lot of things from it regarding blogging. thanks.


    Review my web-site :: tennis game

    ReplyDelete
  19. Everything is very open with a really clear explanation of the issues.
    It was really informative. Your website is useful.
    Thanks for sharing!

    Take a look at my weblog ideas for small businesses
    my web site :: new business help start up

    ReplyDelete
  20. It's appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I want to recommend you few interesting things or advice. Perhaps you can write next articles regarding this article. I wish to read even more issues approximately it!

    Here is my web-site: vor und Nachteile private krankenversicherung

    ReplyDelete
  21. Pretty nice post. I just stumbled upon your blog and wished to say that I've truly enjoyed surfing around your blog posts. After all I'll be subscribing to your feed and I hope you write again soon!



    Feel free to surf to my web-site search Marketing Business

    ReplyDelete
  22. This design is wicked! You certainly know how to keep
    a reader entertained. Between your wit and your videos, I was almost moved to start
    my own blog (well, almost...HaHa!) Wonderful job.

    I really loved what you had to say, and more than that, how you presented it.

    Too cool!

    Here is my web-site - schuh outlet online shop
    my site - outlet store Online shop

    ReplyDelete
  23. Undeniably believe that which you said. Your favorite reason appeared
    to be on the web the simplest thing to be aware of.

    I say to you, I definitely get irked while people think about worries that they just
    do not know about. You managed to hit the nail upon the top as well
    as defined out the whole thing without having side-effects , people can take a signal.
    Will likely be back to get more. Thanks

    Feel free to surf to my webpage Making Money From Affiliate Programs

    ReplyDelete
  24. I do consider all the ideas you have introduced in your post.
    They're really convincing and can definitely work. Nonetheless, the posts are very short for starters. Could you please lengthen them a little from next time? Thanks for the post.

    my blog post - Gesetzliche Krankenversicherung Leistungen

    ReplyDelete
  25. I'm extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it is rare to see a great blog like this one nowadays.

    Have a look at my page; cheap seo

    ReplyDelete
  26. Hello there I am so excited I found your web site, I really found you by
    mistake, while I was browsing on Askjeeve for something else, Nonetheless
    I am here now and would just like to say thanks a lot for a marvelous post and a all round exciting blog (I also love the theme/design), I don't have time to look over it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent work.

    My homepage - Recommended Looking at
    my web page > block island vacation rentals

    ReplyDelete
  27. I love your blog.. very nice colors & theme. Did you make
    this website yourself or did you hire someone to
    do it for you? Plz respond as I'm looking to create my own blog and would like to know where u got this from. appreciate it

    Here is my webpage: starting a business from home

    ReplyDelete
  28. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog
    posts. Anyway I'll be subscribing to your feeds and even I achievement you access consistently rapidly.

    Here is my weblog ... private krankenvollversicherung

    ReplyDelete
  29. I'm curious to find out what blog platform you're working with?
    I'm having some small security issues with my latest website and I'd like
    to find something more risk-free. Do you have any suggestions?


    My homepage ... Full Report
    Also see my website: email us

    ReplyDelete
  30. It's a pity you don't have a donate button!
    I'd certainly donate to this superb blog! I guess for now i'll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will share this website with my Facebook
    group. Talk soon!

    my blog post :: jacksonville home Equity loan
    my web site - Kosten gesetzliche krankenversicherung

    ReplyDelete
  31. Hi there! This post could not be written any better! Reading through
    this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this post to him.

    Pretty sure he will have a good read. Many thanks for sharing!


    Check out my web page - Email Console

    ReplyDelete
  32. I like reading an article that will make men and women think.
    Also, thank you for allowing me to comment!

    Feel free to visit my blog http://jkaluski.homeserver.com/w/index.php?title=User:AndraZop

    ReplyDelete
  33. Asking questions are truly good thing if you are not understanding something entirely, except this post offers
    good understanding yet.

    Also visit my web blog - webhosting anbieter

    ReplyDelete
  34. Marvelous, what a blog it is! This webpage presents valuable information to us, keep it up.


    My homepage :: home equity credit loan

    ReplyDelete
  35. I'll immediately grasp your rss as I can not in finding your e-mail subscription hyperlink or newsletter service. Do you have any? Kindly allow me know in order that I could subscribe. Thanks.

    Feel free to surf to my blog :: Access Restricted

    ReplyDelete
  36. What's Going down i am new to this, I stumbled upon this I have discovered It absolutely useful and it has helped me out loads. I'm
    hoping to give a contribution & aid different users like its helped me.
    Great job.

    Look at my webpage; simply click the next internet site

    ReplyDelete
  37. That is a very good tip particularly to those fresh to the blogosphere.

    Short but very accurate info… Many thanks for sharing this one.

    A must read post!

    Also visit my page; Business Ventures

    ReplyDelete
  38. I like the helpful info you provide in your articles.

    I'll bookmark your weblog and check again here frequently. I am quite certain I will learn a lot of new stuff right here! Good luck for the next!

    Also visit my blog ... click through the next internet site

    ReplyDelete
  39. Hi there! Do you use Twitter? I'd like to follow you if that would be okay. I'm absolutely enjoying your blog and look forward to new posts.


    Take a look at my web blog ... katalog kleidung

    ReplyDelete
  40. I encountered your weblog on yahoo and discover a handful of
    of the posts. excellent function! I just added you to my Google News Reader.



    Check out my blog post ... who will consolidate private student loans

    ReplyDelete
  41. Hi there, I encountered your net log through
    Google even though seeking for very first assistance as well as your spot appears quite intriguing for me.


    My site ... clickbank affiliate website ()

    ReplyDelete