[Show all top banners]

pat
Replies to this thread:

More by pat
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java help
[VIEWED 6208 TIMES]
SAVE! for ease of future access.
Posted on 09-16-06 7:58 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Can someone help me with this question....
#Write a program that reads in a four-digit number (such as 1998) and outputs the number, one digit per line, like so:
1
9
9
8

thanks!!!!!!!!!!!!!!!!!!!!
P.S. didn't want to open a new thread for this... u can watch some of the hollywood and bollywood movies online if you are interested like harry potter and the goblet of fire, scary movie 4, pink panther, golmaal, krrish, etc ....here is the link....

www.justkooky.com

 
Posted on 09-16-06 10:21 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

/ * C++ Program that prompts user to input 4 digits between the range of 1000 and 9999 and outputs the digits one per line. Very inefficient algorithm but works perfectly.

Please look at the algorithm. It would be same for the Java.

Basically you just have to use Divison and Modolus operation.

Initially, I tried to input the 4 digits in an array (somehow !) and return the index value but could not figure it out how to. You might wanna do that too.

Author : Nas :)


*/





#include
using namespace std;

main(){
cout<<"Please input 4 digits : ";
int input;

cin>>input;
if(input<1000 || input>9999){
cout<<"Please input 4 digits between 1000 and 9999 only. "< cout<<"Terminating program. Please try again. "< exit(1);
}
else{
int a=(input/1000);
cout<
int b=(input%1000);
int c=(b/100);
cout<
int d=(input%100);
int e=(d/10);
cout<
int f=(input%10);
cout< }
return 0;
}



After testing the Driver.

Please input 4 digits : 4682
4
6
8
2

When user inputs illegal digits.

Please input 4 digits : 99999
Please input 4 digits between 1000 and 9999 only.
Terminating program. Please try again.
 
Posted on 09-16-06 10:22 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Pat, that hollywood and bollywood thing is the "ghush" or just wanted to give away something to the one who helps you?

If you really want to java, then JAVA! No hollywood or bollywood! :

Here's it anyway. Hope it helps. :)

import java.util.Scanner;
public class NumberOutput
{
public static void main( String args[])
{
int number;
int digit1;
int digit2;
int digit3;
int digit4;

Scanner input = new Scanner( System.in );
System.out.print( "Enter the number: ");
number = input.nextInt();

digit1 = number / 1000;
digit2 = number % 1000 / 100;
digit3 = number % 1000 % 100 / 10;
digit4 = number % 1000 % 100 % 10;

System.out.printf("\nThe first digit is: %d", digit1 );
System.out.printf("\nThe second digit is: %d", digit2 );
System.out.printf("\nThe third digit is: %d", digit3 );
System.out.printf("\nThe fourth digit is: %d\n", digit4 );

}
}
 
Posted on 09-16-06 10:23 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

#include
using namespace std;

main(){
cout<<"Please input 4 digits : ";
int input;

cin>>input;
if(input<1000 ||input>9999){
cout<<"Please input 4 digits between 1000 and 9999 only. "< cout<<"Terminating program. Please try again. "< exit(1);
}
else{
int a=(input/1000);
cout<
int b=(input%1000);
int c=(b/100);
cout<
int d=(input%100);
int e=(d/10);
cout<
int f=(input%10);
cout< }
return 0;
}
 
Posted on 09-16-06 10:26 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This the output!

 
Posted on 09-16-06 10:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Some how this web is not displaying my codes properly !
Its omitting my cout display message !

Anyway you got the answer by slackdemic in Java :)

Slackdemic, how would you read the 4 digits and input each digit in an array? Is there a way?

Prompt the user to input (lets say ) 30 digits.
123.............09

Read these digits into an array and display it when we call the index value such that
array[0]=1
array[2]=2
...
array[29]=9

Our code would be really ineffecient to execute this program !
 
Posted on 09-16-06 10:53 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I am not into C++, but there is a way in java, but it's time for me to get off at work. If I do not turn the computer on at home, it will only be tomorrow before I can answer you.

