helios there was a lot to this homework this is only a small part of it..I have completed the entire assignment up until this point..I do not know how to get back to the original number after applying this operation to the numbers.
giblit i need to get back the original numbers I have to do this equation in reverse.
@EssGeElch thanks for giving me an answer that was worth reading ...Yea there is no way.....I don't understand this homework question!!? What do they expect? Am I misinterpreting this line?
"Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. "
You need to play around with the operator. If you do that, you'll figure it out eventually.
Word of warning: you can't solve this problem by reducing the expression like you do with equations with elementary functions (i.e. x = y + 1 therefore y = x - 1). Modulo doesn't have an inverse operation because it's destructive. You have to use the properties of modulo:
* 0 <= n%m < m
* (n + a) % m == (n % m + a % m) % m
Corollary: (n + m) % m == n % m
Whoops, I meant at most 10 numbers far (0 to 9, a single digitt, exactly like the question says). This means it IS possible.
The basilar inverse equation is equivalent to:
x = (x+(10-7))%10
10 comes from the amount of numbers with a single digit (0 to 9).
7 comes from the number added previously.
Didn't test, but this should do the trick.
Play with the (10-7) value a bit and watch how the values slowly go off the track.
Because you care to help them understand how the solution works.
Anyways OP I'm not sure where you are getting reverse from
"Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. "
This sounds to me add 7 to the digit then get the remainder of that number or in other words digit = (digit + 7) mod 10 like you have in your first post
Maybe if you posted the actual assignment we would have a clearer idea on what you are supposed to be doing because I am a bit confused with what you are saying and the snippet
*EDIT I see what you are talking about. The assignment is to encrypt a text file by adding 7 to it and then getting mod 10.
Thanks for your help im at work right now..But the assignment is to take and encrypt a 4 digit number so like 1234 and then apply that quote from the text above to each number. so encypt it then do the reverse to decrypt
That's not really the solution. You should end up with no ifs in the loop.
What is the order of num[i] = (num[i] + (10 - 7) % 10); ?
What happens first and what happens last?
I had to add the if statements because the the 4 digit numbers have to be under 10.. so like
9 9 9 9...
I would get numbers over 9 after applying that equation you gave me, so...somehow dividing the numbers that were over 10 worked some numbers needed to a rounded up a bit after the division..if one of the numbers started out as a 0 it would become 10 during the encryption process...
And thats my reasoning behind those if statements.. And the program works great
If you are interested here is my final program it is written in Java.
//Frank Novello
//9/28/14
//Purpose: To encrypt/decrypt a 4 digit number and output to screen.
import java.util.Scanner;
publicclass main {
publicstaticvoid main(String[] args) {
int num = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a 4 digit number");
while(num < 1000 || num > 9999 ){
num = sc.nextInt();
if(num < 1000)
{
System.out.println("Error: Number is less than 4 digits");
}
elseif(num > 9999)
{
System.out.println("Error: Number is more than 4 digits");
}
}
Encryption ecrypt = new Encryption(num);
ecrypt.print();
ecrypt.decrypt();
System.out.println();
ecrypt.print();
}
}
Still you must end up with no ifs.
If you cannot understand how to keep a number within 0-9 without ifs, look up what the modulo operator does, since it'll do everything for you.
Also there is a tiny important difference between my, uh, solution and yours.