0% found this document useful (0 votes)
5 views9 pages

JAVA PROGRAMMING

The document contains a Java programming assignment with ten exercises. Each exercise includes a Java program that demonstrates basic programming concepts such as printing output, performing arithmetic operations, finding maximum values, checking even or odd numbers, calculating factorials, and more. The document provides both the source code and expected output for each exercise.

Uploaded by

kanishkasp2006
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)
5 views9 pages

JAVA PROGRAMMING

The document contains a Java programming assignment with ten exercises. Each exercise includes a Java program that demonstrates basic programming concepts such as printing output, performing arithmetic operations, finding maximum values, checking even or odd numbers, calculating factorials, and more. The document provides both the source code and expected output for each exercise.

Uploaded by

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

JAVA PROGRAMMING

ASSIGNMENT 1

KALAIARASAN S
B.TECH IT-B
61782323106046
JAVA PROGRAMMING LAB – B Section
EXERCISE 1 A

1).Write a Java program to print hello world.

PROGRAM:

class ex1

public static void main (String args[])

System.out.print("Hello world");

OUTPUT:

Hello world

2. Write a Java program to add two numbers.

PROGRAM:

import java.util.Scanner;

class ex2

public static void main(String args[])


{

Scanner scan=new Scanner (System.in);

System.out.println("Enter the numbers:");

int a= scan.nextInt();

int b=scan.nextInt();

int c= a+b;

System.out.print("c="+c);

}}

OUTPUT:

Enter the numbers:10 20

C=30

3. Write a Java program to find maximum of 3 numbers

PROGRAM:

import java.util.Scanner;

class ex3{

public static void main(String args[])

Scanner scan = new Scanner (System.in);

System.out.print("Enter the three Numbers :");

int a= scan.nextInt();

int b= scan.nextInt();

int c= scan.nextInt();

if(a>b && a>c)

System.out.print( "Ther number a is greater");

else if(b>c)

System.out.print( "Ther number b is greater");


}

else

System.out.print( "Ther number c is greater");

OUTPUT:

Enter the numbers:10 5 60

The number c is greater

4. Write a Java program to check whether the number is odd or


even.

PROGRAM:

import java.util.Scanner;

class ex4

public static void main(String args[])

Scanner scan = new Scanner (System.in);

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

int a = scan.nextInt();

if(a%2==0)

System.out.print("Even number");

else

System.out.print("Odd Number");
}

OUTPUT:

Enter the number:10

Even number

5. Write a Java program to find the factorial of a number.

PROGRAM:

import java.util.Scanner;

class ex5{

public static void main(String args[])

Scanner scan = new Scanner(System.in);

int i,fact=1;

int number=scan.nextInt();

for(i=1;i<=number;i++){

fact=fact*i;

System.out.println("Factorial of "+number+" is: "+fact);

OUTPUT:

Factorial of 5 is 120

6. Write a Java program to print the following pattern.

**
***

****

PROGRAM:

import java.util.Scanner;

class ex6

public static void main(String args[])

int i, j, row=5;

for(i=0; i<row; i++)

for(j=0; j<=i; j++)

System.out.print("* ");

System.out.println();

OUTPUT:

**

***

****

7. Write a Java program to multiply two numbers.

PROGRAM:

import java.util.Scanner;

class ex7
{

public static void main(String args[])

Scanner scan=new Scanner (System.in);

System.out.println("Enter the numbers:");

int a= scan.nextInt();

int b=scan.nextInt();

int c= a*b;

System.out.print("c="+c);

OUTPUT:

Enter the numbers : 10 5

C=50

8. Write a Java program to check leap year or not.

PROGRAM:

import java.util.Scanner;

class ex8{

public static void main(String args[])

Scanner scan=new Scanner(System.in);

System.out.print("Enter the year :");

int a = scan.nextInt();

if((a%4==0 && a%400==0)|| a%100!=0)

System.out.print("Leap year");

else

{
System.out.print("Not a leap year");

OUTPUT:

Enter the year:2020

Leap year

9. Write a Java program to find quotient and remainder from two


inputs.

PROGRAM:

import java.util.Scanner;

class ex9

public static void main(String args[])

Scanner scan = new Scanner(System.in);

System.out.print("Enter the two numbers:");

int a = scan.nextInt();

int b = scan.nextInt();

int c = a/ b;

System.out.println("Quotient is : " +c);

int d = a%b;

System.out.print("Reminder is : " +d);

OUTPUT:

Enter the two numbers: 3 15

Quotient is :5

Reminder is : 0
10. Write a Java program to calculate power of a number.

PROGRAM:

class ex10 {

public static void main(String[] args) {

int base = 3, exponent = 4;

long result = 1;

while (exponent != 0) {

result *= base;

--exponent;

System.out.println("Answer = " + result);

OUTPUT:

Answer = 81

You might also like