0% found this document useful (0 votes)
4 views10 pages

JAVA Program

The document contains multiple Java programs demonstrating various functionalities such as printing 'Hello World', performing basic arithmetic operations, reversing a number, printing days of the week, and implementing a simple calculator. It also includes programs for employee details, finding the largest of three numbers, swapping numbers, printing numbers, generating multiplication tables, and creating different star patterns. Each program is structured with a class and a main method, showcasing fundamental Java programming concepts.

Uploaded by

Jahnvi Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views10 pages

JAVA Program

The document contains multiple Java programs demonstrating various functionalities such as printing 'Hello World', performing basic arithmetic operations, reversing a number, printing days of the week, and implementing a simple calculator. It also includes programs for employee details, finding the largest of three numbers, swapping numbers, printing numbers, generating multiplication tables, and creating different star patterns. Each program is structured with a class and a main method, showcasing fundamental Java programming concepts.

Uploaded by

Jahnvi Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

JAVA Program

1. write a java program to print hello world.

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

System.out.println("Hello World!");
}

2. Java program to find addition, subtraction, multiplication, division, and remainder.

class mathematicsOperation{
public static void main(String[] args){
int a=50;
int b=30;
System.out.println("Addition:"+(a+b));
System.out.println("Subtraction:"+(a-b));
System.out.println("Multiplication:"+(a*b));
System.out.println("Division:"+(a/b));
System.out.println("Remainder:"+(a%b));

Output:

3. Write a java program to perform operators.


class operator{
public static void main(String[]args){
int a=20,b=10;
int result1,result2;
result1=++a;
result2=--b;
int largest;

System.out.println("Add:"+(a+b));
System.out.println("Sub:"+(a-b));
System.out.println("multiply:"+(a*b));
System.out.println("div:"+(a/b));
System.out.println("Sub:"+(a%b));
System.out.println("Sub:"+(a-b));
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
System.out.println((a > b) && (a > b));
System.out.println((a > b) && (a < b));
System.out.println((a < b) || (a > b));
System.out.println((a > b) || (a < b));
largest=a>b?a:b;
System.out.println(largest);
System.out.println(!(a == b));
System.out.println(!(a > b));
System.out.println(a == b);
System.out.println("increment=" +result1);
System.out.println("decrement=" +result2);
}
}
Output:-

4. Write a java program to reverse a number.


class Reverse{
public static void main(String[] args)
{
int number = 98765, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given number is: " + reverse);
}
}

Output:-

5. Write a java program to print the days.

import java.util.Scanner;
class NChoice{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Number Between 1 to 7 Only: ");
int day_no =sc.nextInt();

switch(day_no){
case 1 : System.out.println("Monday");
break;
case 2 : System.out.println("Tuesday");
break;
case 3 : System.out.println("Wednesday");
break;
case 4 : System.out.println("Thrusday");
break;
case 5 : System.out.println("Friday");
break;
case 6 : System.out.println("Saturday");
break;
case 7 : System.out.println("Sunday");
break;
default:
System.out.println("Invalid choice");
}
}
}
6.write a java program to print calculator.

import java.util.Scanner;
class calculator{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a first number: ");
int num1 =sc.nextInt();
System.out.print("Enter a second number: ");
int num2 =sc.nextInt();
System.out.println("Choose an operator: +, -, *,%,/");
char operator = sc.next().charAt(0);

switch(operator){
case '+' : System.out.println("Addition is:" +(num1+num2));
break;
case '-' : System.out.println("Subtraction is:"+(num1-num2));
break;
case '*' : System.out.println("Multiplication is:"+(num1*num2));
break;
case '%' : System.out.println("Modulus is:"+(num1%num2));
break;
case '/' : System.out.println("Division is:"+(num1/num2));
break;
default:
System.out.println("Invalid choice");
}
}
}
Output:-

7.write a java program to print employee details.

import java.util.Scanner;

class Employee{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter employee name: ");
String name = scanner.nextLine();

System.out.print("Enter employee age: ");


int age = scanner.nextInt();

System.out.print("Enter employee department: ");


scanner.nextLine();
String department = scanner.nextLine();

System.out.print("Enter employee salary: ");


double salary = scanner.nextDouble();

scanner.close();

System.out.println("\nEmployee Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Department: " + department);
System.out.println("Salary: " + salary);
}
}

Output:-

8.write a java program to print largest of three numbers.

public class larger{


public static void main (String[]args)
{

int num1 = 10, num2 = 20, num3 = 30;

int temp, result;

temp = num1>num2 ? num1:num2;


result = temp>num3 ? temp:num3;
System.out.println (result + " is the greatest");
}
}

Output:-

9. swapping two numbers using third variable.


public class swap {

public static void main(String[] args) {


int a, b, temp;

a = 15;
b = 27;

System.out.println("Before swapping : a, b = " + a + ", " + b);

temp = a;
a = b;
b = temp;

System.out.println("After swapping : a, b = " + a + ", " + b);


}
}

Output:-

10. swapping two numbers without using third variable.

class swap1 {
public static void main(String[] args) {
int a, b;
a = 15;
b = 27;
System.out.println("Before swapping : a, b = " + a + ", " + b);

a = a + b;
b = a - b;
a = a - b;

System.out.println("After swapping : a, b = " + a + ", " + b);


}
}
Output:-

11. Java program to print numbers

import java.util.Scanner;
class number{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter the number:");
n =sc.nextInt();
for(int i=1;i<=n;i++){
System.out.println(i);
}
}

Output:-

12. write a java program to print table

import java.util.Scanner;
class table{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of table");
int n =sc.nextInt();
for(int i=1; i <= 10; i++){
System.out.println(n+" * "+i+" = "+n*i);
}

}
}

Output:-
13. write a program to print different star pattern.
import java.util.Scanner;
public class Star2{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output:-

import java.util.Scanner;
public class Star1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n = sc.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output:-
import java.util.Scanner;
public class Star3{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:-

import java.util.Scanner;
class Star {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n = sc.nextInt();
for (int i=0;i<=n-1;i++) {
for (int j=0;j<=i;j++) {
System.out.print("*" + " ");
}

System.out.println("");
}
for (int i=n-1;i>=0;i--) {
for (int j=0;j<=i-1;j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
}
}
Output:-

You might also like