Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find pin
Example input4=38 
if input1=3521 
input2=2452 
input3=1352 
then 
pin.=2+2+4+2+2 =l2


What I have tried:

public class Main {
    public static void main(String[] args) {
        int input1 = 3521;
        int input2 = 2452;
        int input3 = 1352;
        int input4 = 38;

        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;

        while (input1 != 0) {
            sum1 += input1 % 10;
            input1 /= 10;
        }

        while (input2 != 0) {
            sum2 += input2 % 10;
            input2 /= 10;
        }

        while (input3 != 0) {
            sum3 += input3 % 10;
            input3 /= 10;
        }

        while (input4 != 0) {
            sum4 += input4 % 10;
            input4 /= 10;
        }

        int pin = sum1 + sum2 + sum3 + sum4;
        System.out.println("The PIN is: " + pin);
    }
}
Posted
Updated 9-Feb-23 20:24pm
v3
Comments
Richard MacCutchan 10-Feb-23 6:10am    
What is the problem? As far as I can see the code does what you want.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
And the code you show has nothing to do with the task you describe, even as loosely as you have described it.

To get a value for pin you have to do two things:
1) Find the data pin=<expression> and extract it from the rest of the input.
2) Evaluate the <expression>

I'd start by reading the actual assignment carefully - it probably contains information that would make both tasks a lot simpler ...

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900