i need some guidance on how to add digit at odd position together.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 511 Views - Last Post: 22 June 2013 - 06:33 PM Rate Topic: -----

#16 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: i need some guidance on how to add digit at odd position together.

Posted 21 June 2013 - 11:40 AM

View Postjasmin21patel, on 19 June 2013 - 05:49 PM, said:

as for example if i have int a = 123456 how can i add 135. i dont have code because i can not think how to do it so some guidance will be very helpful if you will explain through code that would be big help.

thank you in advance.


You will Need

while(num!=0){
dig[k]=ret(num);	
num=(num-dig[k])/10;
k++;
kkk=k;
}


AND

static int ret(int i){
	
	return i%10;
}

Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: i need some guidance on how to add digit at odd position together.

Posted 21 June 2013 - 11:49 AM

The subtraction is unnecessary: num=(num-dig[k])/10;. Division by 10 will be sufficient. Also, it seems a waste to use a method just to return i % 10. There is no benefit gained from using a method in this case.
Was This Post Helpful? 0
  • +
  • -

#18 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: i need some guidance on how to add digit at odd position together.

Posted 22 June 2013 - 05:36 PM

Here is the Skeleton


 
static int ret(int i){
	
	return i%10;
}
	public static void main(String[] args) {
System.out.println("ENTER The Number");
int num=in.nextInt();
String str=String.valueOf(num);
int dig[]=new int[str.length()];
int k=0;int kkk = 0;
while(num!=0){
dig[k]=ret(num);	
num=(num-dig[k])/10;
k++;
kkk=k;
}
for(int i:dig)System.out.print(i+" ");
 
int rev[]=new int[kkk];

}
 

Was This Post Helpful? -1
  • +
  • -

#19 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: i need some guidance on how to add digit at odd position together.

Posted 22 June 2013 - 06:28 PM

That's still really bulky for the task assigned. Using math on chars is more efficient, as has already been pointed out. Also, as I mentioned before, please adopt a readable indentation convention. You're not a new member and you've been posting in the Java forum for a while, so it's not like you're new to programming. So I'm not sure why you're resistant to writing legible code. If it's hard for our more experienced users to read your code, it's going to be hard for a newbie or someone asking for help to read it.
Was This Post Helpful? 0
  • +
  • -

#20 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: i need some guidance on how to add digit at odd position together.

Posted 22 June 2013 - 06:33 PM

Thanks, I will do My Best to stick to your rectifications and suggestions.

From now on, It's time for me to Change.

Thanks for taking your time.

Have a Nice Day !
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2