Gotta go!

Slackdemic
 
Posted on 09-16-06 10:56 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

You could write it in java bro, i will convert it into C++.
 
Posted on 09-17-06 2:59 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

initially, whether its Java or C, you can always loop to the length(number of digits) of the input number and increase the mod and division depth as you loop.

or, if you have been into recursion, you may try this.

public static void myMethod(int number)
{
if (number < 10)
System.out.println(number);
else
{
myMethod(number/10);
System.out.println(number % 10);
}
}

i have not tested this code though - i guess you get the logic.
 
Posted on 09-17-06 3:32 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

If all you need is an ouput of four or an indefinite number of digits:

import java.util.Scanner;
public class NumOutputter {
public static void main( String args[] ) {

System.out.println("Enter a number: ");

StringBuilder num = new StringBuilder();
num.append(new Scanner( System.in ).next());
for( int i=0; i System.out.println(num.charAt(i));
}
}
}
 
Posted on 09-17-06 7:57 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thanks slacdemic...appreciate it.....

 
Posted on 09-17-06 8:12 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thanks again...It worked.....

 
Posted on 09-17-06 9:21 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Nas,
why don't u just define a an array of required size and call a "for" loop and input all the values using that for loop. Say...
//---------------------------------------------
int/float/char a; //depending on whether u want integer, float or characters.
const int arraySize=wateverNumber;
int array[arraySize];
for(int i=0;i cin>>a;
array[i]=a;
}

I hope thats what you are looking for...if not pop up another question.
 
Posted on 09-17-06 11:54 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Himalayantiger,
I know what you mean bro but my question was little different.

I want to prompt user to input 30 digits value just once. Something like..

Please input the value and hit enter: 1535................................9
Output is:
The first digit is is 1
The second digit is 5
.....
The 30th digit is 9

Your algorithm works only when we prompt user to input multipile times such that each value is stored in each index of the array.

for example:

Please input 1st value: 1
please input 2nd value:5
..
..
Please input 30th value:9

Output:

When we promput user to input the value like this it stores each elements in each indexes of the array and we call the index number to output each element.

How would you solve my problem bro?
 
Posted on 09-17-06 3:08 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

java jamana ma garya, birshyo... but, yetti gare pugdaina ra tyo program?

 
Posted on 09-17-06 11:42 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Nas,
You don't need an array to iterate through a value if you use String capabilities in Java.

Kalank,
Since String is immutable and no thread-safety is needed I'd use StringBuilder instead. Try iterating 10000 times using a String VS StringBuilder and you'll notice the efficiency aspect of it.
 
Posted on 09-18-06 12:04 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

JavaBeans, I should ...
Took Java a long time ago and I don't quite remember using StringBuilder eh. Although I remember using it on C#. Hmmm ... I haven't updated myself, and freakin' time flies. I should practice Java one of these days...
Thanks Tho.

I thought it was better than what I had in my memory from programmin' days. :P

Something like, u ask the user to enter a string... like this

String name = new String();
char c;
blah("Enter your name: ");
c=(char)System.in.read();
while(c != '\n')
{concat. each character to string}
and print out the name..

LOL, how about that for a change. :D
 
Posted on 09-18-06 12:59 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

kalanki bro u changed so much?:O and u say u havent updated urself?;oP hehe..time might fly..but u dun seem to be that behind it if u ask me ;o) hehe..esp comparin to me ;oP..

i never had heard of Scanner nor StringBuilder..until i read this thread :o|

i would have come out with a similiar codes(i dun plagirise blindly hai ;oP..ali ali change garchu ni ;oP) like ur above one..i feel refreshed!;oP hehe..

anyways got the song yet?..hope so..

good day!:oD
 
Posted on 09-18-06 1:16 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

