Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

can anybody help me with java program


  • Please log in to reply

#1
mozala

mozala

    New Member

  • Member
  • Pip
  • 8 posts
I have to write function that will write out all integers between 0 and 999 which sum os all digits equal to a givev integer "x" (-1<x<28): eg. input: x=2, output= 2,11,20,101,110,200.)

ma email is [email protected] or [email protected]
thanks mozala
  • 0

Advertisements


#2
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

What code do you have written so far? If you could provide us with your code so far, we can help you figure out the steps that you can do and with which steps you need help.

Cheers,
Tom

I have to write function that will write out all integers between 0 and 999 which sum os all digits equal to a givev integer "x" (-1<x<28): eg. input: x=2, output= 2,11,20,101,110,200.)

ma email is [email protected] or [email protected]
thanks mozala


  • 0

#3
mozala

mozala

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts

Hello,

What code do you have written so far? If you could provide us with your code so far, we can help you figure out the steps that you can do and with which steps you need help.

Cheers,
Tom


This is what i have tried so far and im in the middle of its half way maybe far from the end and have no idea how to continue .And just so u know im a beginner so...
Anyway thanks for ur interest...

///////////////////////////////////////////////////////////////////////////////////////////////////


public class tutorial {
static final int m = 1000;
public static void main(String[] args) {
int input;
int x,y;
Sys.pln("Type in an input between 0 and 27 ");
input =Sys.readInt();
do
{
if ((input <0) ||(input >27))
Sys.pln(" its not valid ! try again");
}while (input !=input);
for (int i=0;i < m; i++){
x = input%10;
y = ( input/10 ) + x;
Sys.pln("Here is the list of the sum:"+y);

}

tutorial tutorial1 = new tutorial();
}

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  • 0

#4
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

I can provide with a short simple program that will show you how to "extract" each digit in a given number that is less than 1000 (e.g. 0 ... 999). This depends on the use of the "/" function in Java with integers. When you divide integers in java and return that value to an integer, it will store the number of times the value could be divided wholly by the smaller number. Here is short example snippet:
int x = 21/5;
x = 4;

So, in your case, we want to know each digit in the number which corresponds to powers of 10. We have to work with the "hundreds" digit, the "tens" digit and the "ones" digit. To get each of these digits, we simply have to divide by 100, 10, and 1 respectively. We want to start with the "hundreds" digit first so we can eliminate that digit before proceding to the "tens" and then the "ones". Here is a working program that shows you this. I have included a commented out loop featuring the 1000 limit and that performs the finding of all numbers less than 1000 with digits that add up to a given amount. You can add in the user input code.

//**********************************
import java.util.*;
import java.io.*;

public class test_tutorial {

public static void main(String args[]) {
int max_int = 1000;

/*
int sum_of_digits = 5; // change this to user input
int digit_total = 0;
int temp_var = 0;
for (int i=0;i < max_int; i++){
temp_var = i;
digit_total = 0;
digit_total = digit_total + (temp_var/100);
temp_var = temp_var - (temp_var/100)*100;
digit_total = digit_total + (temp_var/10);
temp_var = temp_var - (temp_var/10)*10;
digit_total = digit_total + temp_var;
if (digit_total == sum_of_digits) System.out.println("Found digit: " + i);
}
*/

int x = 202;
System.out.println("x = " + x);
System.out.println("x / 100 = " + (x/100));
x = x - (x/100)*100;
System.out.println("x / 10 = " + (x/10));
x = x - (x/10)*10;
System.out.println("x / 1 = " + x);
}

} //END PROG
//*********************************************

Cheers,
Tom

Edited by gust0208, 07 January 2006 - 01:25 PM.

  • 0

#5
mozala

mozala

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts

Hello,

Would you be able to provide your import statements? I can't compile your code without the import statements and I need to brush myself up on the Sys.pln and Sys.p calls.

Cheers,
Tom


I have used sugar library for input /output classes ,u can download it from here:
http://cs.felk.cvut....erezovs/x36alg/

u can either take sugar zip or just sugar classes...
  • 0

#6
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

I modified my post about the import statements with a sample program showing you how to solve this problem and a for loop that performs the task you stated before. Take a look at my post just before this one.

Cheers,
Tom
  • 0

#7
mozala

mozala

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
wow !!!its amazing im going thru it now and i'll get back to u ,in case anything is not clear to me.
thanks
mozala
  • 0

#8
mozala

mozala

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts

Hello,

I modified my post about the import statements with a sample program showing you how to solve this problem and a for loop that performs the task you stated before. Take a look at my post just before this one.

Cheers,
Tom


Hi. im back again
Since u r using different IO library , for the input what statement do u use...??
  • 0

#9
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

You can use the Sugar libraries you were using before, just replace my System.out.println statements with the Sys.pln and you can add in the user input statements. If you are still having problems, post here and I can take a look at the Sugar libraries.

Cheers,
Tom

Hi. im back again
Since u r using different IO library , for the input what statement do u use...??


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP