Programs
Programs
int sum = a + b;
System.out.println("Sum: " + sum);
int difference = a - b;
System.out.println("Difference: " + difference);
int product = a * b;
System.out.println("Product: " + product);
int quotient = a / b;
System.out.println("Quotient: " + quotient);
int remainder = a % b;
System.out.println("Remainder: " + remainder);
int x = 10;
int y = 5;
int max = (x > y) ? x : y;
System.out.println("Max: " + max);
}
}
OUTPUT:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Max: 10
4.REVERSE A STRING
public class ReverseString {
public static void main(String[] args) {
String str = "Hello, World!";
String reversed = reverseString(str);
System.out.println("Original String: " + str);
System.out.println("Reversed String: " + reversed);
}
public static String reverseString(String str)
{
char[] charArray = str.toCharArray();
int a= 0;
int b = charArray.length - 1;
while (a < b) {
char temp = charArray[a];
charArray[a] = charArray[b];
charArray[b] = temp;
a++;
b--;
}
return new String(charArray);
}
}
5.FACTORIAL
class factorial{
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;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
6.POSITIVE OR NEGATIVE
import java.io.*;
public class number
{
public static void main(String[] args)throws IOException
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(Syst
em.in));
int number;
System.out.print("Enter a number: ");
number=Integer.parseInt(br.readLine());
if (number > 0)
{
System.out.println("The number is positive.");
}
else if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}
}
}
7.GREATEST OF NUMBERS
import java.io.*;
public class greatnumber
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a,b,c;
System.out.print("Enter a number1: ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter a number2: ");
b=Integer.parseInt(br.readLine());
System.out.print("Enter a number3: ");
c=Integer.parseInt(br.readLine());
if (a>b && a>c)
{
System.out.println("The greatest number is "+a);
}
else if (b>a && b>c)
{
System.out.println("The greatest number is "+b);
}
else
{
System.out.println("The greatest number is "+c);
}
}
}
8.FIRST 10 NATURAL & ODD & EVEN NUMBERS
public class numbers {
public static void main(String[] args)
{
System.out.println("First 10 Natural Numbers:");
for (int i = 1; i <= 10; i++)
{
System.out.print(i + " ");
}
10.MULTIPLICATION TABLE
import java .io.*;
public class mul
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,num;
System.out.print("Enter a number ");
num=Integer.parseInt(br.readLine());
for(i=1; i <= 10; i++)
{
System.out.println(num+" * "+i+" = "+num*i);
}
}
}
11.SUM OF ODD NUMBERS
System.out.println("Relational Operators:");
System.out.println("a == b: " + (a == b)); // Equal to
System.out.println("a != b: " + (a != b)); // Not equal to
System.out.println("a < b: " + (a < b)); // Less than
System.out.println("a > b: " + (a > b)); // Greater than
System.out.println("a <= b: " + (a <= b)); // Less than or equal to
System.out.println("a >= b: " + (a >= b)); // Greater than or equal to
System.out.println("\nLogical Operators:");
System.out.println("(a < b) && (b < c): " + ((a < b) && (b < c)));
System.out.println("(a < b) || (b > c): " + ((a < b) || (b > c)));
System.out.println("!(a < b): " + !(a < b));
}
}
13.BORDER LAYOUT
14.FLOW LAYOUT
*<applet code=myButtons.class height=400 width=200>
</applet>
*/
import java.awt.*;
import java.applet.Applet;
15.CARD LAYOUT
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
16.GRID LAYOUT
import java.awt.*;
class GridLayoutEx extends Frame
{
GridLayoutEx()
{
Button[] button=new Button[12];
setLayout(new GridLayout(4,3));
for(int i=0;i<button.length;i++)
{
button[i]=new Button("Button"+(i+i));
add(button[i]);
}
}
}
class GridLayoutJavaExample
{
public static void main(String args[])
{
GridLayoutEx frame=new GridLayoutEx();
frame.setTitle("GridLayout in Java EXample");
frame.setSize(400,150);
frame.setVisible(true);
}
}
17.SINGLE INHERITANCE
import java.io.*;
class Employee {
int salary = 60000;
}
class Engineer extends Employee {
int benefits = 10000;
}
class Gfg {
public static void main(String args[])
{
Engineer E1 = new Engineer();
System.out.println("Salary : " + E1.salary
+ "\nBenefits : " + E1.benefits);
}
}
18.MULTIPLE INHERITANCE
import java.io.*;
import java.lang.*;
import java.util.*;
interface One {
public void print_geek();
}
interface Two {
public void print_for();
}