0% found this document useful (0 votes)
17 views

Java Programs

Java programs 1

Uploaded by

nagpalmokesh18
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)
17 views

Java Programs

Java programs 1

Uploaded by

nagpalmokesh18
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/ 9

Roll No.

- 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.

Q3> Make a program to make a multiplication table as shown


Q4> Write a program to calculate area for circle. Take input at run time.
Q5> Write a program to calculate area for square. Take input at run time.
Q6> Write a program to calculate area for triangle. Take input at run time.
Q7> Write a program to calculate area for rectangle. Take input at run time.
Bonus 1> Write a program to input a number and find its 1's complement and 2's
complement of the number.

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.*;

public class Main {


public static void main(String[] args) {
String name = "",roll_no = "", section = "", branch ="";
Scanner sc = new Scanner(System.in);
System.out.print("Input your name: ");
name = sc.nextLine();

System.out.print("Input your roll number: ");


roll_no = sc.nextLine();

System.out.print("Input your section: ");


section = sc.nextLine();

System.out.print("Input your branch: ");


branch = sc.nextLine();

System.out.println("Your details are as follows: ");


System.out.println("Name: " + name);
System.out.println("Roll. No.: " + roll_no);
System.out.println("Section: " + section);
System.out.println("Branch: " + branch);
}
}

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();

System.out.print("Input your last name: ");


last = sc.next();
System.out.println("Your name is as follows: ");
System.out.println("Name: " + last + " " + first);
}
}
OUTPUT

Q3 - Make a program to make a multiplication table as shown


/*
8/13/2024
Make a program to make a multiplication table as shown
*/
public class Main {
public static void main(String[] args) {
System.out.println("Multiplication table is as follows: ");
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
System.out.print(i*j+"\t");
}
System.out.println();
}
}
}

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a decimal number:");


int decimal = scanner.nextInt();

String hexadecimal = Integer.toHexString(decimal).toUpperCase();

System.out.println("The hexadecimal equivalent is: " +


hexadecimal);

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

You might also like