Raised This Month: $51 Target: $400
 12% 

a question about a appz in c++


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
z0mbiland
Senior Member
Join Date: Jan 2013
Old 12-12-2015 , 02:17   a question about a appz in c++
Reply With Quote #1

I have this appz in c++

PHP Code:
#include <iostream.h>
    
void main()
    { 
long n,c,s,k;
    
cout<<"n=";cin>>n;
    
s=0;k=0;
    do{
    
c=n%10;
    
s=c;
    
k++;
    
n=n/10;}
    while(
n!=0);
    
cout<<"digit sum="<<s<<endl;
    
cout<<"digit number="<<k;} 
I want to know what this will do:
c=n%10;
n=n/10;

What is n%[value] and n/[value]?
Why is used 4 variable in this? n,c,s,k;

k++ this mean increment +1

I need all this program explained from begin to end!

Last edited by z0mbiland; 12-12-2015 at 02:20.
z0mbiland is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-12-2015 , 03:30   Re: a question about a appz in c++
Reply With Quote #2

Sounds like a homework problem or something. Did you even try running it?
__________________
fysiks is offline
z0mbiland
Senior Member
Join Date: Jan 2013
Old 12-12-2015 , 07:54   Re: a question about a appz in c++
Reply With Quote #3

I have a book with problems and i start learning c/c++. I learn how to integrate an algoritm in c/c++.
I'm not a school and is not a homework problem.
Yes, i write it in borland C and is working.

I don't know what mean:
c=n%10;
n=n/10;

What i know is: "%" - mean rest OR mod
"/" mean division

I don't know why is used only the value of 10. If i choose else value what will happen?
I want all this program explained to understand correctly the future programs i will learn.

Last edited by z0mbiland; 12-12-2015 at 07:55.
z0mbiland is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-12-2015 , 10:49   Re: a question about a appz in c++
Reply With Quote #4

Learn first intent your code and next give some empty lines and spacing.

PHP Code:
#include <iostream.h>

void main()

    
long ncsk;

    
cout << "n = "
    
cin >> n;
    
0
    
0;

    do
    {
        
10;
        
c;
        
k++;
        
10;
    }
    while( 
!= );

    
cout << "digit sum = " << << endl;
    
cout << "digit number = " << k;

And guess what it does:
PHP Code:
    cout << "digit sum = " << << endl;
    
cout << "digit number = " << k
Sum the digits of a number and say how much digits such number has? No. To do this, should have:

PHP Code:
    do
    {
        
10;
        
c;
        
k++;
        
10;
    }
    while( 
!= ); 
Now let us use our imagination:

cout << "n = "; print "n = " on screen
cin >> n; wait you to input something and stores it at n

First loop:
c = 31 % 10 = 1
n = 31 / 10 = 3
k = 0 + 1

Second and last loop:
c = 3 % 10 = 3
n = 3 / 10 = 0
k = 1 + 1

digit sum = 4
digit number = 2

Update:
Quote:
Originally Posted by z0mbiland View Post
I don't know why is used only the value of 10. If i choose else value what will happen?
If you use anything else than 10, it will not provide the proper output, because modulus 10 is how to decompose using any number at any base at that algorithm.

Last edited by addons_zz; 12-12-2015 at 15:31. Reason: spelling fixes and new better info
addons_zz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-12-2015 , 13:13   Re: a question about a appz in c++
Reply With Quote #5

I think that the only thing that we should tell you is that "%" is used for modulo and "/" is division but since you are dividing integers, you are doing integer division.

I'd recommend that you search for those two terms on the internet. Everything else that you need to know about that code can be learned from C/C++ tutorials since it's simply basic C/C++ code.
__________________
fysiks is offline
z0mbiland
Senior Member
Join Date: Jan 2013
Old 12-12-2015 , 16:10   Re: a question about a appz in c++
Reply With Quote #6

Quote:
Originally Posted by addons_zz View Post
Learn first intent your code and next give some empty lines and spacing.

PHP Code:
#include <iostream.h>

void main()

    
long ncsk;

    
cout << "n = "
    
cin >> n;
    
0
    
0;

    do
    {
        
10;
        
c;
        
k++;
        
10;
    }
    while( 
!= );

    
cout << "digit sum = " << << endl;
    
cout << "digit number = " << k;

And guess what it does:
PHP Code:
    cout << "digit sum = " << << endl;
    
cout << "digit number = " << k
Sum the digits of a number and say how much digits such number has? No. To do this, should have:

PHP Code:
    do
    {
        
10;
        
c;
        
k++;
        
10;
    }
    while( 
!= ); 
Now let us use our imagination:

cout << "n = "; print "n = " on screen
cin >> n; wait you to input something and stores it at n

First loop:
c = 31 % 10 = 1
n = 31 / 10 = 3
k = 0 + 1

Second and last loop:
c = 3 % 10 = 3
n = 3 / 10 = 0
k = 1 + 1

digit sum = 4
digit number = 2

Update:


If you use anything else than 10, it will not provide the proper output, because modulus 10 is how to decompose using any number at any base at that algorithm.
Now i understood what this "10" is!

I have another question:

In this "n = 31 / 10 = 3" = 3.1 (because i use int or long the number will be represented without comma, that's is correct??? - float, duble, long duble have commas)

31 in base10 = 1 ??? how you did?
Then what is 153 and 45672 in base 10 ?

PHP Code:
base 2 [0,1]        
base 3 [0,1,2]        
base 4 [0,1,2,3]        
base 5 [0,1,2,3,4]
base 6 [0,1,2,3,4,5]
base 7 [0,1,2,3,4,5,6]        
base 8 [0,1,2,3,4,5,6,7]        
base 9 [0,1,2,3,4,5,6,7,8]        
base 10 [0,1,2,3,4,5,6,7,8,9]
base 11 [0 to 9A]
base 12 [0 to 9A,B]
base 13 [0 to 9A,B,C]
base 14 [0 to 9A,B,C,D]
base 15 [0 to 9A,B,C,D,E]
base 16 [0 to 9A,B,C,D,E,F]
base 20 [0 to 9A,B,C,D,E,F,G,H,J,K

Last edited by z0mbiland; 12-12-2015 at 16:29.
z0mbiland is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-12-2015 , 18:52   Re: a question about a appz in c++
Reply With Quote #7

Quote:
Originally Posted by z0mbiland View Post
Now i understood what this "10" is!

I have another question:

In this "n = 31 / 10 = 3" = 3.1 (because i use int or long the number will be represented without comma, that's is correct??? - float, duble, long duble have commas)
At this case yes, but you could do type cast to double and store 3.1 at a double variable, for example.

Quote:
Originally Posted by z0mbiland View Post
31 in base10 = 1 ??? how you did?
Then what is 153 and 45672 in base 10 ?
153 and 45672, at what base? Base 11? 123?

I need a origin base to convert it to base 10.

Quote:
Originally Posted by z0mbiland View Post
31 in base10 = 1 ??? how you did?
Where I did "31 in base10 = 1"?

I said that "c = 31 % 10 = 1" = "c = n % 10;" is how to decompose using any number at any base at that algorithm. It is not converting a number from a base to any base.

See here about base conversion.

https://www.google.com.br/search?q=base+conversion+wiki

Last edited by addons_zz; 12-13-2015 at 04:20. Reason: spelling fixes and new better info
addons_zz is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 03:39.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode