Puzzle5
![]() ![]() ![]() |
Puzzle5
| sree |
Aug 25 2006, 08:42 PM
Post
#1
|
|
CORPORAL ![]() Group: Members Posts: 80 Joined: 20-August 06 From: Hyderabad Member No.: 77 |
write simple program to add the digits in the no.. write the smallest program
(we have to add the digits in the no. until we get the single digit) |
| sree |
Aug 25 2006, 08:43 PM
Post
#2
|
|
CORPORAL ![]() Group: Members Posts: 80 Joined: 20-August 06 From: Hyderabad Member No.: 77 |
i think baku ll b the 1st 2 get ans 4 this !!!!
|
| bakuryu |
Aug 25 2006, 10:27 PM
Post
#3
|
|
SPECIALIST ![]() Group: Members Posts: 32 Joined: 14-June 06 From: India Member No.: 51 |
The smallest program in JAVA for sum of digits will be :
CODE import java.io.*; class puzzle { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); int sum = 0; for (int x = Integer.parseInt(br.readLine());x > 0; sum += x%10, x /= 10 ); System.out.println("The sum is : " + sum); } } However I think this is not what is wanted :: QUOTE (we have to add the digits in the no. until we get the single digit) so the sum of digits except the last digit or the first digit ?? |
| Rocky |
Aug 26 2006, 03:15 PM
Post
#4
|
|
Team CT! ![]() Group: Team CoolTalkz Posts: 156 Joined: 4-July 06 From: KAGAL (MAHARASHTRA) Member No.: 58 |
Any One understand tht Code?
|
| ymhatre |
Aug 27 2006, 08:14 AM
Post
#5
|
|
SERGEANT ![]() Group: Moderators Posts: 160 Joined: 10-June 06 Member No.: 36 |
Yes the code is simple...
Wht sree has asked is....if the no. is 58965 then adding the digits we wuld get 5+8+9+6+5 = 33 now since 33 is also two digit no...the final answer would be 3 + 3 = 6 Now since i had recently formatted my PC...all my compliers have gone... so i have not complied the program...but he logic is made clear to u guys n also now no more Computer Programing subject is with me...I neither need them.... Now im a HARDCORE EXTC enggineer...LOL |
| christo |
Aug 27 2006, 03:13 PM
Post
#6
|
|
PRIVATE FIRST CLASS ![]() Group: Members Posts: 18 Joined: 23-August 06 Member No.: 89 |
C Code..
int puzzle(int n){ s=0; for(;n>0;s+=n%10,n/=10); return(s); } |
| ymhatre |
Aug 28 2006, 12:22 AM
Post
#7
|
|
SERGEANT ![]() Group: Moderators Posts: 160 Joined: 10-June 06 Member No.: 36 |
No christo according to me thts not correct....
We need to pass s, again so tht we can further reduce it to one digit. |
| christo |
Aug 28 2006, 06:26 PM
Post
#8
|
|
PRIVATE FIRST CLASS ![]() Group: Members Posts: 18 Joined: 23-August 06 Member No.: 89 |
Thnx Yogesh..
Wat abt dis int puzzle(int n){ int s=0; if(n<10) return(n); for(;n>0;s+=n%10,n/=10); return(puzzle(s)); } |
| christo |
Aug 28 2006, 06:43 PM
Post
#9
|
|
PRIVATE FIRST CLASS ![]() Group: Members Posts: 18 Joined: 23-August 06 Member No.: 89 |
hahaha
|
| bakuryu |
Aug 31 2006, 04:36 PM
Post
#10
|
|
SPECIALIST ![]() Group: Members Posts: 32 Joined: 14-June 06 From: India Member No.: 51 |
I think we are asked to write the smallest program.
|
![]() ![]() ![]() |
| Lo-Fi Version | Time is now: 10th September 2010 - 02:03 AM |