[Go to site: main page, start]

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

Java Programs

The document contains a series of Java programming examples covering basic concepts such as addition, Fibonacci series, factorial calculation, and array manipulation. It also includes SQL commands for creating, altering, and deleting tables, as well as retrieving specific rows based on conditions. Each example is accompanied by code snippets and expected output.

Uploaded by

Sheetal Sharma
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)
4 views10 pages

Java Programs

The document contains a series of Java programming examples covering basic concepts such as addition, Fibonacci series, factorial calculation, and array manipulation. It also includes SQL commands for creating, altering, and deleting tables, as well as retrieving specific rows based on conditions. Each example is accompanied by code snippets and expected output.

Uploaded by

Sheetal Sharma
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

1. Write a Program in Java to add two numbers.


import [Link];
public class SumOfNumbers
{
public static void main(String args[])
{
int x, y, sum;
Scanner sc = new Scanner([Link]);
[Link]("Enter the first number: ");
x = [Link]();
[Link]("Enter the second number: ");
y = [Link]();
sum = x + y;
[Link]("The sum of two numbers x and y is: " + sum);
}
OutPut:
Enter the first number: 34
Enter the second number: 12
The sum of two numbers x and y is: 46

2. Write a Program to print Fibonacci Series in Java without using recursion


class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
}

}}

Output:

0 1 1 2 3 5 8 13 21 34

3. Factorial Program using loop in java


class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i; }
[Link]("Factorial of "+number+" is: "+fact); }
}
Output:
Factorial of 5 is: 120

4. Java Program to Find Sum of Natural Numbers


public class SumOfNaturalNumber1
{
public static void main(String[] args)
{
int i, num = 10, sum = 0;
//executes until the condition returns true
for(i = 1; i <= num; ++i)
{
//adding the value of i into sum variable
sum = sum + i;
}
//prints the sum
[Link]("Sum of First 10 Natural Numbers is = " + sum);
}
}
Output:
Sum of First 10 Natural Numbers is = 55

5. Write a Program in java to print Right Triangle Star Pattern


public class RightTrianglePattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row=6;
//outer loop for rows
for(i=0; i<row; i++)
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
[Link]("* ");
}
//throws the cursor in a new line after printing each line
[Link]();
}
}
}
Output:
6. Write a program to print Reverse a number using while loop
public class ReverseNumberExample1
{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
[Link]("The reverse of the given number is: " + reverse);
}
}
Output:
The reverse of the given number is: 456789

7. Java Program to Check if a Number is Positive or Negative


import [Link];
public class CheckPositiveOrNegativeExample2
{
public static void main(String[] args)
{
int num;
//object of the Scanner class
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
//reading a number from the user
num = [Link]();
//checks the number is greater than 0 or not
if(num>0)
{
[Link]("The number is positive.");
}
//checks the number is less than 0 or not
else if(num<0)
{
[Link]("The number is negative.");
}
//executes when the above two conditions return false
else
{
[Link]("The number is zero.");
}
}
}
Output:
Enter a number: 23
The number is positive.
8. Write a program to create objects in java
public class CreateObjectExample
{
//constructor of the class
CreateObjectExample()
{
[Link]("Hello World");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObjectExample obj = new CreateObjectExample();
}

Output:
Hello World

9. Java Program to Find Largest of Three Numbers


import [Link];
public class LargestNumberExample1
{
public static void main(String[] args)
{
int a, b, c, largest, temp;
//object of the Scanner class
Scanner sc = new Scanner([Link]);
//reading input from the user
[Link]("Enter the first number:");
a = [Link]();
[Link]("Enter the second number:");
b = [Link]();
[Link]("Enter the third number:");
c = [Link]();
//comparing a and b and storing the largest number in a temp variable
temp=a>b?a:b;
//comparing the temp variable with c and storing the result in the variable
largest=c>temp?c:temp;
//prints the largest number
[Link]("The largest number is: "+largest);
}
}
Output:
Enter the first number:
23
Enter the second number:
11
Enter the third number:
67
Largest Number is: 67
10. Java Program to Find Smallest of Three Numbers Using Ternary Operator
import [Link];
public class SmallestNumberExample2
{
public static void main(String[] args)
{
int a, b, c, smallest;
//object of the Scanner class
Scanner sc = new Scanner([Link]);
//reading input from the user
[Link]("Enter the first number:");
a = [Link]();
[Link]("Enter the second number:");
b = [Link]();
[Link]("Enter the third number:");
c = [Link]();
smallest = c < (a < b ? a : b) ? c : ((a < b) ? a : b);
[Link]("The smallest number is: "+smallest);
}
}
Output:
Enter the first number:
45
Enter the second number:
87
Enter the third number:
34
The smallest number is: 34

11. Write a Program to print the elements of an array in java


public class PrintArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
[Link]("Elements of given array: ");
//Loop through the array by incrementing value of i
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Output:
Elements of given array:
12345
12. Write a program to print the largest element in an array

public class LargestElement_array {


public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < [Link]; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
[Link]("Largest element present in given array: " + max);
}
}

Output:
Largest element present in given array: 75

13. Write a program to print the smallest element in an array


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

//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize min with first element of array.
int min = arr[0];
//Loop through the array
for (int i = 0; i < [Link]; i++) {
//Compare elements of array with min
if(arr[i] <min)
min = arr[i];
}
[Link]("Smallest element present in given array: " + min);
}
}
Output:
Smallest element present in given array: 7
14. Java Program to sort the elements of an array in ascending order
public class SortAsc {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
//Displaying elements of original array
[Link]("Elements of original array: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
//Sort the array in ascending order
for (int i = 0; i < [Link]; i++) {
for (int j = i+1; j < [Link]; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]();
//Displaying elements of array after sorting
[Link]("Elements of array sorted in ascending order: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}
Output:
Elements of original array:
52871
Elements of array sorted in ascending order:
12578

15. Java Program to print the sum of all the items of the array
public class SumOfArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
int sum = 0;
//Loop through the array to calculate sum of elements
for (int i = 0; i < [Link]; i++) {
sum = sum + arr[i];
}
[Link]("Sum of all the elements of an array: " + sum);
}
}
Output:
Sum of all the elements of an array: 15
SQL

1. Create Table

2. Create a Table using another table

3. Drop Table

4. Multiple insert
5. Delete table

6. Alter table to Add Column

Output:

7. Alter table to Rename column

Output:

8. Drop a Column

9. Changes the data type of an existing column

Output:

10. Retrieve rows where City is 'Lucknow'

Output:
11. Retrieve rows where City is 'Lucknow' and Monthly_Salary > 25000

Output:

12. Retrieve rows where City is 'Lucknow' or 'Ghaziabad'

Output:

13. Retrieve rows where City is one of ('Lucknow', 'Kolkata')

Output:

14. Retrieve rows where Monthly_Salary is between 25000 and 35000

Output:

15. Delete the employee whose Employee_ID is 102

Output:

You might also like