C++ program to calculate sum of numbers entered in real time

i have created a c++ program in which a user inputs a number and the compiler gives the sum of digits at hundred ,tens and ones place of that number
like

Number 12345 sum of last three digits is 12.

but what should be done if the compiler has to sum all the digits of that number depending on run time input of the number.

like user enters 1234567890 0r 12345678 or 12345678 then how can compiler choose the function to calculate the digits sum at run time, using sizeof() operator for input number and then placing the condition seems useless because all the digits use 4 byte of int except for the number 1234567890 ,then how can we make condition on the run time?????

Replies

  • Nayan Goenka
    Nayan Goenka
    I am confused in your last part. Do you want to calculate the result at run time, like dynamically when the user is inputing the numbers or you want something else. And what about the sizeof(). that part is not clear. Explain again
  • Vicky One
    Vicky One
    This program calculates the sum of last three digits no matter what the number user inputs.
    #include
    #include
    
    int sum_three(int num);
    int main()
    {
      
       int number;
       cout<<"please enter a number: ";
       cin>>number;
      
       cout<<"the sum of hundred tens and unit of number is: ";
       cout<This function can only sum last three digits.
    i want if i enter 12345 the compiler should calculate the sum of all digits
    and if i enter 123456 then compiler should calculate the sum of all digits.
    By using function and function call as i used in the program.

    obviously there would be several functions and several condtions to chose the function depending upon the digits entered by the user in a number through cin statement , then seeing the digits how compiler can choose the appropriate function call.
    All the matter is that input a number smallest like 10 or larget like
    1234567890 what the condion will be to choose the appropriate function to calculate a sum????
  • Nayan Goenka
    Nayan Goenka
    The program you have made is quite redundant. You should try a different approach. Take the input as a string. Then split the string and store it into an array. Then add the values in the array and display output. This is the best approach I think. It will be able to accept any number of digits.
  • Vicky One
    Vicky One
    oh!!! thats an owsome idea. thx bro but in my case i think the idea is a little poor but does it have any solution other than string or array???
  • Nayan Goenka
    Nayan Goenka
    Vicky One
    oh!!! thats an owsome idea. thx bro but in my case i think the idea is a little poor but does it have any solution other than string or array???
    The idea I gave you is ultimate way to solve it. If you want to skip it, the program will be extremely redundant and you will have to program it in a very complicated manner. I have the alternative you are talking about but I prefer not to advise you on it since it will be extremely confusing and very tough to implement. But yes, it is a substitute to strings as I said above.
  • Vicky One
    Vicky One
    i think m very dull about programming.
    what would be the correct code of following program.

    #include
    #include
    int sum (char num[]);
    int main()
    {
      
       char number[100];
       cout<<"please enter a number: ";
       cin>>number;
       cout<<"your sum is: "<and also confused about  you said of  spliting sting into array..
    All of that i was able to do is i could code the above program which gives garbage value.
  • Nayan Goenka
    Nayan Goenka
    No No No. This is not the correct way. It is too late so I wont be able to give you code for it but there is a header file called string.h. You will have to include it. Plus use the pre defined function string_split or some name like that. Google it. you will find the name.
  • simplycoder
    simplycoder
    If string is used, it means user can enter as many digits as it wants( abstracting the limitations in this context), this means the sum can also go beyond 64-bit number.
    which is of the maximum size primitive data-type.

    so you should also save the sum in string. Perform addition in string and store it in string.
  • Nayan Goenka
    Nayan Goenka
    Storing the answer in string might not be necessary. I dont think any user will input a number which has more than 100digits and the maximum sum of those digits might well as be 900. So this is feasible and well under the int limit
  • Vishal Sharma
    Vishal Sharma
    Vicky One
    i think m very dull about programming.
    what would be the correct code of following program.

    #include
    #include
    int sum (char num[]);
    int main()
    {
    
       char number[100];
       cout<<"please enter a number: ";
       cin>>number;
       cout<<"your sum is: "<and also confused about  you said of  spliting sting into array..
    All of that i was able to do is i could code the above program which gives garbage value.

    Code way implementation... change it as you like! i have declared sum as integer

    #include
    char a[100];
    int main() {
        int sum = 0;
        printf("Enter the number : ");
        fgets(a,sizeof(a),stdin);
        for(int i = 0 ; a[i+1] != '\0' ; i++) {
            sum = sum + (a[i] - '0');  
        }
        printf("%d\n",sum);
        return 0;
    }
  • rahul69
    rahul69
    Vicky One
    i have created a c++ program in which a user inputs a nu..................,then how can we make condition on the run time?????
    There can be more than one method to do this, one is as told by Nayan ie using arrays. Other can be a modification to what u are doing, here is a quick code for ur understanding:
    main()
    {
      int no;
      int sum=0;
      cout<<"\nEnter the number";
      cin>>no; 
      while(no)
      {
      sum=sum+(no%10); 
      no=no/10;     
      }
      cout<<"Sum is :"<Hope it helps ๐Ÿ˜€
    However, if u need to do for large no of digits, then array implementation is better.
    Happy Coding!๐Ÿ‘
  • Vishal Sharma
    Vishal Sharma
    rahul69
    There can be more than one method to do this, one is as told by Nayan ie using arrays. Other can be a modification to what u are doing, here is a quick code for ur understanding:
    main()
    {
      int no;
      int sum=0;
      cout<<"\nEnter the number";
      cin>>no;
      while(no)
      {
      sum=sum+(no%10);
      no=no/10;    
      }
      cout<<"Sum is :"<Hope it helps ๐Ÿ˜€
    However, if u need to do for large no of digits, then array implementation is better.
    Happy Coding!๐Ÿ‘
    the main reason of using array is to make the program efficient for all the inputs taking no as int makes it available for only 32768 (and a few more nos beyond it) so i think the best way would be string and array!
  • Nayan Goenka
    Nayan Goenka
    @#-Link-Snipped-# : See as I said earlier. The best possible approach to your problem statement is array and strings and everyone approves it. There is a main reason behind this. It is a programming trick. If you are not sure about the length of data you will be getting as an input and then you are supposed to process it record by record, it is best way to fetch it as string and then do whatever you want with it. It will expand your options exponentially.
  • rahul69
    rahul69
    Vishal0203
    the main reason of using array is to make the program efficient for all the inputs taking no as int makes it available for only 32768 (and a few more nos beyond it) so i think the best way would be string and array!
    Here is another implementation which does work without an array:
    main()
    {
      char no;
      int sum=0;
      cout<<"\nEnter the number"; 
      while((no=getc(stdin))!='\n')
      {
        sum+=(no-48);   
      }
      cout<<"Sum is :"<๐Ÿ˜
                                        
  • Vicky One
    Vicky One
    well thx alot for all of you.. I have done it using the string the main issue was only to subtract the ascci of zero so that i could get the orignal sum i wanted,,thx to all
    Nayan.......๐Ÿ˜€
  • Nayan Goenka
    Nayan Goenka
    Well it was only me with an idea. Others did the main job. Good job all with the program. ๐Ÿ˜

    @#-Link-Snipped-# @#-Link-Snipped-# @#-Link-Snipped-#
  • Vishal Sharma
    Vishal Sharma
    Nayan Goenka
    Well it was only me with an idea. Others did the main job. Good job all with the program. ๐Ÿ˜

    @#-Link-Snipped-# @#-Link-Snipped-# @#-Link-Snipped-#
    you hit the forum first, doesn't mean that we didn't had any idea ๐Ÿ˜›
  • Nayan Goenka
    Nayan Goenka
    LOL. That is what you get for hitting the place first ๐Ÿ˜›

You are reading an archived discussion.

Related Posts

ISRO - Indian Space Research Organization has confirmed that the launch of GSAT-7, India's first ever defence satellite was successful. The launch was scheduled at 2:00 AM today morning from...
The other day I wrote about the new voice search feature included on their site by Flipkart and CEans reported that the service didn't work 100% for them. I believe...
I've been using iPad for almost 2 years now and have always wished if I could do some real productive work on it, apart from email. There were times I...
NASA's engineers are planning to deliver solar energy on Earth via platforms located in the orbit. John Mankins is heading the project which is in development stages. The project is...
I want all the CE developers to peek their nose into this topic.Consider if you are a developer and creating a new app or software. What are the difficulties faced...