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

• (2 Pages)
• 1
• 2

19 Replies - 1291 Views - Last Post: 22 June 2013 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323261&amp;s=6722056e8b1e5b4ea8af6588bc8c81da&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#16 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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

jasmin21patel, 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.

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;
}
```

#17 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• 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.

#18 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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];

}

```

#19 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• 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.

#20 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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.