Java Programs
Java Programs
- MCA/10071/24
Date – 8/13/2024
Q1> Write a program to print your name, roll no, section and branch
in separate lines
Q2> Write a program in Java to take first name and last name from the user and
print both in one line as last name followed by first name.
Bonus 2> Write a program to input a decimal number and find its hexadecimal
value.
Bonus 3> Write a program to find the bytes of all the primitive datatypes.
Q1 - Write a program to print your name, roll no, section and branch
in separate lines
/*
8/13/2024
Q> Write a program to print your name, roll no, section and branch
in separate lines **/
import java.util.*;
OUTPUT
Q2 - Write a program in Java to take first name and last name from the user and
print both in one line as last name followed by first name.
import java.util.Scanner;
/*
8/13/2024
Write a program in Java to take first name and last name from the user and
print both in one line as
last name followed by first name.
*/
public class Main {
public static void main(String[] args) {
String first = "",last = "";
Scanner sc = new Scanner(System.in);
System.out.print("Input your first name: ");
first = sc.next();
OUTPUT
Q4 - Write a program to calculate area for circle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for circle. Take input at run time.
*/
import java.util.Scanner;
public class Main {
private double area(int a){
return (22*a*a)/7;
}
public static void main(String[] args) {
double result = 0.0;
int a = 0;
Main obj = new Main();
Scanner sc= new Scanner(System.in);
System.out.print("Input the radius of the circle: ");
a = sc.nextInt();
result = obj.area(a);
System.out.println("Area: " + result + " unit(s) sq.");
}
}
OUTPUT
Q5 - Write a program to calculate area for square. Take input at run time.
/*
8/13/2024
Write a program to calculate area for square. Take input at run time.
*/
import java.util.Scanner;
public class Main {
private double area(int a){
return (a*a);
}
public static void main(String[] args) {
double result = 0.0;
int a = 0;
Main obj = new Main();
Scanner sc= new Scanner(System.in);
System.out.print("Input the side of the square: ");
a = sc.nextInt();
result = obj.area(a);
System.out.println("Area: " + result + " unit(s) sq.");
}
}
OUTPUT
Q6 - Write a program to calculate area for triangle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for triangle. Take input at run time.
*/
import java.util.Scanner;
public class Main {
private double area(int a, int b, int c){
double s = (a + b + c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public static void main(String[] args) {
double result = 0.0;
int a = 0, b = 0, c = 0;
Main obj = new Main();
Scanner sc= new Scanner(System.in);
System.out.print("Input the first side of the triangle: ");
a = sc.nextInt();
System.out.print("Input the second side of the triangle: ");
b = sc.nextInt();
System.out.print("Input the third side of the triangle: ");
c = sc.nextInt();
result = obj.area(a, b, c);
System.out.println("Area: " + result + " unit(s) sq.");
}
}
OUTPUT
Q7 - Write a program to calculate area for rectangle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for rectangle. Take input at run time.
*/
import java.util.Scanner;
public class Main {
private double area(int a, int b){
return (a * b);
}
public static void main(String[] args) {
double result = 0.0;
int a = 0, b = 0;
Main obj = new Main();
Scanner sc= new Scanner(System.in);
System.out.print("Input the length of the rectangle: ");
a = sc.nextInt();
System.out.print("Input the breath of the rectangle: ");
b = sc.nextInt();
result = obj.area(a, b);
System.out.println("Area: " + result + " unit(s) sq.");
}
}
OUTPUT
BONUS 1 - Write a program to input a number and find its 1's complement and
2's complement of the number.
/*
8/13/2024
Write a program to input a number and find its 1's complement and 2's
complement of the number.
*/
import java.util.Scanner;
public class Main {
private double area(int a){
return (a*a);
}
public static void main(String[] args) {
int a = 0;
String b ="", c = "";
Scanner sc= new Scanner(System.in);
System.out.print("Input a number: ");
a = sc.nextInt();
if(a<0){
b = Integer.toBinaryString(-a);
while (b.length() < 32) {
b = "0" + b;
}
for(int i=0 ; i < b.length() ; i++){
c += (b.charAt(i) == '0') ? '1' : '0';
}
System.out.println("1's complement: " + c);
}else{
System.out.println("1's complement: " +
Integer.toBinaryString(a));
}
System.out.println("2's complement: " + Integer.toBinaryString(a));
}
}
OUTPUT
BONUS 2 - Write a program to input a decimal number and find its
hexadecimal value
/*
8/13/2024
Q> Write a program to covert a decimal number to its hexadecimal value
*/
import java.util.Scanner;
scanner.close();
}
}
OUTPUT
BONUS 3 - Write a program to find the size of all the primitive datatypes
/*
8/13/2024
Q> Write a program to find the size of all the primitive datatypes
*/
public class Main {
public static void main(String[] args) {
System.out.println("Size of byte: " + Byte.BYTES + " bytes");
System.out.println("Size of short: " + Short.BYTES + " bytes");
System.out.println("Size of int: " + Integer.BYTES + " bytes");
System.out.println("Size of long: " + Long.BYTES + " bytes");
System.out.println("Size of float: " + Float.BYTES + " bytes");
System.out.println("Size of double: " + Double.BYTES + " bytes");
System.out.println("Size of char: " + Character.BYTES + " bytes");
System.out.println("Size of boolean: " + "JVM dependent");
}
}
OUTPUT