ahaaa Anon biradar, Long time.
I could've asked you, but I saw you were already asking for help (computer devices help) ehe... now I can not ask for help to a guy who is already asking for help, can I? ... and beside that, I knew you needed bigger help with gettin' rid of all the hot ladies wearin' em low-cut jeans and causin' scene all over the places.
What song are you talkin' about? "Papa don't preach"? Lol. hahhoa!!
Anyway good to see u, now you can go ahead and resume what you were doing earlier. eh... bang ya head ... against ya girl's mahogany bed... and beg for mercy.. :P (For misspellin' her name on a tatto at the left of your right wrist)... Sorry seems to be hardest word, and in a time like this, it freakin' FLOWS. :P
 
Posted on 09-18-06 1:49 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

big K!;oP hehe

oh that computer devices thing?hah!its some lame thingy that happened then..in another 1hr i be needn 'help' again!tho not really sure wat it will be about this time :oS..

but yeah nothn wrong in askn for help?even tho the other person seems to need help in some other things..and tho he seems useless?;oP hehe..well smtimes ke tha hoina?weird cha world ni..useless pani khoi khoi bela useful ni huncha ;oP..

and oh yah for a while there u made me think i was hallucinatin so went to check ur profile and hence check ur old posts!!!now im thinkin is it me!or is it u hallucinating?;oP hehe...ke bhancha bhanya?:o| ur imaginations really wild today?or has it always been like this?:oS ;oP hehe..

papa dun preach re?hah!me dad did that recently!;oP lucky mum was there later for me re kya ;o) hehe..and oh before i forget it was the that 'relimai' song..got it yet?hope so :oD

that song i guess does bring memories..thats why i remembered!;oP hehe..ma syano hunda(not like i grown that much have i?hah ;oP)...euta didi le concert ma gako..and that was the 1st time i heard that song..and of cos can remember that didi really well..cos ( i was too young to even have crush then so yeah didnt have a crush on her err i think ;oP hehe)..but the dais all sure had a crush on her!used some of me frens as messengers ..no one used me :o( ;oP hehe...

but sacchi who told u about the tattoo i onced nearly did?;oP hehe...im a still a virgin in tattoos!:$ ;oP hehemaan ta cha garney..tara teytikai hoina..herum ke huncha :oD..might be tattooless forever?but lookn at it..doesnt seem that bad..just will give reasons like oh mero skin ta too thick to be tattooed re :o| ;oP hehe..

...but yeah lookn back now..i really do think writin a name of a person on ur body(any parts? ;oP) its a pretty big decision..cos later if it doesnt work out..well u know..the next one(assumin there is a next one ;oP) might kinda not really like it?and esp if ur a stubborn git who likes not to get rid of things which might remind u of mistakes?cos mistakes teaches us doesnt it?;oP hehe..correctin mistakes..and covering/hiding mistakes..aint the same hoina?;oP hehe...

but yeah that sure will get u into some deep troubled water eh?;oP hehe thats why its always nice to have good frens around you to talk sense into ur thick numbskull?tho i think they have given up these days :oS hehe ..oh well..somethings are too tough to crack eh?;oP hehe..ok i dunno wat im typin now..was summarising as always ;oP hehe..

ma ta abo rush ma gaye(times up!:oS)..good luck there..and good luck again ;oP hehe dherai ke ke imagine garney hoina hai :o| if u imagine..errr hopefully its u there!not me!;oP hehe

good day!:oD
ps might try to learn C++ soon..hopefully :oS hehe..(hmm maybe i shud check on C#)..got any recommendation?books etc?;oP hehe..
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 60 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Toilet paper or water?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
I hope all the fake Nepali refugee get deported
From Trump “I will revoke TPS, and deport them back to their country.”
Tourist Visa - Seeking Suggestions and Guidance
advanced parole
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
MAGA denaturalization proposal!!
How to Retrieve a Copy of Domestic Violence Complaint???
wanna be ruled by stupid or an Idiot ?
Travel Document for TPS (approved)
All the Qatar ailines from Nepal canceled to USA
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters