0% found this document useful (0 votes)
2 views2 pages

Java Progs

The document contains three Java programming examples. The first example adds two integers and prints the result, the second checks if a user-entered number is even or odd, and the third prints a right triangle star pattern. Each example demonstrates basic programming concepts and user interaction in Java.

Uploaded by

ksr2024java
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)
2 views2 pages

Java Progs

The document contains three Java programming examples. The first example adds two integers and prints the result, the second checks if a user-entered number is even or odd, and the third prints a right triangle star pattern. Each example demonstrates basic programming concepts and user interaction in Java.

Uploaded by

ksr2024java
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
You are on page 1/ 2

Example1: Program to Add Two Integers

Public class Main {

public static void main(String[] args) {

int first = 10;


int second = 20;

// add two numbers


int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}

Example 2: Program to check if the number


entered by user is even or odd
import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
int num;
System.out.print("Enter an Integer number: ");

//The input provided by user is stored in num


Scanner input = new Scanner(System.in);
num = input.nextInt();

// If number is divisible by 2 then it's an even number


//else it is an odd number
if ( num % 2 == 0 )
System.out.println(num+" is an even number.");
else
System.out.println(num+" is an odd number.");
}
}
Example 3: Program to print Right Triangle Star
Pattern
public class JavaExample
{
public static void main(String args[])
{
int row, column, numberOfRows=8;
for(row=0; row<numberOfRows; row++)
{
for(column=0; column<=row; column++)
{
System.out.print("* ");
}
//This is just to print the stars in new line after each row
System.out.println();
}
}
}

You might also like