how to use modulus

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...
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
.
Last edited on
.
Last edited on
so any help on how to do it ???
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
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.
- 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.
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 ???
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);
}
yeahhh thank you so much essgeeich it worked finally
...
Last edited on
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;
}
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.
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 ??
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.
ok thank you for the help
i will figure out how to do it
Topic archived. No new replies allowed.