0% found this document useful (0 votes)
37 views13 pages

JAVA

Uploaded by

asmainamdar2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views13 pages

JAVA

Uploaded by

asmainamdar2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA

01. Write a JAVA program to print the string Hello World.


public class Helloworld {

public static void main(String[] args) {


[Link]("Hello World");

}
}
OUTPUT :-

02. Write a Java program to display character A to Z using for loop.

public class CharactersAtoZ {


public static void main(String[] args) {
char c;
for(c = 'A'; c <= 'Z'; c++)
[Link](c + " ");
}

OUTPUT
02. Write a Java program to check if a given Number is Positive or Negative
or Zero
using if..else statement by getting the input of a number of user choice
during
runtime.

import [Link];
public class PositiveNegativeZero {
public static void main(String[] args) {
Scanner reader = new Scanner([Link]);
[Link]("Enter the number: ");

int num = [Link]();


if (num < 0)
[Link](+num + " is a negative number.");
else if (num > 0)
[Link](+num + " is a positive number.");
else
[Link](+num + " The given number is zero.");

}
}

OUTPUT
03. Write a Java program to find the largest among three numbers using
if..else
statement by getting the input of three number of user choice during
run time.

import [Link];
public class LargestOfThree {
public static void main(String[] args) {

Scanner reader = new Scanner([Link]);


[Link]("Enter the first number: ");
int n1 = [Link]();
[Link]("Enter the second number: ");
int n2 = [Link]();
[Link]("Enter the third number: ");
int n3 = [Link]();
if( n1 >= n2 && n1 >= n3)

[Link](n1 + " is the largest number.");


else if (n2 >= n1 && n2 >= n3)

[Link](n2 + " is the largest number.");


else

[Link](n3 + " is the largest number.");


}
}

OUTPUT

04. Write a Java program to check whether a number is even or odd using
if...else
statement by getting the input of a number of user choice during run
time.

import [Link];
public class EvenOdd {

public static void main(String[] args) {


Scanner reader = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(num % 2 == 0)
[Link](num + " is even");
else
[Link](num + " is odd");

}
}

OUTPUT

05. Write a Java program to Generate Multiplication Table (number of


iteration
(from 1 to 10)) using for loop of a number by getting the input of a
number of
user choice during runtime.

import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner reader = new Scanner([Link]);
[Link]("Enter the MultiplicationTable number: ");
int num = [Link]();
for(int i = 1; i <= 10; i++)

{
[Link]("%d * %d = %d \n", num, i, num * i);

}
}
}

OUTPUT

07. Write a Java program to find the Area of Rectangle by getting the
input of
length and breadth from the user during run time.

import [Link];
public class AreaOfRectangle {

public static void main(String args[])


{

Scanner s= new Scanner([Link]);


[Link]("Enter the length of the Rectangle:");
double l= [Link]();
[Link]("Enter the width of the Rectangle:");
double b= [Link]();
double area=l*b;
[Link]("Area of Rectangle is: " + area);
}
}

OUTPUT

08. Write a JAVA program to print the sum of first 10 natural numbers 1
to 10.

public class SumNatural {

public static void main(String[] args) {

int i, sum = 0;

for(i = 1; i <= 10; ++i)


{

sum = sum + i;
}

[Link]("The Sum of first 10 Natural numbers is = " + sum);


}
}
OUTPUT

09. Write a Java program to print the FIBONACCI SERIES by getting the
input of
number of terms in the series during run time.

0 1 1 2 3 5 8 13 21 34

import [Link];
class Fibonacci
{

public static void main(String[] args)


{
Scanner s=new Scanner([Link]);
[Link]("Enter number of terms");
int n=[Link]();
int num1=-1, num2=1,num3=0;
[Link]("Fibonacci series is ");
for(int i=0;i<n;i++)
{
num3=num1+num2;
num1=num2;
num2=num3;
[Link](num3);
[Link]("\t");
}
}
}

OUTPUT
10. Write a Java program to print the FACTORIAL of a given number by
getting

the input of a number of user choice during run time.

import [Link];
public class Factorial {
public static void main(String arg[])

{
int n,fact=1;
Scanner s=new Scanner([Link]);
[Link]("Enter a number:");
n=[Link]();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
[Link]("Factorial of the given number "+n +" is " +fact);

}
}

OUTPUT
11. Write a program to sort an Array of strings in alphabetical order
import [Link].*;

class GFG {
public static void main(String[] args)
{
int n = 4;

= { "Rahul", "Ajay", "Gourav", "Riya" };


String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {

if (names[i].compareTo(names[j]) > 0) {
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}

[Link](
"The names in alphabetical order are: ");
for (int i = 0; i < n; i++) {
[Link](names[i]);
}
}
}
OUTPUT

12. Write a program to enable user to handle Divide By Zero Exception.


import [Link].*;
class GFG {
public static void main(String[] args)
{
int a = 6;
int b = 0;
[Link](a / b);
}
}
OUTPUT
13. Write the program to print the following pattern.
*
**
***
****
*****
******
import [Link].*;
public class GeeksForGeeks {
public static void printPattern(int n)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = n - i; j > 1; j--) {
[Link]("* ");
}
for (j = 0; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}

14. write a program in java to sort an array in ascending order and display it.
import [Link];
public class GFG {
public static void main(String[] args)
{
int[] array = new int[] { 12,45,78,5,33 };
[Link](array);
[Link]([Link](array));
}
}
OUTPUT
15. Java program to write a code in for loop from 1 to 10

class GFG {
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++) {
[Link](i);
}
}
}
OUTPUT

You might also like