how to use modulus

Mar 16, 2013 at 8:19pm
if i have 4 intergers for example :
1378 2496 3587 5389
how can i use the modulus operator to disect them so i can get the first number of each integer
what i mean is how can i take 1 form 1378 and 2 from 2496 etc...
Mar 16, 2013 at 8:27pm
I'm not sure you can use the modulo/us operator to do it. You should divide the numbers:

1
2
3
4
5
int Number = 1378;
while(Number > 9)
{
    Number /= 10;
}
Last edited on Mar 16, 2013 at 8:28pm
Mar 16, 2013 at 8:43pm
.
Last edited on Mar 17, 2013 at 12:17pm
Mar 16, 2013 at 8:46pm
.
Last edited on Mar 17, 2013 at 12:18pm
Mar 16, 2013 at 9:22pm
so any help on how to do it ???
Mar 16, 2013 at 10:58pm
You can get each number in this way:

1
2
3
4
5
6
7
8
9
10
int Value = 137;
if(Value < 100 || Value > 999)
{/*Error*/}
else
{
    int Votes[3] = {0};
    Value -= (Votes[2] = (Value % 10) / 1);
    Value -= (Votes[1] = (Value % 100) / 10);
    Value -= (Votes[0] = (Value % 1000) / 100);
}


I haven't tested but this should work.
Votes[0] will have the first number (1)
Votes[1] will have the second number (3)
Votes[2] will have the third number (7)

To receive multiple inputs edit line 2 to switch "ranges", set the Size of the Votes array to the required number of votes and add more lines to the "Value -= (Votes[x] = ..." who follow those logics.
Last edited on Mar 16, 2013 at 11:00pm
Mar 17, 2013 at 3:12am
Take the first integer number 1378%10==8(do something) now divide by 10 and you have 137(integer division)
137%10=7(do something) now divide 137 by 10 and you have 13.
13%10 = 3(do something) now divide by 10 and you have 1.

Put the above in a loop and when you isolate each individual digit call a function to carry out each simple calculation.
If you place these results in a container such as a vector or even an array element [0] would be contestant 1, element [1] would be contestant 2. ... and son. It would then be a simple task to print the five individual results and that of the winner.
Mar 17, 2013 at 3:26am
- They way your problem is presented, i would just have them enter the number as a string, then cast each character into an array or something along those lines.
Mar 17, 2013 at 9:21am
thanks everyone for the reply ...i cant use arrays because we didnt learn them yet
buffbill can u give me hint or a pseudocode on how to write it ???
Mar 17, 2013 at 1:25pm
Well then:

1
2
3
4
5
6
7
8
9
10
11
12
int Value = 137;
if(Value < 100 || Value > 999)
{/*Error*/}
else
{
    int First = 0;
    int Second = 0;
    int Third = 0;
    Value -= (Third = (Value % 10) / 1);
    Value -= (Second = (Value % 100) / 10);
    Value -= (First = (Value % 1000) / 100);
}
Mar 17, 2013 at 6:10pm
yeahhh thank you so much essgeeich it worked finally
Mar 17, 2013 at 6:56pm
...
Last edited on Mar 23, 2013 at 3:15pm
Mar 17, 2013 at 6:57pm
here is what i get at first but the instructor told me the number should be inmputs ...and i should use modulus :

# include <iostream>
using namespace std;
int main()
{
int c=1; // candidate number
int a,b,d,s; // a:grade for beauty b:grade for elegance d: grade for education s:result for candidates
int max_result=0;
int winner=0;
while (c<=5)
{
cout<<"enter the grade for beauty:";
cin>>a;
cout<<"enter the grade for elgance:";
cin>>b;
cout<<"enter the grade for education:";
cin>>d;
cout<<c;
c++;
cout<<a;
cout<<b;
cout<<d<<endl;
a=a*5;
b=b*2;
d=d*3;
s=a+b+d;
cout<<"result of the candidate is\t"<<s<<endl;
if(s>max_result)
{
max_result=s;
winner=c-1;
}
}
cout<<"candidate\t"<<winner<<" \t win with score of "<<max_result<<endl;
}
Mar 17, 2013 at 7:24pm
You are receiving each number in a different input...
This had nothing to do with the question, you asked how to take an arbitrary digit from a number.
Mar 17, 2013 at 7:26pm
well my point was how to use modulus in this case
i was thinking of it all day but im not able to do it
so can u help me write the program ??
Mar 17, 2013 at 8:03pm
I cannot help much, all I could do I did, you have the instructions some posts over this.

I really cannot help anymore, you have your answer right there, and by the way we should not answer homeworks with the entire answer.

But as you can see in that program above:
Each vote is being received on a separate line.

You are not getting an input like 137, you are getting 1, 3, then 7.
Mar 17, 2013 at 8:10pm
ok thank you for the help
i will figure out how to do it
Topic archived. No new replies allowed.