JAVA
01. Write a JAVA program to print the string Hello World.
public class Helloworld {
public static void main(String[] args) {
System.out.println("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++)
System.out.print(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 java.util.Scanner;
public class PositiveNegativeZero {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = reader.nextInt();
if (num < 0)
System.out.println(+num + " is a negative number.");
else if (num > 0)
System.out.println(+num + " is a positive number.");
else
System.out.println(+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 java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the first number: ");
int n1 = reader.nextInt();
System.out.print("Enter the second number: ");
int n2 = reader.nextInt();
System.out.print("Enter the third number: ");
int n3 = reader.nextInt();
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(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 java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(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 java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the MultiplicationTable number: ");
int num = reader.nextInt();
for(int i = 1; i <= 10; i++)
{
System.out.printf("%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 java.util.Scanner;
public class AreaOfRectangle {
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the length of the Rectangle:");
double l= s.nextDouble();
System.out.println("Enter the width of the Rectangle:");
double b= s.nextDouble();
double area=l*b;
System.out.println("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;
}
System.out.println("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 java.util.Scanner;
class Fibonacci
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=s.nextInt();
int num1=-1, num2=1,num3=0;
System.out.println("Fibonacci series is ");
for(int i=0;i<n;i++)
{
num3=num1+num2;
num1=num2;
num2=num3;
System.out.print(num3);
System.out.print("\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 java.util.Scanner;
public class Factorial {
public static void main(String arg[])
{
int n,fact=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter a number:");
n=s.nextInt();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial of the given number "+n +" is " +fact);
}
}
OUTPUT
11. Write a program to sort an Array of strings in alphabetical order
import java.io.*;
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;
}
}
}
System.out.println(
"The names in alphabetical order are: ");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
OUTPUT
12. Write a program to enable user to handle Divide By Zero Exception.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 6;
int b = 0;
System.out.print(a / b);
}
}
OUTPUT
13. Write the program to print the following pattern.
*
**
***
****
*****
******
import java.util.*;
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--) {
System.out.print("* ");
}
for (j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
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 java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int[] array = new int[] { 12,45,78,5,33 };
Arrays.sort(array);
System.out.println(Arrays.toString(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++) {
System.out.println(i);
}
}
}
OUTPUT