d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Help With A Java Prob
1234Next
Add Reply New Topic New Poll
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 1 2014 02:52pm
So, my #1 problem was this and I works.

1. Write a program to read 5 integer numbers. If the number is between 1 and 500, calculate the square of that number and the cube of it. If its not between 1-500 , just don't do it.

Code
int iCinqNbr, iNbr, iCube, iCarre;
DecimalFormat df = new DecimalFormat("0.00");

for ( iCinqNbr = 0; iCinqNbr < 5; iCinqNbr++) {

iNbr=Integer.parseInt(JOptionPane.showInputDialog("Entrer une première valeur!"));

if (( iNbr >= 1) && ( iNbr <= 500 )){
iCarre=iNbr*iNbr;
iCube=iNbr*iNbr*iNbr;
System.out.println("Le carré de "+iNbr+" est de "+iCarre+" et son cube est de "+iCube+".");}
else
System.out.println("Le nombre restera le même.");


This works ..... now with my #2 , that's where I need help ..

I need to take this code, and make it so it doesn't calculate the square and cube if it's a multiple of 10.

Do I need to do another FOR or IF inside, im lost ..
Retired Moderator
Posts: 18,570
Joined: Feb 27 2004
Gold: 11,858.00
Trader: Trusted
Oct 1 2014 03:02pm
Use modulus.

You're going to take the number you're checking against 10 and see if the remainder is 0. If it is then its a multiple of 10.
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 1 2014 06:00pm
Quote (Blind[zF&#93; @ Oct 1 2014 05:02pm)
Use modulus.

You're going to take the number you're checking against 10 and see if the remainder is 0. If it is then its a multiple of 10.


Yup it works thanks.

I've got 1 last question in my homework that im blocking at a certain point.

I need to find the factorial of 1 to 10... which I did. But on my System.out ........... I need it to be like that : The Factorial of 4 is = 4*3*2*1

I just don't know how I can get the 4*3*2*1 with my code.

Code
int fact = 1, iValeur;
for (iValeur =1; iValeur <= 10; iValeur++) {


fact *= iValeur;

System.out.println("La valeur factorielle de "+iValeur+" est "+ "soit" +fact);
}

Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 1 2014 06:35pm
Quote (Jibbosh @ Oct 1 2014 06:00pm)
Yup it works thanks.

I've got 1 last question in my homework that im blocking at a certain point.

I need to find the factorial of 1 to 10... which I did. But on my System.out ...........              I need it to be like that : The Factorial of 4 is = 4*3*2*1

I just don't know how I can get the 4*3*2*1 with my code.

Code
int fact = 1, iValeur;
        for (iValeur =1; iValeur <= 10; iValeur++) {
           
         
          fact *= iValeur;
           
            System.out.println("La valeur factorielle de "+iValeur+" est "+ "soit" +fact);
        }


I think..

Code
int fact = 1, i;
String temp = "";

for (i = 1; i <= 10; i++) {

fact *= i;

if (temp == "")
temp = i;
else
temp = i + "*" + temp;

System.out.println("The factorial of " + i + "is = " + temp);
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 1 2014 06:41pm
Code
temp = i;


I'm not sure that compiles, but the logic looks about right. essentially build up a string in the loop, and only do a print statement once outside of the loop
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 1 2014 06:45pm
Quote (carteblanche @ Oct 1 2014 06:41pm)
Code
temp = i;


I'm not sure that compiles, but the logic looks about right. essentially build up a string in the loop, and only do a print statement once outside of the loop


fairly certain the compiler knows how to cast it

Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 1 2014 06:51pm
Quote (Aimed_Shot @ Oct 1 2014 08:35pm)
I think..

Code
int fact = 1, i;
String temp = "";

for (i = 1; i <= 10; i++) {

fact *= i;

if (temp == "")
  temp = i;
else
  temp = i + "*" + temp;

System.out.println("The factorial of " + i + "is = " + temp);
}


With this, it tells me : temp= i cannot convert from int to string.

This post was edited by Jibbosh on Oct 1 2014 06:51pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 1 2014 06:53pm
Quote (Aimed_Shot @ Oct 1 2014 08:45pm)
fairly certain the compiler knows how to cast it


Not in java. it would work in javascript though.

This post was edited by carteblanche on Oct 1 2014 06:58pm
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 1 2014 06:53pm
Quote (Jibbosh @ Oct 1 2014 06:51pm)
With this, it tells me : temp= i cannot convert from int to string.


do
Code
temp = Integer.toString(i);


e: i'm not sure if that's the most efficient way to cast it, but for looping 10 times it's fine lol


This post was edited by Aimed_Shot on Oct 1 2014 06:59pm
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 1 2014 06:59pm
Quote (Aimed_Shot @ Oct 1 2014 08:53pm)
do
Code
temp = Integer.toString(i);


Works perfectly, thanks!
Go Back To Programming & Development Topic List
1234Next
Add Reply New Topic New Poll