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

java lab-1

The document contains ten Java programs, each with a specific aim and corresponding code. The programs include printing messages, performing arithmetic operations, swapping numbers, finding the largest number, calculating factorials, determining even or odd numbers, checking for Armstrong numbers, reversing numbers, multiplying two numbers, and calculating the volume of a box. Each program is structured with a class and a main method, showcasing fundamental programming concepts.

Uploaded by

aryanbajpai916
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java lab-1

The document contains ten Java programs, each with a specific aim and corresponding code. The programs include printing messages, performing arithmetic operations, swapping numbers, finding the largest number, calculating factorials, determining even or odd numbers, checking for Armstrong numbers, reversing numbers, multiplying two numbers, and calculating the volume of a box. Each program is structured with a class and a main method, showcasing fundamental programming concepts.

Uploaded by

aryanbajpai916
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 1:-

AIM:-Write a program to print a mesaage

class Hello

public static void main(String[] args){

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

Output:-

Program 2:
Aim:-WAP to print addition of two numbers

class Hello

public static void main(String[] args){

int a=10,b=20;

int c=a+b;

System.out.println("Addition of two numbers "+c);

Output:-

Program 3:-

Aim:-Wap to print swap of two numbers


public class SwapNumbers {

public static void main(String[] args) {

int num1 = 10;

int num2 = 20;

System.out.println("Before swapping:");

System.out.println("num1 = " + num1);

System.out.println("num2 = " + num2);

int temp = num1;

num1 = num2;

num2 = temp;

System.out.println("\nAfter swapping:");

System.out.println("num1 = " + num1);

System.out.println("num2 = " + num2);

Output:-

Program 4:-

Aim:-Write a program to find the largest of two numbers in java

public class Main


{

public static void main (String[]args)

int num1 = 50, num2 = 20;

if (num1 == num2)

System.out.println ("both are equal");

else if (num1 > num2)

System.out.println (num1 + " is greater");

else

System.out.println (num2 + " is greater");

Output:-

Program 5:-

Aim:- Wap to find the factorial of a number

public class Factorial {

public static void main(String[] args) {


int num = 10;

long factorial = 1;

for(int i = 1; i <= num; ++i)

// factorial = factorial * i;

factorial *= i;

System.out.printf("Factorial of %d = %d", num, factorial);

Output:-

Program 6:-

Aim:- Wap to find the number is even or odd

import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

if (number % 2 == 0) {

System.out.println(number + " is even.");

} else {

System.out.println(number + " is odd.");

scanner.close();

Output:-

Program 7:-

AIM:-WAP to find that a number is Armstrong number or not?

import java.util.Scanner;

public class Hello {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

int originalNumber = number;

int numDigits = String.valueOf(number).length();

int sum = 0;

int temp = number;

while (temp > 0) {

int digit = temp % 10;

sum += Math.pow(digit, numDigits);

temp /= 10;

if (sum == originalNumber) {

System.out.println(originalNumber + " is an Armstrong number.");

} else {

System.out.println(originalNumber + " is not an Armstrong number.");

scanner.close();

Output:-
Program 8:-

Aim:- Wap to find the reverse of a number

import java.util.Scanner;

public class ReverseNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter a number: ");

int number = scanner.nextInt();

int reversedNumber = 0;

while (number != 0) {

int digit = number % 10;

reversedNumber = reversedNumber * 10 + digit;

number /= 10;

System.out.println("Reversed number: " + reversedNumber);

scanner.close();

Output:-

Program 9:-

Aim:-Wap to find Multiplication of two numbers

import java.util.Scanner;

public class MultiplyTwoNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

double num1 = scanner.nextDouble();


System.out.print("Enter the second number: ");

double num2 = scanner.nextDouble();

double product = num1 * num2;

System.out.println("The product of " + num1 + " and " + num2 + " is: "
+ product);

scanner.close();

Output:-

Program 10:-

Aim:-Wap to calculate the volume of box

public class RectangleVolume {

public static void main(String[] args) {

double length = 10;

double width = 5;

double height = 3;

double volume = calculateVolume(length, width, height);


System.out.println("The volume of the rectangle is: " + volume);

public static double calculateVolume(double length, double width, double


height) {

return length * width * height;

Output:-

You might also like