Showing posts with label Arithmetic. Show all posts
Showing posts with label Arithmetic. Show all posts

Wednesday, April 8, 2009

Puzzle 14 – Multiplication or Addition.

Language – Java Type – Concept Last date 12-Apr-2009 12:00 p.m. IST Points 2

Mathematical rules dictate that multiplication takes precedence over addition. It’s called BODMAS. Somehow the Java folks don’t seem to agree. But then maybe you could give me a better explanation for this anomaly?

package com.twisters;

public class MulOrAdd {
public static void main(String[] args) {
int x=5;
x
*= 5+2;
System.out.println(
"Value of X is : "+x);
}
}



Got an answer? Why don’t you leave it here.

Wednesday, April 1, 2009

Puzzle 11 – Solution

The key to the solution is 'Character is treated as Int while doing arithmetic operations.'

When line 5 x=x+1; gets executed, the Ascii Value of 'x' is added to 1 and the result is an integer.
An integer value cannot be assigned to a char without explicit down casting so the correct way to increment the value of 'x' would be,

x=char (x+1);

Congrats to
1. Joe Smith
for getting the correct answer.

Sunday, March 29, 2009

Puzzle 11 – Alphabet Soup.

Language – Java | Type – Concept | Last date 01-Apr-2009 9:00 p.m. IST | Points 2

I always knew that characters could be represented as numbers. So when my little brother wanted to learn his alphabets I thought of writing up a program just to get all of them neatly printed. I started with this … but something went wrong. Can you help me out?

package com.twister;
public class TestOperator{
public static void main(String args[]) {
char x='a';
x
=x+1;
System.out.println(
"Value of x is " + x);
}
}

Got an answer? Why don’t you leave it here.


Sunday, March 8, 2009

Puzzle 5 – The Sign

Language – Java | Type – Concept | Last date 11-Mar-2009 9:00 p.m. IST | Points 2

Too many signs spoil the code …
package com.twisters;
public class Signs{
public static void main(String[] args) {
int result;
int a = 5, b =1;
result = a---b;
System.out.println("Value of a is : " + a);
System.out.println("Value of b is : " + b);
System.out.println("Value of result is : " + result);
}
}

Got an answer? Why don’t you leave it here.