CoolTalkz.com

Welcome Guest ( Log In | Register )

2 Pages  1 2 > 
Reply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

> Puzzle5

sree
post Aug 25 2006, 08:42 PM
Post #1


CORPORAL
Group Icon

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)


User is offlineProfile CardPM
Go to the top of the page
+Quote Post
sree
post Aug 25 2006, 08:43 PM
Post #2


CORPORAL
Group Icon

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 !!!!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
bakuryu
post Aug 25 2006, 10:27 PM
Post #3


SPECIALIST
Group Icon

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 ??

User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Rocky
post Aug 26 2006, 03:15 PM
Post #4


Team CT!
Group Icon

Group: Team CoolTalkz
Posts: 156
Joined: 4-July 06
From: KAGAL (MAHARASHTRA)
Member No.: 58



Any One understand tht Code?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
ymhatre
post Aug 27 2006, 08:14 AM
Post #5


SERGEANT
Group Icon

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
christo
post Aug 27 2006, 03:13 PM
Post #6


PRIVATE FIRST CLASS
Group Icon

Group: Members
Posts: 18
Joined: 23-August 06
Member No.: 89



C Code.. smile.gif


int puzzle(int n){
s=0;
for(;n>0;s+=n%10,n/=10);
return(s);
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
ymhatre
post Aug 28 2006, 12:22 AM
Post #7


SERGEANT
Group Icon

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.

User is offlineProfile CardPM
Go to the top of the page
+Quote Post
christo
post Aug 28 2006, 06:26 PM
Post #8


PRIVATE FIRST CLASS
Group Icon

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));
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
christo
post Aug 28 2006, 06:43 PM
Post #9


PRIVATE FIRST CLASS
Group Icon

Group: Members
Posts: 18
Joined: 23-August 06
Member No.: 89



hahaha
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
bakuryu
post Aug 31 2006, 04:36 PM
Post #10


SPECIALIST
Group Icon

Group: Members
Posts: 32
Joined: 14-June 06
From: India
Member No.: 51



I think we are asked to write the smallest program.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

2 Pages  1 2 >
Reply to this topicTopic OptionsStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
 

Lo-Fi Version Time is now: 10th September 2010 - 02:03 AM
Copyright © 2006 - 2007 CoolTalkz.com GameTalkz.com GreatDir.com & TechTalkz.com