0% found this document useful (0 votes)
60 views5 pages

Assignment Solutions - Java Variables and Data Types PDF

This document contains 5 Java code examples that demonstrate basic programming concepts: 1) Printing the product of two integer variables 2) Printing the ASCII value of a character 3) Printing the area of a rectangle given its length and breadth 4) Printing the cube of a number 5) Swapping two numbers using a third variable Each code example contains a short description and the Java code to implement the described task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Assignment Solutions - Java Variables and Data Types PDF

This document contains 5 Java code examples that demonstrate basic programming concepts: 1) Printing the product of two integer variables 2) Printing the ASCII value of a character 3) Printing the area of a rectangle given its length and breadth 4) Printing the cube of a number 5) Swapping two numbers using a third variable Each code example contains a short description and the Java code to implement the described task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java - Printing(Output),

variable , data types

Assignment Solutions
Assignment Solutions

Q1 - Take 2 integer values in two variables x and y and print their product.

public class Main


{
public static void main(String[] args) {

int x=2;
int y=4;
System.out.print(x*y);
}
}

Q2 - Print the ASCII value of character ‘U’.

public class Main


{
public static void main(String[] args) {

int x='U';
System.out.print("The ascii value of U is : " + x);
}
}

Cracking the Coding Interview in Java - Foundation


Assignment Solutions

Q3 - Write a Java program to take the length and breadth of a rectangle and print its area.

public class Main


{
public static void main(String[] args) {

int length=6;
int breadth=5;
System.out.println("The length is : "+ length);
System.out.println("The breadth is : "+ breadth);
int area=length*breadth;
System.out.println("The area is : "+area);

}
}

Cracking the Coding Interview in Java - Foundation


Assignment Solutions

Q4 - Write a Java program to calculate the cube of a number.

public class Main


{
public static void main(String[] args) {
int x = 2;
int cube=x*x*x;
System.out.println("The side is : "+x);
System.out.println("The cube is : "+cube);

}
}

Cracking the Coding Interview in Java - Foundation


Assignment Solutions

Q5 - Write a Java program to swap two numbers with the help of a third variable.

public class Main


{
public static void main(String[] args) {

int num1=2;
int num2=3;
System.out.println("The first number before swap is :" + num1);
System.out.println("The second number before swap is : " + num2);
int temp; //variable used to swap two numbers
temp=num1;
num1=num2;
num2=temp;
System.out.println("The first number after swap is :" + num1);
System.out.println("The second number after swap is : " + num2);
}
}

Cracking the Coding Interview in Java - Foundation

You might also like