Click here to Skip to main content
13,865,531 members
Rate this:
Please Sign up or sign in to vote.
See more:
Scanner sc = new Scanner(;

long sum = 0;

int N = sc.nextInt();

for (int i = 0; i < N; i++) {

final long x = sc.nextLong(); // read input

String str = Long.toString((long) Math.pow(1 << 1, x));

str = str.length() > 2 ? str.substring(str.length() - 2) : str;

sum += Integer.parseInt(str);


I want to write a equivalent python program
Can anyone help me understand the logic of this Java Code ?

4 8 6 7 4

Example 2

1 2 3


What I have tried:

n = int(input())
nums = list(map(int,input().split(' '))
while n>2:
Updated 10-Aug-18 0:30am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

So you have found some code that does what your homework asks, and you want it explained to you so you can submit it as your own work?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Instead of spending your time trying to find something that you can hand in, try reading the question carefully, and writing your own code. You will learn a whole load more that way!
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Member 13944678 10-Aug-18 4:47am
Sorry, but i'm not asking anyone to write the whole code for me.
I just want to understand what is happening in this code written in Java(I don't know Java)
OriginalGriff 10-Aug-18 4:54am
And why are you doing that?
Member 13944678 10-Aug-18 5:10am
I found a code in Java that i want to write in python using the same logic.
The problem is that i don't understand Java.
If i know what the coding is taking input , what operation it performs , output.
I can easily write it for python3.
Help me out !!
Nelek 10-Aug-18 8:27am
If you don't understand what the code does, how do know it doesn't what you need to
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should get together with your colleague who posted the question at[^]. I am sure between the two of you you can work it out.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Can someone explain the LOGIC of this code ?

use a debugger and watch the code as it execute, you will see by yourself what it does.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190214.1 | Last Updated 10 Aug 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100