Jump to content

Recommended Posts

1. Cerinta:

Să se scrie o funcție C++ care să returneze suma cifrelor unui număr natural transmis ca parametru.

-  Exemplu:

sumcif(241) = 7

 

2. Rezolvare:

#include <iostream>
using namespace std;


int sumcif(int n)
{
    int s = 0, m = n, ult;
    while (n != 0)
    {
        ult = n % 10;
        s = s + ult;
        n = n / 10;
    }
    return s;
}

int main()
{
    int n;
    cin >> n;
    cout<<sumcif(n);
    system("Pause");
    return 0;
}

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Who's Online   0 Members, 0 Anonymous, 4 Guests (See full list)

    There are no registered users currently online

×

Important Information

This site uses cookies. By continuing browsing, you agree to the use of this information. For more details, see Terms of Use and Privacy Policy.